2

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
4

Another solution with awk:

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

3

I used awk:

awk -F"[ ,]" '{print $1","$2","$4 }' yourfile 

The result:

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

In awk:

awk '{FS="[, ]"; OFS=","; print $1, $2, $4}' filename

In sed:

sed 's/^\(.*,\)[0-9]* \([^,]*\)$/\1\2/' filename
0

According to data shown it can be the following sed

sed -r 's/,[0-9 ]+ /,/' file.csv

In case of data variation regex should be change for more strong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.