Skip to main content
Add condition for printing output
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

The answer is rather simple: You use a[$3] to refer to the third column of file1. However

  • you use the array a to store the second column of file1, and never the third column, and
  • you only ever use the first column (the numbers) as "keys", so attempting to access, say, a["info 1"] (as your a[$3] would do on the first line of processing file2) will return nothing.

The following program would do:

awk 'BEGIN{FS=OFS="|"} NR==FNR{d[$1]=$2;t[$1]=$3;next} ($1 in d) {print $2,d[$1],t[$1],$3}' file1.txt file2.txt > file3.txt

This sets | as field separator for input and output.

  • When processing file1.txt, it stores the day in an array d and the time in an array t, with the first column (number) as key.
  • When processing file2.txt, it prints column 2, the date and time corresponding to column 1, and then the "info" value in column 3, using | as output separator.

The answer is rather simple: You use a[$3] to refer to the third column of file1. However

  • you use the array a to store the second column of file1, and never the third column, and
  • you only ever use the first column (the numbers) as "keys", so attempting to access, say, a["info 1"] (as your a[$3] would do on the first line of processing file2) will return nothing.

The following program would do:

awk 'BEGIN{FS=OFS="|"} NR==FNR{d[$1]=$2;t[$1]=$3;next} {print $2,d[$1],t[$1],$3}' file1.txt file2.txt > file3.txt

This sets | as field separator for input and output.

  • When processing file1.txt, it stores the day in an array d and the time in an array t, with the first column (number) as key.
  • When processing file2.txt, it prints column 2, the date and time corresponding to column 1, and then the "info" value in column 3, using | as output separator.

The answer is rather simple: You use a[$3] to refer to the third column of file1. However

  • you use the array a to store the second column of file1, and never the third column, and
  • you only ever use the first column (the numbers) as "keys", so attempting to access, say, a["info 1"] (as your a[$3] would do on the first line of processing file2) will return nothing.

The following program would do:

awk 'BEGIN{FS=OFS="|"} NR==FNR{d[$1]=$2;t[$1]=$3;next} ($1 in d) {print $2,d[$1],t[$1],$3}' file1.txt file2.txt > file3.txt

This sets | as field separator for input and output.

  • When processing file1.txt, it stores the day in an array d and the time in an array t, with the first column (number) as key.
  • When processing file2.txt, it prints column 2, the date and time corresponding to column 1, and then the "info" value in column 3, using | as output separator.
added 67 characters in body
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

The answer is rather simple: You use a[$3] to refer to the third column of file1. However

  • you use the array a to store the second column of file1, and never the third column, and
  • you only ever use the first column (the numbers) as "keys", so attempting to access, say, a["info 1"] (as your a[$3] would do on the first line of processing file2) will return nothing.

The following program would do:

awk 'BEGIN{FS=OFS="|"} NR==FNR{d[$1]=$2;t[$1]=$3;next} {print $2,d[$1],t[$1],$3}' file1.txt file2.txt > file3.txt

This sets | as field separator for input and output.

  • When processing file1.txt, it stores the day in an array d and the time in an array t, with the first column (number) as key.
  • When processing file2.txt, it prints column 2, the date and time corresponding to column 1, and then the "info" value in column 3, using | as output separator.

The answer is rather simple: You use a[$3] to refer to the third column of file1. However

  • you use the array a to store the second column of file1, and never the third column, and
  • you only ever use the first column (the numbers) as "keys", so attempting to access, say, a["info 1"] will return nothing.

The following program would do:

awk 'BEGIN{FS=OFS="|"} NR==FNR{d[$1]=$2;t[$1]=$3;next} {print $2,d[$1],t[$1],$3}' file1.txt file2.txt > file3.txt

This sets | as field separator for input and output.

  • When processing file1.txt, it stores the day in an array d and the time in an array t, with the first column (number) as key.
  • When processing file2.txt, it prints column 2, the date and time corresponding to column 1, and then the "info" value in column 3, using | as output separator.

The answer is rather simple: You use a[$3] to refer to the third column of file1. However

  • you use the array a to store the second column of file1, and never the third column, and
  • you only ever use the first column (the numbers) as "keys", so attempting to access, say, a["info 1"] (as your a[$3] would do on the first line of processing file2) will return nothing.

The following program would do:

awk 'BEGIN{FS=OFS="|"} NR==FNR{d[$1]=$2;t[$1]=$3;next} {print $2,d[$1],t[$1],$3}' file1.txt file2.txt > file3.txt

This sets | as field separator for input and output.

  • When processing file1.txt, it stores the day in an array d and the time in an array t, with the first column (number) as key.
  • When processing file2.txt, it prints column 2, the date and time corresponding to column 1, and then the "info" value in column 3, using | as output separator.
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

The answer is rather simple: You use a[$3] to refer to the third column of file1. However

  • you use the array a to store the second column of file1, and never the third column, and
  • you only ever use the first column (the numbers) as "keys", so attempting to access, say, a["info 1"] will return nothing.

The following program would do:

awk 'BEGIN{FS=OFS="|"} NR==FNR{d[$1]=$2;t[$1]=$3;next} {print $2,d[$1],t[$1],$3}' file1.txt file2.txt > file3.txt

This sets | as field separator for input and output.

  • When processing file1.txt, it stores the day in an array d and the time in an array t, with the first column (number) as key.
  • When processing file2.txt, it prints column 2, the date and time corresponding to column 1, and then the "info" value in column 3, using | as output separator.