Skip to content
Advertisement

Change some field separators in awk

I have a input file 1.txt

joshwin_xc8@yahoo.com:1802752:2222:
ihearttofurkey@yahoo.com:1802756:111113
www.rothmany@mail.com:xxmyaduh:13@;:3A

and I want an output file: out.txt

joshwin_xc8@yahoo.com||o||1802752||o||2222:
ihearttofurkey@yahoo.com||o||1802756||o||111113
www.rothmany@mail.com||o||xxmyaduh||o||13@;:3A

I want to replace the first two ‘:’ in 1.txt with ‘||o||’, but with the script I am using

awk -F: '{print $1,$2,$3}' OFS="||o||" 3.txt

But it is not giving the expected output. Any help would be highly appreciated.

Advertisement

Answer

Following sed may also help you in same.

sed 's/:/||o||/;s/:/||o||/'  Input_file

Explanation: Simply substituting 1st occurrence of colon with ||o|| and then 2nd occurrence of colon now becomes 1st occurrence of colon now and substituting that colon with ||o|| as per OP’s requirement.

Advertisement