AWK:Combining two files using array and if


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK:Combining two files using array and if
# 1  
Old 08-23-2011
AWK:Combining two files using array and if

I want to create an array containing each line in a file. Then I want to read a different file and print array[1] and the lines of that file until I reach a record separator. Now I want to print array[2] and the lines of said file until next record separator and so on.

The file I want to get into the array looks like this:
Code:
4110 639069.51 885931.51 R04-101_2
4190 638784.51 885521.01 R04-101_2
4270 638499.01 885110.01 R04-101_2

...and so on.

The second file looks like this:
Code:
7773 2405
8927 2881
9600 3163
RS
3279 1500
3417 1502
3603 1524
RS
4889 1725
5229 1759
5412 1805

...and so on.

I am pretty new to AWK, for loops and if conditions. It seems I made a infinite loop with my first "working" script.

Here is what I came up with:
Code:
#!/bin/ksh
awk ' BEGIN {
 
for ( x=0 ; ++x ; getline < "HH_OUT_array.iseb" > 0 )
myarr[$0]=x
}
{x=1}
{
if( $1 ==/[:digit:]/ )
printf($0, myarr[x])
else if ($0 =="RS")
++x
}' testTVEL_OUT.iseb

This is probably complete rubbish as it is a rough stitch job of simple simmilar scripts I found while trying to figure it out. But I'd be espacially grateful if someone can point out where and why im going wrong.

Thanks

Last edited by RocketLawnChair; 08-23-2011 at 10:51 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 08-23-2011
From the sample input you have provided , what is the output you are expecting?
# 3  
Old 08-24-2011
The output should look like this:
Code:
4110 639069.51 885931.51 R04-101_2 7773 2405
4110 639069.51 885931.51 R04-101_2 8927 2881
4110 639069.51 885931.51 R04-101_2 9600 3163
4190 638784.51 885521.01 R04-101_2 3279 1500
4190 638784.51 885521.01 R04-101_2 3417 1502
4190 638784.51 885521.01 R04-101_2 3603 1524
4270 638499.01 885110.01 R04-101_2 4889 1725
4270 638499.01 885110.01 R04-101_2 5229 1759
4270 638499.01 885110.01 R04-101_2 5412 1805

The files I am merging are of course much bigger.

Last edited by vbe; 08-24-2011 at 08:46 AM.. Reason: please use code tags for your code and data! Thanks
# 4  
Old 08-24-2011
Try as
Code:
awk -vj=0 -vk=0 'NR==FNR{a[i++]=$0;next}{printf j<=2?a[k]" "$0"\n":"";++j}/RS/{++k;j=0}' inputfile1 inputfile2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combining echo and awk

i have a script that has many lines similar to: echo $var | awk -F"--" '{print $2}' as you can see, two commands are being run here. echo and awk. id like to combine this into one awk statement. i tried: awk -F"--" "BEGIN{print $var; print $2}" but i get error messages. (10 Replies)
Discussion started by: SkySmart
10 Replies

2. Shell Programming and Scripting

awk problem - combining awk statements

i have a datafile that has several lines that look like this: 2,dataflow,Sun Mar 17 16:50:01 2013,1363539001,2990,excelsheet,660,mortar,660,4 using the following command: awk -F, '{$3=strftime("%a %b %d %T %Y,%s",$3)}1' OFS=, $DATAFILE | egrep -v "\-OLDISSUES," | ${AWK} "/${MONTH} ${DAY}... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. UNIX for Dummies Questions & Answers

Need Help in reading N days files from a Directory & combining the files

Hi All, Request your expertise in tackling one requirement in my project,(i dont have much expertise in Shell Scripting). The requirement is as below, 1) We store the last run date of a process in a file. When the batch run the next time, it should read this file, get the last run date from... (1 Reply)
Discussion started by: dsfreddie
1 Replies

4. Shell Programming and Scripting

awk, associative array, compare files

i have a file like this < '393200103052';'H3G';'20081204' < '393200103059';'TIM';'20110111' < '393200103061';'TIM';'20060206' < '393200103064';'OPI';'20110623' > '393200103052';'HKG';'20081204' > '393200103056';'TIM';'20110111' > '393200103088';'TIM';'20060206' Now i have to generate a file... (9 Replies)
Discussion started by: shruthi123
9 Replies

5. Shell Programming and Scripting

Combining AWK statements

Hello UNIX Community, I have file that contains the following data: testAwk2.csv rabbit penguin goat giraffe emu ostrich hyena elephant panda dog cat pig lizard snake antelope platypus tiger cheetah lion rhino spider I then find the character length of the... (1 Reply)
Discussion started by: vnayak
1 Replies

6. Shell Programming and Scripting

Use Awk and Array to get total size of files

Hello all, I need to do scripts total up the size in selected extension file for example motion.mov and segmentation.avi is in Label Media. For file info.doc and calc.xls in Label Document. I need output will be like this: count 1 Media,,2 GB count 2 Document,,4 GB My problem is,... (16 Replies)
Discussion started by: sheikh76
16 Replies

7. Shell Programming and Scripting

Combining awk statements

I have a pretty simple script below: #!/bin/sh for i in *.cfg do temp=`awk '/^InputDirectory=/' ${i}` input_dir=`echo ${temp} | awk '{ print substr( $0, 16) }'` echo ${input_dir} done As you can see its opening each cfg file and searching for the line that has "InputDirectory="... (3 Replies)
Discussion started by: ssbsts
3 Replies

8. Shell Programming and Scripting

Combining two awk scripts

I have a file like this consisting of blocks separated by > of two number X and T > 10 0 13 5.92346 16 10.3106 19 13.9672 22 16.9838 25 19.4407 28 21.4705 31 23.1547 34 24.6813 37 26.0695 40 27.3611 43 28.631 46 29.8366 49 30.9858 52 32.0934 55 33.1458 (6 Replies)
Discussion started by: kristinu
6 Replies

9. Shell Programming and Scripting

AWK Matching Fields and Combining Files

Hello! I am writing a program to run through two large lists of data (~300,000 rows), find where rows in one file match another, and combine them based on matching fields. Due to the large file sizes, I'm guessing AWK will be the most efficient way to do this. Overall, the input and output I'm... (5 Replies)
Discussion started by: Michelangelo
5 Replies

10. Shell Programming and Scripting

help combining lines in awk

I seem to have gotten myself in over my head on this one. I need help combining lines together. I have a text file containing 24,000 lines (exactly why I need awk) due to bad formatting it has separated the lines (ideally it should be 12,000 lines total). Example of file: ... (2 Replies)
Discussion started by: blueheed
2 Replies
Login or Register to Ask a Question