-1

Can anyone tell me why sed fails to delete the lines containing 0 in the last column. I have used the following and nothing seems to work.

sed "/0/d"
sed "/ 0 /d"
sed "/\t0\n/d"
sed "/0\n/d"

Output:

 93000000       95       95       3328   0
 99000000       96       96       3206   0
 99000000       96       96       3210   0
 99000000       96       96       3215   0
 99000000       96       96       3224   0
 99000000       96       96       3230   0
 99000000       96       96       3236   0
 99000000       96       96       3268   0
 99000000       96       96       3292   0
 99000000       96       96       3459   0
 591000000      85       85       2      2 CDL (control)
 321000000      40       40       109    4.2 COZI-TV
 585000000      84       84       8      8 NJTV
 585000000      84       84       8134   10 WTBY
 543000000      77       77       710    10.1 WLNY-10
 585000000      84       84       62     12 N12WC
 657000000      101      101      1014   14 OPTCH
  • 2
    What is the complete command you are using? At the very least, the first command listed should remove all the lines from your input (since they all contain a 0 somewhere). – Greg Hewgill May 27 '14 at 2:21
  • sed '/0$/d' filename will remove all lines ending in a 0. – kwarrick May 27 '14 at 2:25
  • 2
    Yes, we're missing info here. When I copy/paste your "output" into a file, and then sed '/0/d' /tmp/file, I get no output (as expected). – Patrick May 27 '14 at 2:53
1

You cannot match newlines with \n (or anything else you've tried) in sed, since sed works per-line, thus newlines as you think of them aren't in scope of its working. What you're looking for is the $ special operator, matching End of Line:

sed -e '/0$/d'
1

If you don't want to also delete lines such as that ending in WLNY-10 then you need to be a bit more specific than even /0$/, for example

sed -r '/\s0$/d' file

or, if GNU sed is not available

sed '/[[:space:]]0$/d' file

or even

awk '$NF!=0' file
1

For deleting lines contain 0 in last column, try:

$ sed '/0$/d' file
591000000      85       85       2      2 CDL (control)
321000000      40       40       109    4.2 COZI-TV
585000000      84       84       8      8 NJTV
585000000      84       84       8134   10 WTBY
585000000      84       84       62     12 N12WC
657000000      101      101      1014   14 OPTCH

Your solutions fail because:

  • sed "/0/d": /0/ meaning match line contains 0 in any position, so it will delete all your input lines.

  • sed "/ 0 /d": / 0 / meaning match line contains 0 before and after a space. It will do nothing because your input does not match.

  • sed "/\t0\n/d" and sed "/0\n/d" also don't match because the regex you used wrong way.

Conclusion, for match 0 in the last column, you must use /0$/. You should read man sed or any articles abour regular expression for more details.

Updated

According to @steeldriver's answer, I update my answer to preserve lines contains number that end with 0:

$ sed '/ 0$/d' cat
591000000      85       85       2      2 CDL (control)
321000000      40       40       109    4.2 COZI-TV
585000000      84       84       8      8 NJTV
585000000      84       84       8134   10 WTBY
543000000      77       77       710    10.1 WLNY-10
585000000      84       84       62     12 N12WC
657000000      101      101      1014   14 OPTCH
0

Another awk command,

$ awk '$NF==0 {next;}1' file
591000000      85       85       2      2 CDL (control)
321000000      40       40       109    4.2 COZI-TV
585000000      84       84       8      8 NJTV
585000000      84       84       8134   10 WTBY
543000000      77       77       710    10.1 WLNY-10
585000000      84       84       62     12 N12WC
657000000      101      101      1014   14 OPTCH

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.