Remove lines ending with a certain character

I have a file of a content like this:

abc_bla -def 800
abc_bla -def 802
abc_bla -def 804
abc_bla -def 806
abc_bla -def 808
abc_bla -def 810
abc_bla -def 812
abc_bla -def 814
...
abc_bla -def 898
abc_bla -def 900
abc_bla -def 902
abc_bla -def 904
...
abc_bla -def 990
abc_bla -def 992
abc_bla -def 994
abc_bla -def 996
abc_bla -def 998
abc_bla -def 1000
abc_bla -def 1002
abc_bla -def 1004
abc_bla -def 1006
...
...
abc_bla -def 1800

I would like to delete all lines in a file that end with a 0 (zero).

I tried this:

sed '/0/d' file

but this deletes all the lines containing a zero, and I want to delete only those *ending* with a zero.
Then I tried

grep '0$' file

but that gives me as output only the last line.

How can I select that it chooses the pattern when it is *only* at the end of the line?
So I want my output to be all the lines not ending with a 0.

you were pretty close. Try this:

sed '/0$/d' file

Welcome to the forum.

You weren't too far off. Why didn't you apply what you had in your grep approach to the sed command, i.e. anchor the regex at line end? Like

sed '/0$/d' file

Or, add grep 's ( man grep )

option ?

How come your grep "gives me as output only the last line." ?

Hi maya3,
One would expect that the command:

grep '0$' file

would print out all lines with a 0 at the end. Since that is not happening, we have to wonder what actually follows the 0 in the lines that are not being printed. Might this be a DOS format text file that has <carriage-return><newline> character pairs at the end of most lines instead of a UNIX format text file that just has a <newline> character at the end of each line? Please show us the output from the command:

head file | od -bc

where file is the name of the sample file you showed us in post #1 in this thread.

Note that grep '0$' file will give you the lines you want to delete (not the lines you want to keep) if file really does have lines with a trailing 0 character. To get the lines you want to keep, you probably want to use:

grep -v '0$' file > new_file

after we clean up whatever is keeping the lines with a 0 at the end of a line from being recognized.

Thank you for the answers.

It seemed that I had \r\n at the end of the line and I replaced it with \n but the outputs are still the same.
This is what I get when I write

head file | od -bc

(the lines are actually longer so this is adapted to my sample file)

0000000 147 156 141 141 143 137 144 141 147 137 163 165 142 155 151 164
          a   b   c   _   b   l   a   _   s   u   b   m   i   t
0000020 040 055 147 167 145 145 153 040 070 060 062 040 012 147 156 141
              -   d   e   f       8   0   0      \n   a   b   c
0000040 141 143 137 144 141 147 137 163 165 142 155 151 164 040 055 147
          _   b   l   a  etc.

That output doesn't seem to originally belong to the command you issued, and it certainly doesn't match the sample in post #1. Please apply utmost care when posting code and data!

Still from the octal dump I infer you don't have digits (incl. zeroes) at line end, but digits plus spaces. Try

sed '/0 *$/d' file

or

grep -v '0 *$' file
1 Like

Let us be clear here. The above display is not sample output from any file. The above display is not adapted output from any file. The above output is contrived output that is intended to confuse anyone who might want to help you solve your problem.

If RudiC's guess (like mine), is that the octal byte values show real data and the character values have been concocted to confuse us, then the code RudiC suggested may solve your problem. If it doesn't please show us the complete, real output from the head piped through od command I gave you so we have a chance of figuring out what is really going on.

The fake data that you did show us does not include any line that has a 0 at the end of a line (even if we ignore trailing <space>s. The only complete line shown in the seemingly real od output you concocted ends with a decimal digit 2 followed by a <space>. Guessing that all other lines in your input file (except the last one) also have trailing <space>(s) from a sample of a single concocted input line is a seemingly good guess, but it certainly isn't anything that we can rely on.

2 Likes

Thank you, this works now. I think I might have accidentally added the whitespace later on while I was trying to find a solution to remove 0 with text editor's options. It works now with your command and I could see how to remove whitespace too!