cancel
Showing results for 
Search instead for 
Did you mean: 

Re: awk help needed

 
SOLVED
Go to solution
Andrew Beal
Frequent Advisor

awk help needed

Hi,

I am trying to write a script that will stagger the output below 6 fields accross separated by white spaces then create a new line until all of stdout has been appended in this format to a file.

example of output to be formatted... (only the file names field are to be formatted).

[root@melhpux3] /home/beala01-> du -sk ./*
7 ./Navimon.cfg
651 ./RPGC.prn
5 ./agent.config
8 ./audit-hpux-10092003.sh
29 ./benstat-dc.pri
2 ./bin/
7 ./class-users.csv
0 ./crap.out
0 ./crap/
1 ./default
7467 ./emcgrab/
1525 ./emcgrab_HP-UX_v3.4_1.tar
59 ./fileset-state.doc
107 ./fileset.doc
17 ./jaf.etp
4 ./nportids
6 ./p143027b.src
71 ./passwd
1 ./payroll-users.csv
370 ./scripts/
35030 ./spancustom.tar
4170 ./spo740c.tar
1 ./stuff.txt
22 ./superb-users.csv
1 ./supero-users.csv
3 ./tmp/
1 ./wp-testing.txt
157 ./wp/

desired output...

[root@melhpux3] /home/beala01-> ls
.Xauthority RPGC.prn
.cshrc agent.config
.jpiu benstat-dc.pri

etc...

If there is some way to do this in awk, i would appreciate your help.

Thanks in advance :)

11 REPLIES 11
Elmar P. Kolkman
Honored Contributor
Solution

Re: awk help needed

The solution should be something like this:

ls | awk '{printf "%s",$0 ; if (NR % 6) {printf "\t";} else {printf "\n"}} END {printf "\n"}'

Or, if you want it better looking, but only if your columns are not too wide:
ls | awk '{ printf "%-15s",$0 ; if ((NR % 6) == 0) { printf "\n";}} END {printf "\n"}'

You can alter the columnwidth in the printf here, and the number of columns is the 6, used with the modulo sign (%).

(Of course it can be shorter, for instance replace printf "\n" with print ""..)
Every problem has at least one solution. Only some solutions are harder to find.
Muthukumar_5
Honored Contributor

Re: awk help needed

ls will try to display the informations in the current directory and du will try do everything under the current directory.

To get at more behaviour of ls with du then,

du -a | awk '{ print $2 }' | sed -e 's/^\.\///;s/\/[^ ]*$//' | sort -u

ls will display file as,

1 3 5 7
2 4 6 8

the order may be changed depends up on the files count there.

Easy to suggest when don't know about the problem!
Hoefnix
Honored Contributor

Re: awk help needed

Hi,

cat in.txt | awk -F / 'BEGIN {cnt=0} {cnt+=1; if (cnt < 6) {printf "%s ",$2;} else {cnt=0; printf "%s\n",$2;}} END {printf "\n"}'

HTH,
Peter
Andrew Beal
Frequent Advisor

Re: awk help needed

Thankyou all,

Elmar & Peter, both your commands worked perfectly, and gave the exact output I required!!!

OMG... you have no idea how much pain you guys have saved me!

Muthukumar, I couldn't get your's to work imediatley, but I will fiddle with it a bit.

Thankyou all once again :)
H.Merijn Brand (procura
Honored Contributor

Re: awk help needed

Why not use pr?

# whatever_command | pr -t -a -w78

-t: terminal output
-a: accross
-w78: output width

Many more options supported

Enjoy, Have FUN! H.Merijn [ who uses the right function for the job ]
Enjoy, Have FUN! H.Merijn
Muthukumar_5
Honored Contributor

Re: awk help needed

I misunderstand that " you want to make du output as like ls output" so why tried with sed + awk there.

I hope you got the awk with king's reply there. But procura's command more good as,

ls | pr -d -c 6

it will give easily.
Easy to suggest when don't know about the problem!
Elmar P. Kolkman
Honored Contributor

Re: awk help needed

Sorry to disagree, but pr doesn't do what you want. In Procura's solution it does not generate 6 columns, and in Muthukumar it will truncate long file names...

Look at this as explanation:
ptc27b08:/var/adm/syslog # ls | pr -t -d -c 6
OLDsyslog.l mail.log.2. mail.log.6. syslog.log syslog.log. syslog.log.

genlog.log mail.log.3. mail.log.7. syslog.log. syslog.log. syslog.log.

mail.log mail.log.4. mail.log.8. syslog.log. syslog.log. syslog.logS

mail.log.1. mail.log.5. mail.log.9. syslog.log. syslog.log.
ptc27b08:/var/adm/syslog # ls
OLDsyslog.log mail.log.1.gz mail.log.4.gz mail.log.7.gz syslog.log syslog.log.3.gz syslog.log.6.gz syslog.log.9.gz
genlog.log mail.log.2.gz mail.log.5.gz mail.log.8.gz syslog.log.1.gz syslog.log.4.gz syslog.log.7.gz syslog.logSAMTRM
mail.log mail.log.3.gz mail.log.6.gz mail.log.9.gz syslog.log.2.gz syslog.log.5.gz syslog.log.8.gz
ptc27b08:/var/adm/syslog #
Every problem has at least one solution. Only some solutions are harder to find.
H.Merijn Brand (procura
Honored Contributor

Re: awk help needed

man tr

I did not give a -# (like -2) argument, because the original statement is not clear enough to me. Neither is the general definition of columned output of ls. There is no `rule' on how many columns ls produces. It depends on -F, using colors, screen width, screen properties (man terminfo look at xmc-cookie-glitch) and many other factors.

if the output of command 'blah' gives you three columns, and you want to make 6 columns where only the last field of each line is reformatted, do

# blah | pr -2 -a -t

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Elmar P. Kolkman
Honored Contributor

Re: awk help needed

But that still does not what you want... If you have long column entries, it still truncates to make 6 columns of equal width. So pr is only usable if you als give a commandline option to set the width to 6 times the lenght of the largest entry plus six, or in formula:
6*(width + 1)

Every problem has at least one solution. Only some solutions are harder to find.