Skip to main content
added 7 characters in body
Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

Another awk solution, which assumes that the lines from src will be used onlyexactly once each and in order. This allows us to only keep track of the next line from src until it has been used, and then read the next one in.

awk -F '|' ' BEGIN { OFS=FS } ! have { getline line <"src" split(line, pat) have = 1 } $1 == pat[1] { if ($4 == pat[2]) $4 = pat[3] have = 0 }; 1' dest 

If the flag have is not set, or zero, the next line from src is read into line and split up into the array pat. This is done in the ! have block.

If the current line of input from dest has a 1st field that is identical to the first element of pat, then we'll test the 4th field against pat[2] and replace it with pat[3] if they are the same. The have flag is then reset to zero to trigger reading a new line in from src.

The trailing 1 at the end of the awk program causes the (possibly modified) record to be outputted.

The output, given the data in the question:

ID|NAME|COMPANY|NUMBER 1001|Adam||15001 1002|eve|adam&eve|15002 1003|||15003 1004|||15004 1005|||15005 

Another awk solution assumes that the lines from src will be used only once each and in order. This allows us to only keep track of the next line from src until it has been used, and then read the next one in.

awk -F '|' ' BEGIN { OFS=FS } ! have { getline line <"src" split(line, pat) have = 1 } $1 == pat[1] { if ($4 == pat[2]) $4 = pat[3] have = 0 }; 1' dest 

If the flag have is not set, or zero, the next line from src is read into line and split up into the array pat. This is done in the ! have block.

If the current line of input from dest has a 1st field that is identical to the first element of pat, then we'll test the 4th field against pat[2] and replace it with pat[3] if they are the same. The have flag is then reset to zero to trigger reading a new line in from src.

The trailing 1 at the end of the awk program causes the (possibly modified) record to be outputted.

The output, given the data in the question:

ID|NAME|COMPANY|NUMBER 1001|Adam||15001 1002|eve|adam&eve|15002 1003|||15003 1004|||15004 1005|||15005 

Another awk solution, which assumes that the lines from src will be used exactly once each and in order. This allows us to only keep track of the next line from src until it has been used, and then read the next one in.

awk -F '|' ' BEGIN { OFS=FS } ! have { getline line <"src" split(line, pat) have = 1 } $1 == pat[1] { if ($4 == pat[2]) $4 = pat[3] have = 0 }; 1' dest 

If the flag have is not set, or zero, the next line from src is read into line and split up into the array pat. This is done in the ! have block.

If the current line of input from dest has a 1st field that is identical to the first element of pat, then we'll test the 4th field against pat[2] and replace it with pat[3] if they are the same. The have flag is then reset to zero to trigger reading a new line in from src.

The trailing 1 at the end of the awk program causes the (possibly modified) record to be outputted.

The output, given the data in the question:

ID|NAME|COMPANY|NUMBER 1001|Adam||15001 1002|eve|adam&eve|15002 1003|||15003 1004|||15004 1005|||15005 
Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

Another awk solution assumes that the lines from src will be used only once each and in order. This allows us to only keep track of the next line from src until it has been used, and then read the next one in.

awk -F '|' ' BEGIN { OFS=FS } ! have { getline line <"src" split(line, pat) have = 1 } $1 == pat[1] { if ($4 == pat[2]) $4 = pat[3] have = 0 }; 1' dest 

If the flag have is not set, or zero, the next line from src is read into line and split up into the array pat. This is done in the ! have block.

If the current line of input from dest has a 1st field that is identical to the first element of pat, then we'll test the 4th field against pat[2] and replace it with pat[3] if they are the same. The have flag is then reset to zero to trigger reading a new line in from src.

The trailing 1 at the end of the awk program causes the (possibly modified) record to be outputted.

The output, given the data in the question:

ID|NAME|COMPANY|NUMBER 1001|Adam||15001 1002|eve|adam&eve|15002 1003|||15003 1004|||15004 1005|||15005