Timeline for Is there a method to indicate the last iteration in a loop over lines of an input file?
Current License: CC BY-SA 4.0
10 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 13, 2021 at 3:29 | comment | added | cas | @Garo TIMTOWTDI | |
| Oct 13, 2021 at 2:53 | comment | added | Garo |
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
|
|
| Oct 12, 2021 at 22:45 | comment | added | cas |
@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.
|
|
| Oct 12, 2021 at 19:36 | comment | added | user1937198 |
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.
|
|
| Oct 12, 2021 at 10:56 | comment | added | cas |
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.
|
|
| Oct 12, 2021 at 10:45 | history | edited | cas | CC BY-SA 4.0 |
added 43 characters in body
|
| Oct 12, 2021 at 10:36 | history | edited | cas | CC BY-SA 4.0 |
edited body
|
| Oct 12, 2021 at 10:30 | history | edited | cas | CC BY-SA 4.0 |
added 225 characters in body
|
| Oct 12, 2021 at 10:26 | vote | accept | Sollosa | ||
| Oct 12, 2021 at 10:24 | history | answered | cas | CC BY-SA 4.0 |