System Administration

Re: How print output of awk into specific file based on column parameter?

 
SOLVED
Go to solution
debian111
Regular Advisor

How print output of awk into specific file based on column parameter?

Hi all,
usnig this part of code

cat 942prm_200611* | tr ' | ' ' ' > 942sve.txt

awk ' { if(( $10=="30001") && (($22=="34") || ($22=="128") || ($21=="31"))) print $10 "\t" $22 }' 942sve.txt > jacause.txt
awk ' { if(( $10=="30350") && (($22=="31") || ($22=="128") || ($22=="16"))) print $10 "\t" $22 }' 942sve.txt >> jacause.txt
awk ' { if(( $10=="50111") && (($22=="34") || ($22=="128"))) print $10 "\t" $22 }' 942sve.txt >> jacause.txt


I got output like bellow ....

30001 128
30001 128
30001 128
30350 16
30350 16
30350 16
30350 16
30350 31
30350 128
30350 128
30350 31
30350 128
30350 31
30350 16
30350 128
30350 31
30350 31

first column means telephne number and second column menas specific cause for that number. Now I need to implement additional script ( or inside this one ) that will make possible to for example number 30350 select all causes to different files. I need to make output like
in file 30350_128.txt should be

30350 128
30350 128
30350 128

in file 30350_31.txt

30350 31
30350 31

in file 30350_16.txt

30350 16
30350 16
30350 16
30350 16

For number 30001 I need make the same
for example 30001_128.txt

30001 128
30001 128
30001 128

( there is no other causes for this number, just this one )

and for every differnt number in column one write data for specific cause in column 2 into differen file like in example above.

I am hoping that someone know how to do this and have time to write, or just send me an hint how to do this... and help me.

I must do it in awk, no other program languages.

Thank you in advance
3 REPLIES 3
Peter Godron
Honored Contributor

Re: How print output of awk into specific file based on column parameter?

Hi,
you may want to expand on Hein's answer in
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=856411

Another helpful util could also be:
uniq -c to count the unique entries for each key (number + cause)
Jean-Yves Picard
Trusted Contributor
Solution

Re: How print output of awk into specific file based on column parameter?

Hello

awk '{ u[$1 "_" $2 ] ++ }
END {
for (x in u )
{ printf "%d messages",u[x] >("f" x ".txt") }
}' jacause.txt

to count

or
awk '{printf $0 > $1 "_" $2 ".txt"}' jacause.txt to answer your question.

Jean-Yves Picard


debian111
Regular Advisor

Re: How print output of awk into specific file based on column parameter?

Thank you, with your help I solved this :)