Skip to main content
Format code and data.
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

I have a text file shown below:

Doc_A 123 abc Doc_A 456 def Doc_A 789 ghi Doc_B 123 abc Doc_B 456 def Doc_C 123 abc Doc_C 456 def Doc_C 789 ghi Doc_C 101 jkl

Doc_A 123 abc
Doc_A 456 def
Doc_A 789 ghi
Doc_B 123 abc
Doc_B 456 def
Doc_C 123 abc
Doc_C 456 def
Doc_C 789 ghi
Doc_C 101 jkl

And a reference file

Doc_A Doc_B Doc_C Doc_D Doc_E Doc_F

Doc_A
Doc_B
Doc_C
Doc_D
Doc_E
Doc_F

I want to extract the first line from text file that matches the name in the reference file and print that line and if there is no match print a certain fixed pattern as shown:

Doc_A 123 abc Doc_B 123 abc Doc_C 123 abc Doc_D 10 20 Doc_E 10 20 Doc_F 10 20

Doc_A 123 abc
Doc_B 123 abc
Doc_C 123 abc
Doc_D 10 20
Doc_E 10 20
Doc_F 10 20

I can use awk as shown below to print for matching pattern. How would I print patterns not found, certain fixed way as I require?

awk 'FNR == NR { a[$1] = 0; } FNR != NR { for (i in a) if ($0 ~ i && a[i]++ == 0) { print $0; break; } }' \ref.txt file.txt

awk 'FNR == NR { a[$1] = 0; } FNR != NR { for (i in a) if ($0 ~ i && a[i]++ == 0) { print $0; break; } }' \ref.txt file.txt

I have a text file shown below:

Doc_A 123 abc Doc_A 456 def Doc_A 789 ghi Doc_B 123 abc Doc_B 456 def Doc_C 123 abc Doc_C 456 def Doc_C 789 ghi Doc_C 101 jkl

And a reference file

Doc_A Doc_B Doc_C Doc_D Doc_E Doc_F

I want to extract the first line from text file that matches the name in the reference file and print that line and if there is no match print a certain fixed pattern as shown:

Doc_A 123 abc Doc_B 123 abc Doc_C 123 abc Doc_D 10 20 Doc_E 10 20 Doc_F 10 20

I can use awk as shown below to print for matching pattern. How would I print patterns not found, certain fixed way as I require?

awk 'FNR == NR { a[$1] = 0; } FNR != NR { for (i in a) if ($0 ~ i && a[i]++ == 0) { print $0; break; } }' \ref.txt file.txt

I have a text file shown below:

Doc_A 123 abc
Doc_A 456 def
Doc_A 789 ghi
Doc_B 123 abc
Doc_B 456 def
Doc_C 123 abc
Doc_C 456 def
Doc_C 789 ghi
Doc_C 101 jkl

And a reference file

Doc_A
Doc_B
Doc_C
Doc_D
Doc_E
Doc_F

I want to extract the first line from text file that matches the name in the reference file and print that line and if there is no match print a certain fixed pattern as shown:

Doc_A 123 abc
Doc_B 123 abc
Doc_C 123 abc
Doc_D 10 20
Doc_E 10 20
Doc_F 10 20

I can use awk as shown below to print for matching pattern. How would I print patterns not found, certain fixed way as I require?

awk 'FNR == NR { a[$1] = 0; } FNR != NR { for (i in a) if ($0 ~ i && a[i]++ == 0) { print $0; break; } }' \ref.txt file.txt
Source Link

How to extract first line that matches pattern from a file and if pattern doesn't exist print fixed output?

I have a text file shown below:

Doc_A 123 abc Doc_A 456 def Doc_A 789 ghi Doc_B 123 abc Doc_B 456 def Doc_C 123 abc Doc_C 456 def Doc_C 789 ghi Doc_C 101 jkl

And a reference file

Doc_A Doc_B Doc_C Doc_D Doc_E Doc_F

I want to extract the first line from text file that matches the name in the reference file and print that line and if there is no match print a certain fixed pattern as shown:

Doc_A 123 abc Doc_B 123 abc Doc_C 123 abc Doc_D 10 20 Doc_E 10 20 Doc_F 10 20

I can use awk as shown below to print for matching pattern. How would I print patterns not found, certain fixed way as I require?

awk 'FNR == NR { a[$1] = 0; } FNR != NR { for (i in a) if ($0 ~ i && a[i]++ == 0) { print $0; break; } }' \ref.txt file.txt