bash
can't detect the end of a file (without trying to read the next line and failing), but perl can with its eof
function:
$ perl -n -e 'print "Last " if eof; print "Direction: $_"' direction
Direction: east
Direction: north
Direction: south
Direction: west
Last Direction: south-west
note: unlike echo
in bash, the print
statement in perl doesn't print a newline unless you either 1. explicitly tell it to by including \n
in the string you're printing, or 2. are using perl's -l
command-line option, or 3. if the string already contains a newline....as is the case with $_
- which is why you often need to chomp()
it to get rid of the newline.
BTW, in perl, $_
is the current input line. Or the default iterator (often called "the current thingy" probably because "dollarunderscore" is a bit of a mouthful) in any loop that doesn't specify an actual variable name. Many perl functions and operators use $_
as their default/implicit argument if one isn't provided. See man perlvar
and search for $_
.
sed
can too - the $
address matches the last line of a file:
$ sed -e 's/^/Direction: /; $s/^/Last /' direction
Direction: east
Direction: north
Direction: south
Direction: west
Last Direction: south-west
The order of the sed
rules is important. My first attempt did it the wrong way around (and printed "Direction: Last south-west"). This sed script always adds "Direction: " to the beginning of each line. On the last line ($
) it adds "Last " to the beginning of the line already modified by the previous statement.