awk command - column merging

I have two files having 3 coulms and 1 column respectively

file1.txt
0    22.89    35.60
10    22.80    35.61
20    22.70    35.63
30    22.32    35.68
50    19.23    35.79
75    16.10    35.59
100    15.00    35.52
125    14.45    35.46
150    13.91    35.41
200    12.94    35.28
file 2
1575.15
1574.68
1574.16
1571.7
1550.89
1530.54
1523.9
1520.79
1517.8
1512.52
1506.69
1500.99
1491.52

required : I want to merge column 1 from file1 with coumn 1 of file2 >> new file. but in file 2 i have more rows than required.

In my new file i want equal number of rows as in file 1
(no of rows in file 1 varies everytime)

output file
0      1575.15
10    1574.68
20    1574.16
30    1571.7
50    1550.89
75    1530.54
100  1523.9
125  1520.79
150  1517.8
200  1512.52

No of rows should always be equal to number of rows in file 1 (file 2 will always have more rows than required)

Try this:

awk 'NR==FNR{a[NR]=$1; next}FNR in a{print a[FNR], $0}' file1 file2
1 Like

The command works good. I have few quries regarding the command

1> what does FNR do?.
2> "next" ??
3> FNR in a ??? ->
4> can u explain the syntax used......the

1> what does FNR do? The FNR variable contains the number of lines read, but is reset for each file read

2> "next" ?? Read the next line and start with the first command

3> FNR in a ??? -> if the line number of the 2nd file matches with the line number of the first file

4> can u explain the syntax used......the have read of The GNU Awk User's Guide

If awk seems a complicated topic , then u can try this below code

# !/usr/bin/sh

while read LINE
do
     echo $LINE | cut -d " " -f 1 >> file4.txt
done < file.txt

count=`wc -l file4.txt | cut -d " " -f 1`
paste file4.txt file1.txt | head -$count
paste file1 file2 | head -$(wc -l <file1)
paste file1 file2  | awk '/^[0-9]/{print $1,$4}'
awk '{a=$1; getline < "file2.txt";print a,$0}' file1.txt