0

I have a file containing numeric data. Each line has a varying number of columns. In each line, I want to delete the columns containing 0.

Below is a sample Input

25 60 0
29 0 10
23 0
26 43 49
1 56 7 0
21 0 64 3

Target Output

25 60
29 10
23
26 43 49
1 56 7
21 64 3
0

Try this,

sed 's/ 0//g;s/^0 //g' file

25 60
29 10
23
26 43 49
1 56 7
21 64 3

it just removes the pattern 0. if the output is fine, use -i option to do inline edit.

  • What if 0 is in the first column? – Benjamin W. Apr 16 at 17:50
1

Using perl:

$ perl -alne 'print join " ", grep { $_ != 0 } @F' file
25 60
29 10
23
26 43 49
1 56 7
21 64 3

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.