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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with an awk output

Status
Not open for further replies.

chrchcol

Technical User
Jul 23, 2006
7
US
I am using z=`ls -la "$f" | awk '{print $5} to pipe the filenames from one variable into another variable for later output.

However the filenames print left to right, I really need them to print right to left the way they do in the ls -la command

chris
 
Well, the output of the ls -la command for size would be the larger sizes would be to the left and on the right the smaller sizes line up. Such as

123456
123
567
33
123456

That would be correct. WHen you use ls -la | awk '{print $5}'

The output looks more like

123456
22
234
235
123457


Chris
 
Hi

So you want right aligned numbers. Not a nice solution :
Code:
ls -la | awk '{s[NR]=$5;if(l<length($5))l=length($5)}END{for(i=1;i in s;i++)printf"%"l"s\n",s[i]}'

Feherke.
 
Hi chrchcol,

If you are using Korn Shell (ksh), you could try:

typeset -R8 z=""
z=`ls -la "$f" | awk '{print $5}
echo "$z"


I hope that helps.

Mike
 
OK now I am totally confused, the code you gave me worked perfect on the command line. It was Right aligned.

<CODE>ls -la | awk '{s[NR]=$5;if(l<length($5))l=length($5)}END{for(i=1;i in s;i++)printf"%"l"s\n",s}'</CODE>

However when I tried to add it into my script and added this

<CODE>
for f in $(<uploads); do x=`ls -la "$f"| awk '{s[NR]=$5;if(l<length($5))l=length($5)}END{for(i=1;i in s;i++)printf"%"l"s\n",s}'`
echo "$x"

Then I run it and it lists the size but its left aligned.

Chris
 
As previously suggested:
for f in $(<uploads); do
[!]typeset -R8[/!] x=$(ls -la "$f" | awk '{print $5}')
echo "x='$x'"
done


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
bash claims to be (at least) Posix compliant, so I guess yes perhaps. ;-)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I hope I wasn't being rude, I was just letting you know I could not get the Typeset command to work with those switches. I do see the command for typeset and have been trying to play with it.

Chris
 
I found a command that worked almost completley
<c>z=`ls -la "$f" | awk '{ printf("%-15s\t%10d\n", $9, $5)}'`</c>

This command works perfect. The onlyput I need is $5 though. When I remove $9 it does not work. Is there something I am missing?

Chris
 
And this ?
for f in $(<uploads); do
x=$(ls -la "$f" | awk '{printf "%8d",$5}')
echo "x='$x'"
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top