1834097 Members
2143 Online
110063 Solutions
New Discussion

Awk inside for loop

 
SOLVED
Go to solution
Allan Umandap
Advisor

Awk inside for loop

Hi Guys,

I have a text file that look like this:

20050605

00 qq
01 ss
02 aa
03 rr

Now, i want to make it in this format...

20050605 00 qq
20050605 01 ss
20050605 02 aa
20050605 03 rr


using awk i was able to produce it:

awk '
NF == 1 { tab = $1 ; next }
NF == 2 { print tab , $0 } ' filename

but when i put inside a loop im only getting this result:

00 qq
01 ss
02 aa
03 rr

the other field is gone..i need to run it inside a for loop since i have mutiple files to convert and this is what i used:

for sgry in *.log
do
awk '
NF == 1 { tab = $1 ; next }
NF == 2 { print tab , $0 } ' $sgry > new.tmp
mv new.tmp $sgry
done

is there a better way to do it?
10 REPLIES 10
Muthukumar_5
Honored Contributor

Re: Awk inside for loop

You can do it as,

# awk '/20050605/ { var=$1; getline;getline;} { print var" "$0 }' testfile
20050605 00 qq
20050605 01 ss
20050605 02 aa
20050605 03 rr


getline;getline is used to move 20050605,' ' line.

hth.
Easy to suggest when don't know about the problem!
Biswajit Tripathy
Honored Contributor

Re: Awk inside for loop

One technique would be the following:

# Script Starts here
####################
exec 4<$1
read -ru4 first
while read -ru4 f1 f2
do
echo $first $f1 $f2
done
# Script Ends Here
###################

Store above script in a file "myscript"
and run at commandline as:
# ./myscript testfile_name

- Biswajit
:-)
Muthukumar_5
Honored Contributor

Re: Awk inside for loop

Just try simply as,


for file in `ls *.log`
do

awk '/20050605/ { var=$1; getline;getline;} { print var" "$0 }' $file > tmpfile
mv tmpfile $file

done

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor
Solution

Re: Awk inside for loop

sorry again. You can try as,

for file in *.log
do

awk '{ if ( NR == 1) { var=$1;} if ( $0 != "" && NF != 1 ) { print var" "$0 }}' $file > tmpfile
mv tmpfile $file

done

hth.
Easy to suggest when don't know about the problem!
Shyjith P K
Frequent Advisor

Re: Awk inside for loop

Hi Allan,

Can you try like this?

ls *.log >test
for sgry in `cat test`
do
awk '
NF == 1 { tab = $1 ; next }
NF == 2 { print tab , $0 } ' $sgry > new.tmp
mv new.tmp $sgry
done

Cheers
Shyjith
Leif Halvarsson_2
Honored Contributor

Re: Awk inside for loop

Hi,
yet another solution
awk 'BEGIN { getline; var=$0 ; getline } { print var" "$0 }' file
Allan Umandap
Advisor

Re: Awk inside for loop

hi guys...i already sold my problem thanks for all your help..however i have another question before i close this thread...

The 20050607 is actually the current date yyyymmdd, can anybody show me how to make my output like this:

Jun07 00 aa
Jun07 01 bb
Jun07 02 cc
Jun07 03 dd
Jun07 04 ee

tnx...

Muthukumar_5
Honored Contributor

Re: Awk inside for loop

Try as,


for file in *.log
do

awk '{ if ( $0 != "" && NF != 1 ) { print var" "$0 }}' var="Jul 07" $file > tmpfile
mv tmpfile $file

done
Easy to suggest when don't know about the problem!
Leif Halvarsson_2
Honored Contributor

Re: Awk inside for loop

Hi,
Maybe a task for Clay Stephenssons "date hammer".

http://mirrors.develooper.com/hpux/caljd-2.2.pl
Muthukumar_5
Honored Contributor

Re: Awk inside for loop

sorry. If there is no space between month and date then,

you can easily do it as,

## CODE ###
for file in *.log
do

awk '{ if ( $0 != "" && NF != 1 ) { print var" "$0 }}' var=`date +'%a%d'` $file > tmpfile
mv tmpfile $file

done
Easy to suggest when don't know about the problem!