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

awk print format question 2

Status
Not open for further replies.

pdtak

Technical User
Feb 25, 2008
63
US
I'm trying to line up (align) output from "ls -l". I have the following awk command in a script that writes to a file (INFILE has bunch of file names):
#!/bin/sh
for LINE in `cat ${INFILE}`
do
echo `ls -l $LINE | awk '{printf ("%s %s:%s \t %s\n",$1, $3, $4, $9) }'` >> ${FORMATTED}
done

The output come out like the following:
-rwxr--r-- one:groupone /home/userone/fileone
-rwxr---w- three:groupthree /home/userthree/filethree
-rwxr----x twenty:grouptwenty /home/usertwenty/filetwenty

I want them to look like:
-rwxr--r-- one:groupone /home/userone/fileone
-rwxr---w- three:groupthree /home/userthree/filethree
-rwxr----x twenty:grouptwenty /home/usertwenty/filetwenty

Any ideas on how I can do this?

Thanks!

 
What about something like this ?
printf ("%s %7s:%-12s \t %s\n",$1, $3, $4, $9)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Sorry, I substituted your printf command, but it didn't work... same output display.
I think because I'm writing to an output file it's not taking \t in the printf command... any other ideas?
Thanks PHV.
 
Hi

That is probably because the UUOE ( Useless Use Of Echo ) :
Code:
#!/bin/sh
for LINE in `cat ${INFILE}`
do
  ls -l $LINE | awk '{printf ("%s  %s:%s \t %s\n",$1, $3, $4, $9) }' >> ${FORMATTED}
done
Personally I would prefer it abit differently :
Code:
#!/bin/sh
while read LINE; do
  ls -l "$LINE" | awk '{printf ("%s  %s:%s \t %s\n",$1, $3, $4, $9) }'
done < "${INFILE}" > "${FORMATTED}"

Feherke.
 
And, in a ksh-like shell, an one-liner:
Code:
ls -l $(<$INLINE) | awk '{printf "%s %7s:%-13s\t%s\n",$1,$3,$4,$9}' >$FORMATTED

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You were right feherke, it worked when I took out the echo command.
PHV, your simplied version worked too.
You guys really know your stuff!
Thanks a lot!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top