Shell - How to remove a particular string after nt

2019-09-16 00:02发布

I have a file which has pipe delimited data and i want to remove this string "EES" after 17th occurrence of pipe and write the data back to same file again

Below is the input i want to pass

AMC|FRS|M|123456|01/30/1992|QWERT||WERTYU|OK|USA|74000|1234567|QQ|34567142|11/01/2011|12/31/9999|Y|V3EES|13

below is the output i'm expecting

AMC|FRS|M|123456|01/30/1992|QWERT||WERTYU|OK|USA|74000|1234567|QQ|34567142|11/01/2011|12/31/9999|Y|V3|13

Help is appreciated, Thanks in advance!!!

2条回答
beautiful°
2楼-- · 2019-09-16 00:28

Using awk

awk  'BEGIN {FS=OFS="|"} {sub("EES","",$18); print}'  file 

Using grep & awk

egrep -o "[A-Z0-9a-z]*" file | awk '{if(NR==18){sub("EES","")}printf("%s|",$0)}'
查看更多
叛逆
3楼-- · 2019-09-16 00:34

Following awk should help you in same.

awk -F"|" '{sub("EES","",$18)} 1' OFS="|"   Input_file > temp_file && mv temp_file Input_file

Explanation: Making here field separator as |(pipe) as per OP's Input_file then using awk's substitute utility sub where I am mentioning that replace string EES with NULL in column 18. Now mentioning 1 which will print the lines(edited/non-edited 18th column). Setting OFS(Output field separator as |(pipe)) here to get | in output, then mentioning the Input_file.

查看更多
登录 后发表回答