LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2012, 08:45 AM   #1
hectorharvey
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Rep: Reputation: Disabled
Read a line, not a word


I am trying to read the last 5 lines of a log in reverse order. The code below writes the last 5 to a temporary file in reverse order. But the read gets the first word (space delimited?), and not the first line.
How can I get the entire line?

Code:
tail -n5  /../wrapper.log | tac > tempfile;
while read wLine
do
  printf $wLine;
done < tempfile;
 
Old 04-19-2012, 09:10 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well.. two problems here. First, the syntax of the printf command is not correct: it should be
Code:
printf FORMAT [ARGUMENT]...
Second, you have to use double quotes to prevent word splitting and make printf see the value of the wLine variable as a single string, despite the presence of spaces or TAB characters:
Code:
while read wLine
do
  printf "%s\n" "$wLine"
done < tempfile
where the printf statement with this specific format (a string followed by newline) is equivalent to echo.
 
Old 04-19-2012, 09:21 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by hectorharvey View Post
I am trying to read the last 5 lines of a log in reverse order.
Code:
tail -5 $InFile | tac
Daniel B. Martin
 
Old 04-19-2012, 10:09 AM   #4
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Quote:
Originally Posted by colucix View Post
Well.. two problems here. First, the syntax of the printf command is not correct: it should be
Code:
printf FORMAT [ARGUMENT]...
Second, you have to use double quotes to prevent word splitting and make printf see the value of the wLine variable as a single string, despite the presence of spaces or TAB characters:
Code:
while read wLine
do
  printf "%s\n" "$wLine"
done < tempfile
where the printf statement with this specific format (a string followed by newline) is equivalent to echo.
I do agree with that said.

But if you omit the Format string the first argument is used a format string, which will be expanded to it self, if it does not contain any format specification.

Code:
a="hello a string with spaces"
printf $a
printf '\n'

printf "$a\n"
Of course this is not proper programming, as this construct is misleading.
Allbeit that the desired result is printed.
 
Old 04-19-2012, 12:01 PM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by uhelp View Post
I do agree with that said.

But if you omit the Format string the first argument is used a format string, which will be expanded to it self, if it does not contain any format specification.

Code:
a="hello a string with spaces"
printf $a
printf '\n'

printf "$a\n"
Of course this is not proper programming, as this construct is misleading.
Allbeit that the desired result is printed.
Actually, the usage of printf is indeed (as posted by colucix)

Code:
printf FORMAT [ARGUMENT]...
This is actually very similar to usage of printf() function in C.
Your
Code:
printf "$a\n"
is correct and there's nothing wrong with that, since the FORMAT string can contain string literals, just as in C you would write

Code:
printf("Hello World\n");
instead of

Code:
printf("%s %s\n", "Hello", "World");
Following, however,

Code:
a="hello a string with spaces"
printf $a
will not print the desired output, because

printf $a

will be substituted by

printf hello a string with spaces

and 6 arguments will be passed to printf. printf will interpret the first argument as a format string, and because it does not contain any references (such as %s or %f), printf will have no reason to use any of the other arguments. Therefore, only "hello" will be printed, the rest of the arguments will be discarded (unlike echo which will print the entire string). In your example, the double quotes are required around $a:

Code:
printf "$a"

Last edited by millgates; 04-19-2012 at 12:04 PM.
 
Old 04-19-2012, 01:35 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
There is one difference between Bash printf and C printf() function, though:
Code:
printf '<%s>\n' 'First' 'Second' 'Third'
will output
Code:
<First>
<Second>
<Third>
because printf command will apply the FORMAT to the parameters, outputting the result, until there is no more output to print. Except, of course, if FORMAT does not consume any parameters.

Here is the relevant note from the Bash manual (bash-builtins man page), edited for clarity:
Quote:
Code:
printf [ -v VAR ] FORMAT [ ARGUMENTS... ]
Write the formatted ARGUMENTS to the standard output under the control of the FORMAT. The FORMAT is a character string which contains three types of objects: plain characters, which are simply copied to standard output, character escape sequences, which are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive argument. In addition to the standard printf(1) formats, ‘%b’ causes printf to expand backslash escape sequences in the corresponding argument, (except that ‘\c’ terminates output, backslashes in ‘\'’, ‘\"’, and ‘\?’ are not removed, and octal escapes beginning with ‘\0’ may contain up to four digits), and ‘%q’ causes printf to output the corresponding argument in a format that can be reused as shell input.

The -v option causes the output to be assigned to the variable VAR rather than being printed to the standard output.

The FORMAT is reused as necessary to consume all of the ARGUMENTS. If the FORMAT requires more ARGUMENTS than are supplied, the extra format specifications behave as if a zero value or null string, as appropriate, had been supplied. The return value is zero on success, non-zero on failure.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file word by word part 2 justina Programming 7 01-25-2011 01:19 PM
[SOLVED] bash shell script read file word by word. justina Programming 15 01-22-2011 10:12 AM
How can i read two files word by word at a time using any loop by shell script? vaibhavs17 Programming 16 03-19-2010 03:48 AM
print second word in 1st line along with 5th word in all the lines after the first bangaram Programming 5 08-31-2009 03:42 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration