saving values in file in an array in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting saving values in file in an array in awk
# 1  
Old 01-13-2009
saving values in file in an array in awk

hi i am trying to save values in a file in an array in awk..the file is as follows:

Code:
0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,

so far i have this:

Code:
awk 'BEGIN {RS="\n";FS=","}
{
for(i=1;i<=NR;i++)
{
for(j=1;j<=NF;j++)
{
 a[i,j]=$j;
}
}
}
END {
for(i=1;i<=NR;i++)
{
for(j=1;j<=NF;j++)
{
printf("%c\n",a[i,j]);
}
}
}' try > try1

however, i am not getting anything in the try1 file. what am i doing wrong? thanks
# 2  
Old 01-13-2009
You don't need to do a for loop to loop through the records, because awk itself will automatically read every line of the input file and execute the code between { ... } on those lines. Also you do not need to set the RS to \n because that is the default record separator anyway. So this should be sufficient:

Code:
awk '
        BEGIN { FS="," }
        {
                for(j=1;j<=NF;j++) {
                         a[NR,j]=$j;
                }
        }
        END {
                for(i=1;i<=NR;i++) {
                        for(j=1;j<=NF;j++) {
                                printf("%d\n",a[i,j]);
                        }
                }
        }
' try > try1

Also note that I changed %c to %d. %c would print the character whose ascii code is 0, which is a null character, probably the main reason your output file looked empty.
# 3  
Old 01-13-2009
thanks a lot for your help.. that worked perfect.. just one last question.. is there a quick and dirty way to get rid of the last comma in the last column? since my FS is a comma it is picking up an extra column after the last column..
# 4  
Old 01-13-2009
Well, you could just use <NF instead of <=NF to ignore the final field... is that what you want?

Last edited by Annihilannic; 01-13-2009 at 09:26 PM.. Reason: Oops, I meant NF, not NR
# 5  
Old 01-13-2009
yup, got it..dunno what i was thinking that was easy..thanks a lot for your help..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk saving field of first file into array

Hello guys, I just start trying out AWK and encounter a problem, I try to think a bit but seems my way is incorrect. I have two input file, with the first file has only one field, the second file has 3 fields, I suppose to do stuffs to them by writing an awk program, kinda sort them out. Since I... (15 Replies)
Discussion started by: RozenKristal
15 Replies

2. Shell Programming and Scripting

Assigning array values using awk in shell scripting

hi My script as below #!/bin/ksh for i in `seq 1 7` do a=$(awk '{print $i}' /home/rama/expenese.txt) done for i in `seq 1 7` do echo "${a}" done content of expense.txt is as below 5032 210179 3110 132813874 53488966 11459221 5300794 I want output as... (6 Replies)
Discussion started by: Ramakrishna V
6 Replies

3. Shell Programming and Scripting

Array in awk outputs multiple values

Disclaimer: OP is 100% Awk beginner. I use this code on ASCII files I need to report against. awk 'BEGIN { tokens = 0 tokens = 0 tokens = 0 } { for (token in tokens) { if ($1 == token){print $0; tokens++;}}} END {for (token in tokens){ if( tokens ==... (1 Reply)
Discussion started by: alan
1 Replies

4. Shell Programming and Scripting

AWK- extracting values from columns, saving them and gettins statistics

Hello, I am obviously quite new to unix and awk. I need to parse certain columns of a file (delimited by spaces), and somehow save the value of this column somewhere, together with the value of the column just after it (by pairs; so something like ). I'm then supposed to count the times that... (9 Replies)
Discussion started by: acsg
9 Replies

5. Shell Programming and Scripting

Saving file content in arrays using AWK

Hi, im new to shell scripting. i have a query for which i have searched your forums but coulndt get what i need. i have a file that has two records of exactly the same length and format and they are comma seperated. i need to save the first and the second columns of the input file to 2 different... (11 Replies)
Discussion started by: atikan
11 Replies

6. Shell Programming and Scripting

Piping Unix Variable Array values into AWK

#ksh Here is my code: ERRORLIST="43032 12001 12002 12003 12004 34019 49015 49016 49017 49018 49024 49025 49026 58004 72003 12005 12006 12007 12008 12011 12012 16024 16023" for ERROR in ${ERRORLIST} do awk -v l="$lastdate" '/^....-..-../&&$0>l{d=$0}d&&/Error: '"${ERROR}"'/{print... (3 Replies)
Discussion started by: k1ko
3 Replies

7. Shell Programming and Scripting

Problem with lookup values on AWK associative array

I'm at wits end with this issue and my troubleshooting leads me to believe it is a problem with the file formatting of the array referenced by my script: awk -F, '{if (NR==FNR) {a=$4","$3","$2}\ else {print a "," $0}}' WBTSassignments1.txt RNCalarms.tmp On the WBTSassignments1.txt file... (2 Replies)
Discussion started by: JasonHamm
2 Replies

8. Shell Programming and Scripting

fetching values using awk and storing into array

hi all I am using awk utility to parse the file and fetching two different vaues from two different record of a record set. I am able to see the result, now i want to store the result and perform some check of each values form database to mark valid and invalid. could you please help me... (3 Replies)
Discussion started by: singhald
3 Replies

9. Shell Programming and Scripting

saving values from awk expression into shell array

hi i am trying to save the values i extract from a file with the help of awk in a bash shell array. i have: exec 10<file2 while read LINE <&10; do ARRAY1=$(awk '{print $1}' file2) ((count++)) done echo ${ARRAY1} it prints just blank lines. file1 has two columns and i... (4 Replies)
Discussion started by: npatwardhan
4 Replies

10. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies
Login or Register to Ask a Question