With paste
+awk
:
$ paste -d'\n' jobname.txt value.txt \
|awk '{ print (NR%2?"update_job:":"max_run_alarm:", $0) }'
Basically with the paste
command as its name is saying, we paste both files line by line from each but adds a single \n
ewline character between each lines, then we re-process the previous output with awk
and we prepend desired "text" for the lines which their line number is module of 2 (lines with even line numbers; for example line numbers 2, 4, 6, ...; the NR
contains the line number of a line that awk is read for the processing) we prepend text "update_job:" or for odd line numbers we prepend text "max_run_alarm:", then print the line itself $0
.
With just awk
:
awk 'NR==FNR{
getline val<ARGV[2]; print "update_job:", $0 ORS "max_run_alarm:", val; next
}
{ exit }' jobname.txt value.txt
Here we process differently, above we said NR is the line number, here you see FNR; NR and FNR has always the same value (i.e, they both contains the line number) but NR keep incrementing for all the lines of all input files, while FNR reset back to 1 as soon as the next input file was read (if any), so by saying NR==FNR
we are ensuring that the following block of the code only runs for the first input file.
then we call "getline
into a Variable from a File" to read a single line from the second argument we passed as the input (here ARGV[2]
return value.txt
) and save it into val
variable; then the rest is just printing things. ORS prints a line break between each.
or maybe you prefer this one (but I don't like hard-coding fileName):
awk '{
getline val<"value.txt"; print "update_job:", $0 ORS "max_run_alarm:", val
}' jobname.txt