storing records in awk array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting storing records in awk array
# 1  
Old 01-05-2009
storing records in awk array

hi i have a file as follows:

Code:
1
2
3
4
5
6

i want to store all these numbers in an array using awk.. so far i have:

Code:
awk '{for(i=1;i<=NR;i++) {a[i]=$1}} END {for(i=1;i<=NR;i++) {printf("%1.11f",a[i])}}' 1.csv > test

however, i am getting all values as zero in the "test" file..
appreciate any help.
# 2  
Old 01-05-2009

Code:
awk '
{ a[NR]=$1 }
 END { for(i=1;i<=NR;i++) printf "%1.11f\n", a[i] }
' 1.csv > test

Of course, if you just want to format the file:

Code:
awk '{ printf "%1.11f\n", $1 }' 1.csv > test

# 3  
Old 01-05-2009
ok thanks that worked..
here is what i want to do next. i have this file called test which has values as follows:

Code:
1
2
3
4
5
6
7

i have another file called data with multiple fields and records as follows:
(there are more columns but following is just a snapshot of the file)

Code:
col1  col2
1       1.4
2       1.6
3       1.7
4       1.8
5       1.9
6       2.0

i would like to get a file with entries as follows: (i am subtracting each row from the data file for the first column from the first entry of the first row in the test file)

Code:
col1         col2
1-1         1.4-2
2-1         1.6-2
3-1         1.7-2
4-1         1.8-2
5-1         1.9-2
6-1         2.0-2

so far i was trying something along these lines:

Code:
awk 'NR==FNR{for(i=1;i<=NF;i++) {a[NR]=$i;next} {b[NR]=$i;next} END {for(i=1;i<=NF;i++) {printf("%1.11f, %1.11f\n",a[i],b[i])}}' test data > try

i am trying to save all the rows from the test file in an array(a). how do i save the col1 and col2 values from the data file in an array and then use it to subtract values from array(a)? i want to stick to awk.
thanks
# 4  
Old 01-05-2009
I think that's what you want, but not 100% sure:
Code:
awk 'NR==FNR{
           a[NR]=$i
           next
       }
       {
           for(i=1;i<=NF;i++) 
              printf("%1.11f%c", $i - a[FNR], (i==NF) ? ORS : OFS)
        }
' test data

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Storing timestamp of files in an array

Hi, I have written the below script to get the timestamp of each files and result is as below Script find /home/user -type f -name "*.json" -printf '%Tc %p\n' | awk {'print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " $7'} Input -rw-r--r-- 1 user domain users 17382 Jul 19 06:10... (5 Replies)
Discussion started by: nextStep
5 Replies

2. Shell Programming and Scripting

Storing two dimensional array for postprocessing

Hi Community, Would love to get some quick help on below requirement. I am trying to process mpstat output from multiple blades of my server I would like to assign this the output to an array and then use it for post processing. How can I use a two dimensional array and assign these value ... (23 Replies)
Discussion started by: sshark
23 Replies

3. Shell Programming and Scripting

Awk: Storing string in array(triangular form)

I have a string like below. Now i want to split this string like below and put the value in the array using awk. I am able to do it using bash but getting no clue for doing it in awk O/P A=0 A=01 A=014 A=0143 A=01438 A=014387 A=0143876 A=01438765 A=014387650 ... (7 Replies)
Discussion started by: siramitsharma
7 Replies

4. Shell Programming and Scripting

Storing command output in an array

Hi, I want keep/save one command's output in an array and later want to iterate over the array one by one for some processing. instead of doing like below- for str in `cat /etc/passwd | awk -F: '$3 >100 {print $1}' | uniq` want to store- my_array = `cat /etc/passwd | awk -F: '$3 >100 {print... (4 Replies)
Discussion started by: sanzee007
4 Replies

5. Programming

C; storing strings in an array

I am trying to get userinput from stdin and store the lines in an array. If i do this: using a char **list to store strings allocate memory to it #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { char *prog = argv; char **linelist; int... (5 Replies)
Discussion started by: tornow
5 Replies

6. Shell Programming and Scripting

Storing data in perl 2D array

Respected All, Kindly help me out. I have got file listings in a directory like this: -rw-r--r-- 1 root root 115149 2011-11-17 07:15 file1.stat.log -rw-r--r-- 1 root root 115149 2011-11-18 08:15 file2.stat.log -rw-r--r-- 1 root root 115149 2011-11-19 09:15 file3.stat.log -rw-r--r-- 1... (2 Replies)
Discussion started by: teknokid1
2 Replies

7. Shell Programming and Scripting

Storing blanks in an array in bash

Hi Guys, I have a file which is as follows: 1 2 4 6 7 I am trying to store these values in an array in bash. I have the following script: FILE=try.txt ARRAY=(`awk '{print}' $FILE`) echo ${ARRAY} (3 Replies)
Discussion started by: npatwardhan
3 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

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

10. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies
Login or Register to Ask a Question