Sponsored Content
Top Forums Shell Programming and Scripting Print array into a single file - AWK Post 302724265 by PaulaL on Wednesday 31st of October 2012 12:27:23 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 09:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy