How to read a value on a specific line in an external file into an AWK script

130 views Asked by At

Input file:

2
5
7
1
2

Modifier file (external file):

4
6
2
7
9

What i want to achieve is, summarizing line1 from input file with line1 from an external file.

AWK script:

sum=$1+[value of line 1 on modifier file];
printf("%s\n", sum);

expected output:

6
11
9
8
11
3

There are 3 answers

2
quelotic On

Layed down simply what OP wants to do is:

say you want your output file to be like this:

words     number1
texts     number2
stuff     number3
things    number4

in which case the external file has 4 lines of text which are:

number1
number2
number3
number4

and the awk script is

printf("words ","line one of external file")
printf("texts ","line two of external file")
printf("stuff ","line three of external file")
printf("things ","line four of external file")
3
Ed Morton On
$ awk 'NR==FNR{m[NR]=$0;next} {print $0+m[FNR]}' modifierFile inputFile
6
11
9
8
11
2
Mikkel Christiansen On

You are looking for

getline valueline < $modfile