The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl: storing regex in array variables trouble xist Shell Programming and Scripting 3 08-29-2008 09:07 AM
storing variables in array.Please help nua7 Shell Programming and Scripting 7 07-17-2008 10:47 AM
Storing pointer array in C arunkumar_mca UNIX for Dummies Questions & Answers 1 08-23-2007 09:49 AM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 01:15 PM
Getting database records in an array rawat_me01 Shell Programming and Scripting 2 05-07-2007 01:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-05-2009
npatwardhan npatwardhan is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 135
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 (permalink)  
Old 01-05-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

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 (permalink)  
Old 01-05-2009
npatwardhan npatwardhan is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 135
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 (permalink)  
Old 01-05-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0