Multiple array problem in NAWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple array problem in NAWK
# 1  
Old 08-19-2010
Question [Solved] Multiple array problem in NAWK

I HAD these 2 files:

file1
Code:
pictures.txt 5 ref2313 4 ref2345 3 ref5432 2 ref4244 1
dance.txt 6 ref2342 5 ref2352 4 ref0695 3 ref5738 2 ref4948 1
treehouse.txt 6 ref8573 5 ref3284 4 ref5838 3 ref4738 2 ref4573 1

file2
Code:
pictures.txt 1 3
dance.txt 2 4
treehouse.txt 3 5

what I wanted to do was compare them in such a way that only the numbers with the refernce numbers in file1, which are between the 2 numebrs in file2 would show up. (NOTE: all version 1's don't have a reference number) so:

output:
Code:
pictures.txt 3 ref5432 2 ref4244 1
dance.txt 4 ref0695 3 ref5738 2 ref4948
treehouse.txt 5 ref3284 4 ref5838 3 ref4738

and I had code
Code:
#!/bin/bash
/usr/bin/nawk 'NR==FNR {for (i=2;i<=NF;i+3) a[$1 FS $i]=$(i+1);next}
   {printf $1 FS} {for (i=($3);i>=($2);i-=1) printf i FS a[$1 FS i] FS}{printf "\n"}' file1 file2

which worked fine! BUT!! now my input file, file1, has changed to:

file1
Code:
pictures.txt 5 ref2313 dn32334 4 ref2345 dn32334 3 ref5432 dn32334 2 ref4244 dn32334
dance.txt 6 ref2342 dn32334 5 dn32334 ref2352 4 dn32334 ref0695 3 ref5738 dn32334 2 dn32334 ref4948 1
treehouse.txt 6 dn32334 ref8573 5 ref3284 dn32334 4 ref5838 dn32334 3 dn32334 ref4738 2 dn32334 ref4573 1

so a dn numebr has been added to each version number (except version 1). The order on which the reference and dn appear may change for each one, but that doesn't matter. So I tried to edit my code to reflect this change:

new code:
Code:
/usr/bin/nawk 'NR==FNR {for (i=2;i<=NF;i+3) a[$1 FS $i]=$(i+1); b[$1 FS $i]=$(i+2) ;next}
   {printf $1 FS} {for (i=($3);i>=($2);i-=1) printf i FS a[$1 FS i] FS b[$1 FS i] FS}{printf "\n"}' file1 file2

but this doesn't work!! as it only prints out whichever comes first for each version number out of the reference and dn number:

Code:
pictures.txt 3 ref5432 2 ref4244 
dance.txt 4 dn32334 3 ref5738 2 dn32334
treehouse.txt 5 ref3284 4 ref5838 3 dn32334

and breath!!!!

Please help!

---------- Post updated at 04:23 PM ---------- Previous update was at 03:35 PM ----------

if anyone cares, I worked it out!

Code:
/usr/bin/nawk 'NR==FNR {for (i=2;i<=NF;i+3) (a[$1 FS $i]=$(i+1)) (b[$1 FS $i]=$(i+2)) ;next}
   {printf $1 FS} {for (i=($3);i>=($2);i-=1) printf i FS a[$1 FS i] FS b[$1 FS i] FS}{printf "\n"}' file1 file2


Last edited by linuxkid; 08-19-2010 at 11:41 AM..
# 2  
Old 08-19-2010
Thanks for giving us your solution!

All the best
(And marked as solved in title for search purpose...)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple bash variables passed into nawk

I have a file that has 2 fields called b_file: 11977 DAR.V3.20150209.1.CSV 3295 DAR.V3.20150209.1.CSV 1721 DAR.V2.20150210.1.CSV I need to search a sftplog using the field 1, but want to maintain the relationship between field 1 and 2. I am passing field 1 as a parameter in a bash loop. ... (14 Replies)
Discussion started by: smenago
14 Replies

2. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

3. Shell Programming and Scripting

Nawk if logic problem

nawk '{ fmt="%3s %22s %48s %35s %21s\n"; if ($3==$6 && $1=="STOPLOSS") { tpy="Successful Match"; jnme=$1; sts="File will be loaded"; cntrl=$3; audit=$6; printf (fmt, tpy,jnme,sts,cntrl,audit) >> "'${AUDIT_DATA_FILE}/${AUDIT36}'" }else if ($3!=$6 && $1=="STOPLOSS") { tpy="Mis-Match ";... (4 Replies)
Discussion started by: wawa
4 Replies

4. Shell Programming and Scripting

Counting Multiple Fields with awk/nawk

I am trying to figure out a way in nawk to 1) get a count of the number of times a value appears in field 1 and 2) count each time the same value appears in field 2 for each value of field 1. So for example, if I have a text file with the following: grapes, purple apples, green squash, yellow... (2 Replies)
Discussion started by: he204035
2 Replies

5. Shell Programming and Scripting

problem in redirecting records using nawk

I am trying to redirect record to two files using nawk if-else. #Identify good and bad records and redirect records using if-then-else nawk -F"|" '{if(NF!=14){printf("%s\n",$0) >> "$fn"_bad_data}else{printf("%s\n",$0) >> $fn}}' "$fn".orig "$fn".orig is the source file name bad... (7 Replies)
Discussion started by: siteregsam
7 Replies

6. Shell Programming and Scripting

Nawk help searching for multiple lines and multiple searches

I use this command to find a search (Nr of active alarms are) and print one line before and 10 lines after the search keywords. nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=1 a=10 s="Nr of active alarms are:" *.log However, I would like to know how to tell it to print... (3 Replies)
Discussion started by: tthach830
3 Replies

7. Shell Programming and Scripting

Nawk, creating a variable total from multiple lines(records)

Good Morning/Afternoon All, I am having some trouble creating a variable called "total" to display the sum of the values in a specific field, $6 for example. The data I am working on is in the following form: John Doe:(555) 555-5555:1:2:3 Jane Doe:(544) 444-5556:4:5:6 Moe Doe:(654)... (2 Replies)
Discussion started by: SEinT
2 Replies

8. Shell Programming and Scripting

NAWK array to store lines from huge file

Hi, I would like to clarify about the NAWK array to store multiple lines from huge file. The file is having an unique REF.NO, I wants to store the lines (it may be 100+ lines) till I found the new REF.NO. How can I apply NAWK - arrays for the above? Rgds, sharif. (1 Reply)
Discussion started by: sharif
1 Replies

9. UNIX for Dummies Questions & Answers

nawk array help

This is a derivative of a question I asked earlier, but I can't figure out how to make all of the pieces work (mainly the array piece). How do I get the following data set: aaa|bbb|ccc|123|ddd... abc|def|ghi|123|jkm... aaa|bbb|ccc|456|ddd... abc|def|ghi|456|jkm... aaa|bbb|ccc|789|ddd...... (4 Replies)
Discussion started by: clueless181
4 Replies

10. Shell Programming and Scripting

nawk problem

How are ya, Heres the problem. I have a line of data that can either be in this format. "20" or "20kg" for the 20kg one i need to be able to read that their is kg on the end of this field and then ignore it and move on to the next line. Can anyone help. Cheers (3 Replies)
Discussion started by: ben_shark
3 Replies
Login or Register to Ask a Question