Modify Specific Column with Sed or Awk – Text Processing Guide

awksedtext processing

I have a CSV file that looks like this:

qw12er,foo,0 bn5mgh
rt8yp,foo,10 gh78jk
bn852mv,foo,852 78ghjkh
tgbr,foo,10 ujmyhn
wsx2d,foo,0000 ui52ohn
tgbr,foo,7418529 ujmyhn
ikl896o,foo,22 wsxdc52

I want to modify the 3rd column and remove all numbers and space from the beginning of the 3rd column.

Then output would be as follows:

qw12er,foo,bn5mgh
rt8yp,foo,gh78jk
bn852mv,foo,78ghjkh
tgbr,foo,ujmyhn
wsx2d,foo,ui52ohn
tgbr,foo,ujmyhn
ikl896o,foo,wsxdc52

Best Answer

Another solution with awk using sub:

awk -F, 'sub("^[0-9]+\\s","",$3)' OFS=, file 

Output:

qw12er,foo,bn5mgh
rt8yp,foo,gh78jk
bn852mv,foo,78ghjkh
tgbr,foo,ujmyhn
wsx2d,foo,ui52ohn
tgbr,foo,ujmyhn
ikl896o,foo,wsxdc52

Explanation:

  • -F,: set the comma as input field separator

  • OFS=,: set the comma as output filed separator (a space by default)

  • sub("^[0-9]+\\s","",$3): erase numbers followed by a space at the beginning of the string $3 and print the current line (because "print" is the default action in awk)

In this way you can edit the desired column and print all the others (that in general may be many).

Related Question