Reputation: 649
I have file with 6 columns seperated by space with data
cell in out f ty le
A A1 Z A1 com 2
B A1,B Z AB com 0,2
I want to remove the 0,
from 6th column getting output as
cell in out f ty le
A A1 Z A1 com 2
B A1,B Z AB com 2
I tried code
awk '{sub(/\0,.*$/,"",$6);print $1,$2,$3,$4,$5,$6}' file
But this did not worked.
Upvotes: 2
Views: 71
Reputation: 133458
Simple solution would be, as per your shown samples.
awk '{sub(/^0,/,"",$6)} 1' Input_file
In case you have few cases where comma could come in starting or ending of line of can come in between then you could please try following, written and tested with shown samples in GNU awk
.
awk '{gsub(/^,+0,+|,+0,+$/,"",$6);gsub(/,0,/,",",$6)} 1' Input_file
problem in OP's attempt: OP using .*
after 0,
which is very greedy and will match everything of 6th field hence its substituting everything in 6th field with NULL.
Fixes in OP's attempts: I have added logic to substitute starting 0,
and ending ,0
with NULL in 6th field. Then to handle in between zero substituting from ,0,
to ,
, to make solution generic here.
NOTE: In case your Input_file is TAB separated then add BEGIN{FS=OFS="\t"}
.
Upvotes: 3
Reputation: 203229
Since this is a simple substitution on an individual string, I'd just use sed:
$ sed 's/0,\([^[:space:]]*\)$/\1/' file
cell in out f ty le
A A1 Z A1 com 2
B A1,B Z AB com 2
otherwise with an awk that has gensub() (e.g. GNU awk):
$ awk '{print gensub(/0,([^[:space:]]*)$/,"\\1",1)}' file
cell in out f ty le
A A1 Z A1 com 2
B A1,B Z AB com 2
or with any awk:
$ awk 'match($0,/0,[^[:space:]]*$/){$0=substr($0,1,RSTART-1) substr($0,RSTART+2)} 1' file
cell in out f ty le
A A1 Z A1 com 2
B A1,B Z AB com 2
Note that all of the above work with and simply retain whatever spacing you already have in your input.
Upvotes: 3