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

2 Answers 2

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

1
  • What if 0 is in the first column? Commented Apr 16, 2019 at 17:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.