Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 6
    BTW, there's one major but non-obvious difference between the perl and sed versions. If given multiple input files, the perl version will print "Last Direction: " for the last line of each and every input file. The sed version will only print "Last Direction:" for the very last line of the entire input. You can get perl to behave like sed by using eof() instead of just eof....see perldoc -f eof for why. To get sed to behave like perl does, use the -s option - sed -s -e '.....' - this is a GNU extension to sed and may or may not be available in other versions. Commented Oct 12, 2021 at 10:56
  • If your listing all the ways in perl to add a newline when printing, theres also use v5.10; and then use the say function instead of print, which always appends a newline. Commented Oct 12, 2021 at 19:36
  • 1
    @user1937198 i wasn't, really. it was about print. That para was explaining why the print function works to do what the OP wants - it doesn't output a newline unless you tell it to, which makes it useful to print a single line in stages. In shell, you'd have to use printf with a format string without a newline (you can't rely on the many incompatible variations of echo that offer different ways of doing that). perl's say is a similar but different function...and btw, you can just use -E instead of -e to enable it, which is less typing than use v5.10; or -Mv5.10. Commented Oct 12, 2021 at 22:45
  • 1
    I would prefer skipping the print and using a separate substitution for the last line. (So that it also works when the last substitution doesn't contain the first):perl -pne 'if(eof) { s/^/Last Direction: /; } else { s/^/Direction: /; }' direction Commented Oct 13, 2021 at 2:53
  • 1
    @Garo TIMTOWTDI Commented Oct 13, 2021 at 3:29