Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printf

0 views
Skip to first unread message

The Magnet

unread,
Aug 24, 2009, 4:23:14 PM8/24/09
to

Hi,

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!

Barry Margolin

unread,
Aug 24, 2009, 9:09:54 PM8/24/09
to
In article
<b1e2d381-8a51-43d2...@t13g2000yqt.googlegroups.com>,
The Magnet <a...@unsu.com> wrote:

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 ***

Geoff Clare

unread,
Aug 25, 2009, 8:57:44 AM8/25/09
to
Barry Margolin wrote:

>> 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>

Stephane CHAZELAS

unread,
Aug 25, 2009, 3:08:42 PM8/25/09
to
2009-08-25, 13:57(+01), Geoff Clare:
[...]

> printf "Result: %s\n\n" "$(tail -n 1 "$ren_data")"
[...]

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

The Magnet

unread,
Aug 25, 2009, 10:34:04 PM8/25/09
to
On Aug 24, 8:09 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article
> <b1e2d381-8a51-43d2-aa79-780c05aff...@t13g2000yqt.googlegroups.com>,


Thanks!!

Geoff Clare

unread,
Aug 26, 2009, 8:51:02 AM8/26/09
to
Stephane CHAZELAS wrote:
> 2009-08-25, 13:57(+01), Geoff Clare:
> [...]
>> printf "Result: %s\n\n" "$(tail -n 1 "$ren_data")"
> [...]
>
> printf "Result: %s\n\n" "$(tail -n 1 -- "$ren_data")"
>
> in case $ren_data starts with a -,

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>

0 new messages