How to work with two files with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to work with two files with awk
# 1  
Old 04-24-2008
How to work with two files with awk

I have two files:

The number of lines in both files are always same.

I could get these specific lines from a huge data file, but what I want to do now is take first line of file1 which is 1 and print first line of file2 which is 'a' one time, similarly letter 'b' from file2 corresponding to file1 2 times and so on. Any suggestions or hints?.

awk 'NR==1' file1 | print $1== ---not sure here

Any help?.

file1:
1
2
3
4
5
6

file2:
a
b
c
d
e
f
# 2  
Old 04-24-2008
awk 'BEGIN{ while ( getline < "secondfile" ) { arr[i++]=$0 } }{ print arr[j++], $1 }' firstfile
# 3  
Old 04-24-2008
or try like this

nawk 'BEGIN {

while ( getline < "f1" ) {arr[i++] = $0 }
}

{ print arr[j++], $0 }' f2
# 4  
Old 04-24-2008
I'm not sure what you want to do, perheaps :
Code:
awk '
NR==FNR {
   arr[FNR] = $1;
   next;
}
{
   for (i=1; i<=arr[FNR]; i++)
      print $0;
}
' f1 f2

Output:
Code:
a
b
b
c
c
c
d
d
d
d
e
e
e
e
e
f
f
f
f
f
f

Jean-Pierre.
# 5  
Old 04-24-2008
or try like this

nawk 'BEGIN {

while ( getline < "f1" ) {arr[i++] = $0 }
}

{ print arr[j++], $0 }' f2

====> In this case it just prints both lines 1 and 2




awk '
NR==FNR {
arr[FNR] = $1;
next;
}
{
for (i=1; i<=arr[FNR]; i++)
print $0;
}
' f1 f2

===> it does not print anything at all

Then I tried


awk '
NR==FNR {
arr[FNR] = $1;
next;
}
{
for (i=1; i<=arr[FNR]; i++) }
{ print $0;
}
' f1 f2

===> now it prints f1 and f2...does not print the way I want the output to be. Any comments?. Am I missing anything?
# 6  
Old 04-24-2008
do u want output like this

$ cat f1
1
2
3
4
5
6

$ cat f2
a
b
c
d
e
f

output below

$ 2files.s
1 a
2 b
3 c
4 d
5 e
6 f
# 7  
Old 04-24-2008
Quote:
Originally Posted by calsum
Then I tried


awk '
NR==FNR {
arr[FNR] = $1;
next;
}
{
for (i=1; i<=arr[FNR]; i++) }
{ print $0;
}
' f1 f2

===> now it prints f1 and f2...does not print the way I want the output to be. Any comments?. Am I missing anything?
I am very surprised, how can you ge a result since the awk program is invalid:
Code:
$ awk '
> NR==FNR {
> arr[FNR] = $1;
> next;
> }
> {
> for (i=1; i<=arr[FNR]; i++) }
> { print $0;
> }
> ' f1 f2
awk: cmd. line:7: for (i=1; i<=arr[FNR]; i++) }
awk: cmd. line:7:                             ^ syntax error
$

Please show us the required output.

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

This awk should work, shouldn't it?

Heyas Trying to parse a tempfile, but somehow i mess up. To my understand, this should work... Plain: tail -n1 out.tmp 1 81.5M 1 1066k 0 0 359k 0 0:03:52 0:00:02 0:03:50 359k I want to get the 81.5M, so i'd assume it'll be $2 for awk.... tail -n1 out.tmp | awk... (24 Replies)
Discussion started by: sea
24 Replies

2. Shell Programming and Scripting

Why my awk doesn't work?

root@SDP_Wildcat_Pass-3-C1:~# cat /proc/driver/rtc rtc_time : 05:29:40 rtc_date : 2014-12-19 alrm_time : 01:51:53 alrm_date : 2014-12-20 alarm_IRQ : no alrm_pending : no update IRQ enabled : no periodic IRQ enabled : no periodic IRQ... (4 Replies)
Discussion started by: yanglei_fage
4 Replies

3. UNIX for Dummies Questions & Answers

Why does this awk statement work?

So, I have an awk statement that does a little filtering and formats the output conveniently. Here's what I had originally: <input> | awk -F "\t" 'BEGIN{OFS=","} {sub(" ","_",$2)} (NR == 1) || (substr($2,9,2) >= 19 && substr($2,1,7) == "2011-02") {print}' That did what I wanted, except that... (2 Replies)
Discussion started by: treesloth
2 Replies

4. HP-UX

awk don't work in hp-ux 11.11

Hello all! I have problem in hp-ux 11.11 in awk I want to grep sar -d 2 1 only 3 column, but have error in awk in hp-ux 11.11 Example: #echo 123 234 | awk '{print $2}' 123 234 The situattions in commands bdf | awk {print $5}' some... In hp-ux 11.31 - OK! How resolve problem (15 Replies)
Discussion started by: ostapv
15 Replies

5. Shell Programming and Scripting

Parse file using awk and work in awk output

hi guys, i want to parse a file using public function, the file contain raw data in the below format i want to get the output like this to load it to Oracle DB MARWA1,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 MARWA2,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 this the file raw format: Number of... (6 Replies)
Discussion started by: dagigg
6 Replies

6. Shell Programming and Scripting

awk: assign variable with -v didn't work in awk filter

I want to filter 2nd column = 2 using awk $ cat t 1 2 2 4 $ VAR=2 #variable worked in print $ cat t | awk -v ID=$VAR ' { print ID}' 2 2 # but variable didn't work in awk filter $ cat t | awk -v ID=$VAR '$2~/ID/ { print $0}' (2 Replies)
Discussion started by: honglus
2 Replies

7. Shell Programming and Scripting

awk -v -- Why doesn't my example work?

Hi. I've been playing around a bit. This isn't for any practical purpose-- it's really just a theoretical exercise. I wrote this little thing: foreach num ( 6 5 4 ) awk -v "number=$num" 'BEGIN{for(x=0;x<$number;x++) printf "-"; printf "\n"}' end I would expect the following output: ... (3 Replies)
Discussion started by: treesloth
3 Replies

8. Shell Programming and Scripting

NEED HELP (AWK or anything that would work)

Hi guys, heres my first post..... Input.txt: <abc a="" b="" c="" > <error x="" y="" z="" /> </abc> <abc a="" e="" c="" > ... (10 Replies)
Discussion started by: qzv2jm
10 Replies

9. Shell Programming and Scripting

why awk does not work here?

I am trying to find any line with the 9th column's number greater than 200, but why the following awk command does not work? awk '$9 > 200' /tmp/test 2007-09-05 10:13:05.714 640.847 any 1.2.3.4 719 2445 487260 32 6082 199 2007-09-05 10:13:02.686 641.827... (2 Replies)
Discussion started by: fedora
2 Replies

10. UNIX for Dummies Questions & Answers

awk may work!

:) have you tried awk... and pipe the actual start and end dates in the directory you're looking for when i go through my directories and look for certain matching files thats what i do except I am not quite sure what you are asking for so I can't give an exact example awk -f script file |... (0 Replies)
Discussion started by: moxxx68
0 Replies
Login or Register to Ask a Question