Peter Wildemann
Peter Wildemann

Reputation: 495

Print to the console using in-place command-line editing

When using ruby -pi.xxx -e '$stdout.print $_' ./some_file.txt ruby is going to write into the file some_file.txt and I will end up having every text line twice in my file.

How can I redirect the input to end up in the console and not in my file?

Upvotes: 1

Views: 249

Answers (1)

Patrick Oscity
Patrick Oscity

Reputation: 54684

From the Ruby man page:

-i extension

    Specifies in-place-edit mode. The extension, if specified, is added to old file name
    to make a backup copy.

In-place edit mode means that the output of the program is written to the original file instead of $stdout. To change this back to printing to stdout, simply leave out the -i flag, i.e.

ruby -pe '$stdout.print $_' ./some_file.txt

Some side notes:

  • $stdout.print $_ does exactly the same as print $_
  • I hope your example is not actual code, because it doesn't make very clear what it is supposed to achieve. To double the lines in a file I would rather write either ruby -ne 'puts $_ * 2' ./some_file.txt or ruby -pe '$_ *= 2' ./some_file.txt although I think the first one is clearer.

Upvotes: 1

Related Questions