Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Blank line inserted in output file from "ls -l" command 3

Status
Not open for further replies.

pdtak

Technical User
Feb 25, 2008
63
US
I'm trying to capture the date/time portion of "ls -l" command as a file, but it puts a "blank line". How do I make it so that output file doesn't include a blank line?

Example 1 (saved as a variable works good):
var1=`ls -l /file1 | awk '{print $6,$7,$8}'`
echo $var1
May 24 23:06

Example 2 (blank line inserted):
ls -l /file1 | awk '{print $6,$7,$8}' > /tmp/out1
cat /tmp/out1
<blank line>
May 24 23:06

Any suggestion is appreciated.
 
Try this...
Code:
ls -l | [b]tail +2[/b] | awk '{print $6,$7,$8}' > /tmp/out1
cat /tmp/out1
 
That did the trick, adding "tail +2" to this command seems to have gotten rid of the leading blank line.
Thanks a bunch!
 
But do you see why? Try these two commands and see the difference in output.
Code:
ls -l
ls -l | tail +2
The "[tt]tail +2[/tt]" is just removing the first line of output from the "[tt]ls -l[/tt]".

This would work too...
Code:
ls -l | [b]sed '1d'[/b] | awk '{print $6,$7,$8}' > /tmp/out1
cat /tmp/out1
That uses "[tt]sed[/tt]" to delete the first line of "[tt]ls -l[/tt]" output.

Hope this helps.

 
Why not simply this ?
ls -l | awk 'NF>8{print $6,$7,$8}' > /tmp/out1

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

Why the UUOT and UUOS while AWK can do that too ?
Code:
ls -l /file1 | awk '[highlight]NR>1[/highlight]{print $6,$7,$8}' > /tmp/out1
pdtak said:
Example 1 (saved as a variable works good):
var1=`ls -l /file1 | awk '{print $6,$7,$8}'`
That only apparently works, actually the whitespaces get compromised due to word splitting, because you not quoted the string.


Feherke.
 
Thank you all for your inputs.
I gave you all a "STAR" for your awsomeness.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top