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!
sed
may not be the best tool for this. Is ther a particular reason you don't want to useawk
, orperl
or something better suited?awk
orperl
, so whenever I have a "field inversion in text data" problem like this, the first thing I think of is what I know: "\2 \1
" which is something I get fromsed
. However,sed
not being suited for the task is also an answer I appreciate (since as I said, that's not the first thing that pops to my mind when I have this problem)... Cheers!