1832977 Members
3041 Online
110048 Solutions
New Discussion

sed -> awk buffers ...

 
SOLVED
Go to solution
A. Daniel King_1
Super Advisor

sed -> awk buffers ...

Alright shell gurus, here is an easy 10 points.

sed "s/^\([^:]*\):\([^:]*\)/\2:\1/" /tmp/somefile.txt

This sed snippet takes a colon delimited file and switches the first and second field. How do you do the same thing in awk?
Command-Line Junkie
10 REPLIES 10
Pete Randall
Outstanding Contributor

Re: sed -> awk buffers ...

cat /tmp/somefile.txt | awk '{ print $2, $1 }'


Pete

Pete
A. Daniel King_1
Super Advisor

Re: sed -> awk buffers ...

So close ... "colon delimited" ...

How about sub-field matches? I'm really interested in the buffering \( \) in sed ... how do you do the same thing in awk?
Command-Line Junkie
Pete Randall
Outstanding Contributor

Re: sed -> awk buffers ...

cat /tmp/somefile.txt | awk -F: '{ print $2, $1 }'

Pete
Hein van den Heuvel
Honored Contributor
Solution

Re: sed -> awk buffers ...


Do you know how many fields?
Then you could use:

awk -F: 'BEGIN {OFS=":"}{print $2,$1,$3,$4,$5}' /tmp/somefile.txt

for a variable field count, one suggestion is:

awk -F: '{print $2 ":" $1 substr ($0,match($0,":" $3))}' /tmp/somefile.txt


Hein.
Thierry Poels_1
Honored Contributor

Re: sed -> awk buffers ...

awk -F':' .....

regards,
Thierry
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
TwoProc
Honored Contributor

Re: sed -> awk buffers ...

Assuming there's only two fields...
cat /tmp/somefile.txt | cut -f 1,2 -d : | awk '{print $2," ",$1 }'
We are the people our parents warned us about --Jimmy Buffett
Hein van den Heuvel
Honored Contributor

Re: sed -> awk buffers ...

Ah... the obligatory perl alternatives....

like SED:

perl -pe 's/(.*):(.*):/$2:$1:/' x


Split words in an array, swap, join and have printed:

perl -pe '@words=split(":"); $x=$words[0];$words[0]=$words[1];$words[1]=$x; $_=join(":",@words)' x


Split, join in new order, and print.
The final @words is in scalar context and is the number of elements of words.

perl -pe '@words=split(":"); $_=join(":",@words[1,0,2..@words])' x


grins,
Hein.
Elmar P. Kolkman
Honored Contributor

Re: sed -> awk buffers ...

If you don't know the number of columns on each line, do it this way:

awk -F: '{printf "%s:%s",$2,$1;
for (i=3;i<=NF;i++)
printf ":%s",$i
printf "\n";
}' < /tmp/somefile.txt


You could also use split on $0 with a ":" seperator and swap the first two array entries, for instance. But the resulting could would be bigger as the above one, since I don't know of a join function in awk, like in perl.

Good luck,

Elmar
Every problem has at least one solution. Only some solutions are harder to find.
Hein van den Heuvel
Honored Contributor

Re: sed -> awk buffers ...

Argh, I hate to reply to myself, but I hate it even more to leave something that is wrong.

My perl example for the SED look alike is wrong.

I forgot about the greedy-ness of the .* match. So you can not say 'anything' because that may include colons. You have to say 'any number of non-colon chars'

perl -pe 's/([^:]*):([^:]*):/$2:$1:/' x

Hein.



A. Daniel King_1
Super Advisor

Re: sed -> awk buffers ...

I needed the delimiter to be a colon when done, too! The substr item is exactly what I was looking for, though I was hoping for something as elegant as the sed/perl buffer.

Thanks, all.
Command-Line Junkie