3

I have a file:

1 2 5 6 8 0 0 0 0
1 2 5 6 8 0 0 0 0
1 2 5 6 8 0 0 0 0
1 2 5 6 8 0 0 0 0

I want delete all zeros:

1 2 5 6 8 
1 2 5 6 8 
1 2 5 6 8 
1 2 5 6 8

I tried tr -d [:0:]. Unfortunately, it is not supposed to work in this way.

I believe sed and perl would do the job, but I do not know exactly how.

I searched on google, if you believe me, the results are mixed and relevant, but I am not able to find the right one in the first page.

  • 2
    Can you elaborate with a real example please? This looks suspiciously like a homework question, and those don't fit well with this site. – Andrew B Feb 25 '16 at 15:53
  • This is a real example from my Kaggle competition. kaggle.com/c/yelp-restaurant-photo-classification – John Hass Feb 25 '16 at 18:20
  • I agree with your last point. I found Unix & Linux is better. – John Hass Feb 25 '16 at 18:23
  • "I want to delete all zeroes" -- you mean all fields with value zero, right? If you have the number "100", you don't want to have just "1" as a result, right? – glenn jackman Feb 27 '16 at 20:16
2

tr is fine for this job:

tr -d 0 <file

[:0:] is invalid because you need to use a character class between [: and :].

4
[8]root@lab711:> cat file
1 2 5 6 8 0 0 0 0
1 2 5 6 8 0 0 0 0
1 2 5 6 8 0 0 0 0
1 2 5 6 8 0 0 0 0
[8]root@lab711:> sed 's/0//g' file
1 2 5 6 8
1 2 5 6 8
1 2 5 6 8
1 2 5 6 8
  • 1
    This is going to leave all the spaces between the 0s in the output – Michael Mrozek Feb 26 '16 at 15:47
2

In order to you remove all zero's in the file the command to use is sed 's/0//g' filename the s stands for substitute. The /0 stands for all 0's in the file. the // stands for replace with nothing. The g stands for globally

  • 2
    It's important to note that the / character is actually just the delimiter, nothing special really. While hard to read, sed 's1011g' filename is just as valid. – Zak Henry Feb 25 '16 at 18:39
  • 1
    oh wow I did not know that thanks for the info – Dustin Feb 25 '16 at 18:43
  • @zak, funny example. How about sed 's0\000g' – glenn jackman Feb 27 '16 at 20:17
2

Try

perl -pe 's/\s+0\b//g' <filename>
  • -p loops on files line and print $_
  • -e execute
  • /gGlobal (all occurences)
  • Regex s/\s+0\b// is deleting series of at least one space, followed by character 0, followed by end of word.

Or, in place replacement in file,

sed -ibbak -e 's/\s\+0\b//g' <filename>
  • -i in place
  • -b.bak make back-up (extention is .bac)
  • Here the regex + character must be escaped.
  • \s in regex is one space
  • \b in regex is End of word

Hopes it makes a kick start!

  • wrong. That leaves you with 800 as the last field. You are too greedy with your whitespace, leaving yourself with two zeros with no preceding whitespace. You want s/\s+0\b//g – glenn jackman Feb 27 '16 at 20:14
  • Hokinaru:~$ echo '5 6 800' | sed 's/\s\+0\s\+//g' 5 6 800 As expected... But end of word is indeed a good idea. – MUY Belgium Feb 29 '16 at 9:37
  • Try using this input: 5 6 8 0 0 0 0 – glenn jackman Feb 29 '16 at 11:03
  • indeed... That makes the regex more complex, but that is mandatory. – MUY Belgium Feb 29 '16 at 12:59

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.