Skip to content
Advertisement

How to remove some string with awk/sed or another command in linux?

I want to ask, how I can remove some string with command in linux?

cat /data/filename.txt 
A|2|3|4|5|6|7|8|9|10|0030|12|05/09/17 
B|2|3|4|5|6|7|8|9|10|00045|12|05/09/17
C|2|3|4|5|6|7|8|9|10|061|12|05/09/17

and the output:

cat /data/filename.txt 
A|2|3|4|5|6|7|8|9|10|30|12|05/09/17
B|2|3|4|5|6|7|8|9|10|45|12|05/09/17
C|2|3|4|5|6|7|8|9|10|61|12|05/09/17

Advertisement

Answer

try:

awk -F"|" '{sub(/^0+/,"",$11)} 1' OFS="|"   Input_file

Making field separator as | then substituting the starting zeros with NULL of 111th field as per your requirements, mentioning 1 will print the newly edited line(if any zeros were replaced with NULL), setting Output field separator as | and mentioning the Input_file then.

EDIT: Adding 2nd solution for removing -000 into 11th field as follows.

awk -F"|" '{sub(/^0+/,"",$11);sub(/-0+/,"-",$11)} 1' OFS="|" Input_file

Here I am making | as field separator then substituting the starting zeros in 11th field with NULL and then substituting the -0+ means from -0 till all the zeros to NULL too here and then 1 will print the lines. Setting Output field separator as | too.

Advertisement