Sed parsing data: fields (words) separated with whitespace

sed

I'm using sed on Linux, trying to match data lines having three fields, tab separated (but the separation could be any whitespace), as in:

 12.3 0a 1b
 15.5 0v 1h
 17.7 5k 3c

; right now I'm using this:

sed -n 's/^\([^[:blank:]]*\)[[:blank:]]*\([^[:blank:]]*\)[[:blank:]]*\([^[:blank:]]*\)/\1\t\3\t\2/p' mydata.txt

… so I'm able to extract and manipulate (in the example, just position inversion) individual fields via \1, \2, \3.

Is there a better way to specify this?

Cheers!

Best Answer

A trivial example in awk to suggest what can be done

awk '{print $2 $1 $3}` < input_file.txt

simply rearranges the first two fields while printing all three on all lines.

To rearrange those lines have exactly three fields, preserve any that start with # (i.e. comment in sh-like languages) and delete all others

awk `/^#/{print $0;next} NF==3{print $2 $1 $3;next} {}' < input_file.txt

Most unix systems have a fairly complete awk man-page.

The important this for your purposes here is that the fields are accessibly with $1, $2, ..., where "field" is defined as strings of stuff separated by FS (i.e. the field separator) which defaults to (a space).

Related Question