Print array into a single file - AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print array into a single file - AWK
# 1  
Old 10-31-2012
Print array into a single file - AWK

Hi all,
I been looking for a solution to the fact that when I use:
Code:
for (i=1; i<=NF; i++) print $i

fields that are originally in a single line are printed in a single line

I have severals files for which the first 7 are the same, but the number of variables after that can vary, for example NF is 16, 46, 109, etc...

I want to change the codes AAA, BBB and CCC for numbers, for ex, 10,20,30
And I want to do it for a list of files.

Here is an example of my input file:
Code:
SAMP1,189,198,473909,7479044,1517,AAA,8.2,27.7,18.0
SAMP2,231,240,473917,7478991,1487,AAA,12.7,50.2,30.0
SAMP3,197,205,474194,7479052,1511,BBB,15.5,58.8,17.0
SAMP4,226,235,474018,7479000,1494,CCC,-99,-99,-99
SAMP5,326,335,475049,7482002,1427,BBB,15.2,69.4,11.0

The code that I'm using:
Code:
cat<<END>list.txt
file1
file2
file3
file4
file5
file6
file7
file8
END
FILE=`awk '{print $1}' list.txt`
for var in $FILE
do 
awk 'BEGIN {FS = "," ; OFS = ","} ; {
for (i=1; i<=NF; i++) val[i] = $i};
{
val[8] = -99
if ($8 == "AAA")
 val[8] = 10
else if ($8 == "BBB")
 val[8] = 20
else if ($8 == "CCC")
 val[8] = 30;
for (i=1; i<=NF; i++) print val[i];  
}' $var.csv > $var.aaa
 
done

the problem is that the output is:

Code:
SAMP1
189
198
473909
7479044
1517
10
8.2
27.7
18.0
SAMP2
231
240
473917
7478991
1487
10
12.7
50.2
etc

RATHER THAN:

Code:
SAMP1,189,198,473909,7479044,1517,10,8.2,27.7,18.0
SAMP2,231,240,473917,7478991,1487,10,12.7,50.2,30.0
SAMP3,197,205,474194,7479052,1511,20,15.5,58.8,17.0
SAMP4,226,235,474018,7479000,1494,30,-99,-99,-99
SAMP5,326,335,475049,7482002,1427,20,15.2,69.4,11.0

Smilie

Could anybody help?

Last edited by radoulov; 10-31-2012 at 01:38 PM.. Reason: Additional code tags.
# 2  
Old 10-31-2012
That is several useless uses of awk and backticks... You could just do

Code:
for FILE in file1 file2 file3 file4 file5 file6 file7 file8
do
...
done

or

Code:
while read FILE
do
...
done < list.txt

Also, why should it be printing what you want? You're telling it to print x lines, you get x lines.

Also, why should it be printing the new values? You stored the old values in the array, and that's what you get.

You don't need to do either of them. You don't need an array, and you don't need to store all the old values.

In fact you don't need a loop at all, awk is quite capable of handling more than one file all by itself.

So:

Code:
# Store these things in a file for awk to grab easily
cat >trans.txt <<EOF
AAA,-10
BBB,20
CCC,30
EOF

awk -F"," -v OFS="," '

# Load data from trans.txt, the very first file
NR==FNR { T[$1]=$2; next }
# Translate data if found in array
$8 in T { $8=T[$8] }
{ print > FILENAME ".aaa" }' trans.txt file*


Last edited by Corona688; 10-31-2012 at 01:47 PM..
# 3  
Old 10-31-2012
Thanks!

Sorry for the messy code, I don't have any formal training, so everything I use is pieces and patches I found in the web.

It looked like it have work (the traslation and formating), but I'm getting all files into a single file.

Also could you explain a little more what you are doing in the lines:
Code:
NR==FNR { T[$1]=$2; next }
$8 in T { $8=T[$8] }
{ print > FILENAME ".aaa" }' trans.txt file*

# 4  
Old 10-31-2012
NR and FNR are special variables in awk. NR means the total number of records processed, FNR is the number of records read from the current file. When they are identical, that means you are in the very first file and nowhere else.

I use that to load the list of stuff to translate into awk, stuffing it into an array like T["AAA"]="-10" Then the 'next' tells it to skip to the next line, preventing it from running any of the code below.

For all the other files, it checks if the array T has anything with an index identical to column 8. $8 in T ...and if it does, it just overwrites column 8 with the contents of the array. $8=T[$8]

Then it prints the entire line to the file FILENAME ".aaa". FILENAME is a special variable which names the current file. Putting two strings together like this concatenates them. So if the current file is file1, it will write to "file1.aaa".
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-31-2012
Thank you, it work great!
and thank you very much for the explanations!!!

---------- Post updated at 01:31 PM ---------- Previous update was at 12:32 PM ----------

Hi Again Corona688,

How can I do it if the string codes I want to replace AAA, BBB, CCC occurs in more than one field; $8, $9, $10 and $11, for example. What do I need to change to the code:

Code:
awk -F"," -v OFS="," '
NR==FNR { T[$1]=$2; next }
$12 in T { $12=T[$12] }
{ print > FILENAME ".aaa" }' trans.txt $FILE.csv

Thank you very much in advance Smilie
# 6  
Old 11-01-2012
Code:
awk -F"," -v OFS="," '
NR==FNR { T[$1]=$2; next }
{ for(N=8; N<=11; N++) if($N in T) $N=T[$N
   print > FILENAME ".aaa" }' trans.txt $FILE.csv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not getting array in .awk file and print it

I have test.sh file as below : set -A IDARR $ID echo | awk -f test.awk -v TempArr="${IDARR }" I have test.awk file as below : BEGIN { Flag = 1; } { print "Hello"; for(i in TempArr) { print i; } } (9 Replies)
Discussion started by: nes
9 Replies

2. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

3. Shell Programming and Scripting

How do you print a single quote character in AWK

How do you print out a single quote character in AWK? Using the escape character does not seem to work. {printf "%1$s %2$s%3$s%2$s\n" , "INCLUDE", " \' ", "THIS" } does not work. Any suggestions? (6 Replies)
Discussion started by: cold_Que
6 Replies

4. Shell Programming and Scripting

BASH: print matrix from single array

I am creating a report in groff and need to format data from a file into a table cell. Sample data: dador,173323,bpt,jsp,39030013338878,1 dador,173323,brew,jsp,39030013338860,1 dador,173323,brew,jsp,39030013339447,1 dador,173323,brew,jsp,39030013339538,1 I would like to build a table... (12 Replies)
Discussion started by: Bubnoff
12 Replies

5. Shell Programming and Scripting

URGENT! single apex in awk print

cat a | awk -F";" '{print "update db set column=' "$2" ' where column1=\""$1"\";"}' > ip-add.sql Hi! I'm a new user! i've an urgent help on the single apex use in the double apex print string The apex between che "$2" should not be interpreted, but....how?! I'm trying to use \ but don't work... (1 Reply)
Discussion started by: Re DeL SiLeNziO
1 Replies

6. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

7. Shell Programming and Scripting

want to print single quote using awk

i want to print ' symbol using awk i tried: awk '{print " ' "}' awk '{print "\' "}' both not work please help me. (2 Replies)
Discussion started by: RahulJoshi
2 Replies

8. UNIX for Dummies Questions & Answers

how to print single quote in awk

Hi all, It is a very stupid problem but I am not able to find a solution to it. I am using awk to get a column from a file and I want to get the output field in between single quotes. For example, Input.txt 123 abc 321 ddff 433 dfg ........ I want output file to be as ... (6 Replies)
Discussion started by: gauravgoel
6 Replies

9. Shell Programming and Scripting

Accessing single elements of a awk array in END

How do I access one of the indices in array tst with the code below? tst=sprintf("%5.2f",Car / 12) When I scan thru the array with for ( i in tst ) { print i,tst } I get the output of: vec-7 144 But when I try this in the END print tst It looks like it's not set. What am... (6 Replies)
Discussion started by: timj123
6 Replies

10. UNIX for Dummies Questions & Answers

awk to print ' (single quotes)

I'm building a file with sql delete statements. I need to print the single quotes for the where clause. i.e. delete from Command where CommandName = 'SomeName'; I have the following in my script input=$(pwd)/Cmnd.csv i=0 while read line do if test $i -ge 1; then ... (2 Replies)
Discussion started by: orahi001
2 Replies
Login or Register to Ask a Question