2

How can I remove dummy information from a file named results.txt with lines like this?

Lines inside file are like this:

_my0001_split00000000.txt:Total Dynamic Power    =   0.0000 mW        
_my0001_split00000050.txt:Total Dynamic Power    = 117.5261 uW  (100%)

... and they should change to a tab-separated format like this:

0001    00000000    0.0000  mW  
0001    00000050    117.5261    uW  

4 Answers 4

6

How about a using sed instead of awk?

sed -r 's/^_my([0-9]+)_split([0-9]+)\.txt:[^=]*=\s*([0-9.]+) *(\S+).*/\1\t\2\t\3 \4/' /path/to/file
6

If you have GNU awk, then you can indeed specify fixed fieldwidths e.g.

gawk -vFIELDWIDTHS="3 4 6 8 30 8 1 3" -vOFS="\t" '{print $2,$4,$6,$8}' results.txt
0001    00000000          0.0000        mW
0001    00000050        117.5261        uW

However you could also consider a regular expression based solution - for example, in perl you could print the first three numeric values plus power units as:

perl -lne 'print join "\t", (/\d*\.?\d+|\b.W\b/g)[0..3]' results.txt
0001    00000000        0.0000  mW
0001    00000050        117.5261        uW
0
4

You can do it in awk. For example:

$ awk -vOFS="\t" '{ 
                    sub(/.txt:.*= */," "); 
                    sub(/^.../,"",$1); 
                    sub(/_split/, "\t",$1); 
                    print $1,$2,$3
                  }' file  
0001    00000000    0.0000  mW
0001    00000050    117.5261    uW

It really isn't the best tool for the job though. Awk excels when you want to extract fields unchanged, it can get cumbersome when you need to edit them.

I would instead use something like:

$ perl -lne '
    @m=(/\D+(\d+)\D+(\d+).*=\s*([0-9.]+)\s+(\S+).*$/); 
    print join "\t", @m' file 
0001    00000000    0.0000  mW
0001    00000050    117.5261    uW
3
sed 's/_my//;s/_split/\i/;s/\.txt:Total Dynamic Power *= */\i/' /path/to/input
1
  • 6 seconds difference. :D Commented Jan 6, 2017 at 17:22

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.