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
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 othersMost 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).