How to use Ruby's command line in-place-edit mode to remove lines from a text file

Guss

I've been a long time perl user, and in perl you can easily remove lines from text files like this:

perl -pi -e 'undef $_ if m/some-condition/' file.txt

I'm trying to move my scripting to Ruby, which also does in-place-edit, so I wrote this:

ruby -pi -e '$_ = nil if $_ =~ /some-condition/' file.text

but that instead nullified the entire file. I then tried

ruby -pi -e 'nil if $_ =~ /some-condition/' file.text

but that didn't do anything.

Any ideas?

Amadan
ruby -pi -e '$_ = nil if $_ =~ /some-condition/' file.text

should be correct. If it doesn't work, the problem is in some-condition. To demonstrate,

echo -e "a\nb\nc" > x ; ruby -pi -e '$_ = nil if $_ =~ /b/' x ; cat x

prints

a
c

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove a place holder in a text file with text from another file when multiple lines are present?

From Dev

How to remove specific lines from a text file?

From Dev

How to remove all lines from a text file starting at first empty line?

From Dev

How to Merge Lines from 2 Text File without creating a Line Break in Batch Command

From Dev

How to remove a line from a text file?

From Dev

How to edit a file from the command line Mac OS X?

From Dev

How can I edit a ruby file (Rakefile) in place from within ruby?

From Dev

How can I place an line text from a file into an textbox?

From Dev

How to match and remove whole lines from a file in ruby?

From Dev

Is there a way to enable command prompts 'quick edit' mode from the command line?

From Dev

How to remove blank lines from text file using powershell

From Dev

Scala: How to remove blank lines when reading text from file

From Dev

How to remove particular words from lines of a text file?

From Dev

How to remove particular words from lines of a text file?

From Dev

How can I remove all lines not containing `@` from a text file?

From Dev

How to remove blank lines from text file using powershell

From Dev

How to remove duplicate lines from a large text file efficiently?

From Dev

How can I remove unnecessary text from a command line output?

From Dev

How to edit a config file using the command line?

From Dev

How to remove extra comma from line in text file in batch file?

From Dev

How to remove extra comma from line in text file in batch file?

From Dev

How can I run a ruby file in command line from Java?

From Dev

What's the quickest way to add text to a file from the command line?

From Dev

How to remove nodes from XML file as command line?

From Dev

How to remove characters from file names using command line?

From Dev

How to retrieve a random line from text file, then remove the line from the text file in PHP

From Dev

How to edit pdf metadata from command line?

From Dev

How to edit pdf metadata from command line?

From Dev

How do I edit previous lines in a multiple line command in Bash?

Related Related

  1. 1

    How to remove a place holder in a text file with text from another file when multiple lines are present?

  2. 2

    How to remove specific lines from a text file?

  3. 3

    How to remove all lines from a text file starting at first empty line?

  4. 4

    How to Merge Lines from 2 Text File without creating a Line Break in Batch Command

  5. 5

    How to remove a line from a text file?

  6. 6

    How to edit a file from the command line Mac OS X?

  7. 7

    How can I edit a ruby file (Rakefile) in place from within ruby?

  8. 8

    How can I place an line text from a file into an textbox?

  9. 9

    How to match and remove whole lines from a file in ruby?

  10. 10

    Is there a way to enable command prompts 'quick edit' mode from the command line?

  11. 11

    How to remove blank lines from text file using powershell

  12. 12

    Scala: How to remove blank lines when reading text from file

  13. 13

    How to remove particular words from lines of a text file?

  14. 14

    How to remove particular words from lines of a text file?

  15. 15

    How can I remove all lines not containing `@` from a text file?

  16. 16

    How to remove blank lines from text file using powershell

  17. 17

    How to remove duplicate lines from a large text file efficiently?

  18. 18

    How can I remove unnecessary text from a command line output?

  19. 19

    How to edit a config file using the command line?

  20. 20

    How to remove extra comma from line in text file in batch file?

  21. 21

    How to remove extra comma from line in text file in batch file?

  22. 22

    How can I run a ruby file in command line from Java?

  23. 23

    What's the quickest way to add text to a file from the command line?

  24. 24

    How to remove nodes from XML file as command line?

  25. 25

    How to remove characters from file names using command line?

  26. 26

    How to retrieve a random line from text file, then remove the line from the text file in PHP

  27. 27

    How to edit pdf metadata from command line?

  28. 28

    How to edit pdf metadata from command line?

  29. 29

    How do I edit previous lines in a multiple line command in Bash?

HotTag

Archive