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!

re: AWK question 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hey,


I am trying to do

who -u | nawk '{print $1, $6, $7}' >/tmp/myfile


I would like the output to be neatly formatted by spaces.My output now is all squished together. Can someone help please.


 
Try:
who -u|nawk '{print $1, " ", $6, " ", $7}'>/tmp/myfile

Bill.
 
To get a well aligned output (you can play on the %10s parameter):

who -u | nawk '{printf("%10s %10s %10s %\n",$1,$6,$7)}' >/tmp/myfile

Hope this helps!
 
My post had a typo (sorry). This one should work:

who -u | nawk '{printf("%10s %10s %10s \n",$1,$6,$7)}' >/tmp/myfile

 
#!/usr/bin/ksh

# who -u | nawk '{print $1, $6, $7}' >/tmp/myfile

who -u|while read LINE #reads the results of"who -u" line by line
do
# define variables
USER_ID=`echo $LINE|awk '{print $1}'`
TIME=`echo $LINE|awk '{print $6}'`
PID=`echo $LINE|awk '{print $7}'`

# determines the length of USER_ID and adds two tabs if less than 8
# \t is a tab
case ${#USER_ID} in
[1-7])
echo "$USER_ID\t\t$TIME\t\t$PID"
echo "$USER_ID\t\t$TIME\t\t$PID" >> /tmp/myfile.$$
;;
*)
echo "$USER_ID\t$TIME\t\t$PID"
echo "$USER_ID\t$TIME\t\t$PID" >> /tmp/myfile.$$
esac
done

echo "Results can be found in /tmp/myfile.$$"
 
hey,


WOW, thanks guys I think I have found the best site for learning UNIX. Its incredible how much knowledge is available so quickly, thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top