0
votes

I'm trying to parse file2 for record 23 and use it to replace file1's record 23.

This is the command I'd like to run each time.

script.awk file1 file2

Here is an example of record 23 for either file. Same delimiters and number of digits in each value as well as having the same first field string.

currentmax: 1.7229 1.6888 1.1069 1.6238 

This would then be the first section of a larger awk script. I would add more to this script to work only with the new values in file1 and output to file3 but that would be for later.

I'm not sure if a different file order in the command would make the script easier to write and then manipulate file1 until output to file3. Switching file1 and file2 may work.

script.awk file2 file1
3
May be it's just me but can you elaborate more on this? Like show us some content for file1 and file2 and you end result.jaypal singh

3 Answers

5
votes

You'll need to specify file2 first, because it has the data you want to capture.

awk has a couple of built-in variables that will help you. FNR is the record number of the current file being processed. NR is the record number of all lines seen so far.

To capture record 23 from file2 and replace file1's record 23:

awk '
  NR == FNR {
    # this block is processing the first file
    if (FNR == 23) line = $0
    next
  }
  FNR == 23 {$0 = line}
  {print}
' file2 file1 > file3
0
votes

I would do that in a separate command. It should be as easy as for example

{ sed -e '23,$d' file1; sed -n -e '23p' file2; sed -e '1,23d' file1; } > newfile
mv newfile file1
0
votes

This might work for you:

l=23; a=c # l=line number a=c to change, a=i to insert or a=a to append
sed "$l"'s/.*/'"$l$a"' &/p;d' file2 | sed -i -f - file1