I'd like to be able to turn the 3 lines below into 1 command string.
Can anyone help?
printf "Result: "
cat $ren_data | tail -1
printf "\n"
Thanks!
printf "Result: %s\n" "`tail -1 $ren_data`"
--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
>> I'd like to be able to turn the 3 lines below into 1 command string.
>> Can anyone help?
>>
>> printf "Result: "
>> cat $ren_data | tail -1
>> printf "\n"
> printf "Result: %s\n" "`tail -1 $ren_data`"
Assuming the output of cat in the original ends with a newline (if it
doesn't, tail might misbehave), this will produce one fewer newline
than the original, because one will be discarded by the command
substitution.
It's also worth correcting a couple of other mistakes in the
original besides the UUOC: $ren_data should be quoted[1], and
"tail -1" is obsolescent syntax that UNIX systems have not
been required to support since 2001. It's easier to add the
extra quotes if $(...) is used instead of `...`:
printf "Result: %s\n\n" "$(tail -n 1 "$ren_data")"
[1] If $ren_data was intentionally not quoted because it might
expand to more than one file, it would be more efficient to
just run tail on the last file rather than concatenating them
all:
printf "Result: %s\n\n" \
"$(for f in $ren_data; do :; done; tail -n 1 "$f")"
--
Geoff Clare <net...@gclare.org.uk>
printf "Result: %s\n\n" "$(tail -n 1 -- "$ren_data")"
in case $ren_data starts with a -, or better:
printf "Result: %s\n\n" "$(tail -n 1 < "$ren_data")"
in case $ren_data may be a file called "-" (which is otherwise
interpreted by tail as meaning "standard input")
Also:
sed -n '$s/.*/Result: &\
/p' < "$ren_data"
Which is not guaranteed to work either if the last line of the
file is not terminated.
--
Stᅵphane
Thanks!!
Yup. I thought I had caught all the problems, but missed that one.
> or better:
>
> printf "Result: %s\n\n" "$(tail -n 1 < "$ren_data")"
>
> in case $ren_data may be a file called "-" (which is otherwise
> interpreted by tail as meaning "standard input")
s/is otherwise/might otherwise be/
POSIX/SUS allows tail to treat a "-" operand as meaning stdin, but
doesn't require it. In any case you are right that portable
applications should guard against it.
--
Geoff Clare <net...@gclare.org.uk>