0

I have a pipe delimited file

Lookup.txt:

Column1|yellow|value|628291
Column9|red|value|346801

File.txt:

Yellow
YellowDiamond
Redgold

I am trying to get the value of Yellow present In File.txt to the exact match from pipe delimited file I.e Lookup.txt. The output should be the entire line as shown below. File.txt file are having the values to search in the file Lookup.txt

I tried awk, fgrep commands but they are giving me different values; The expected output is:

Column1|yellow|value|628291
2
  • 3
    Show us what you try. May 22, 2022 at 14:01
  • 1
    Should the match be limited to a specific field of the pipe-delimited file? Should it be case-insensitive, as implied by your example? May 22, 2022 at 14:34

2 Answers 2

2

Any reason why your grep approach didn't work?

grep -Fwif File.txt Lookup.txt

looks promising...?

1
  • 1
    It IS working with the sample data you posted.
    – RudiC
    May 23, 2022 at 7:59
1

Store the strings from File.txt as keys in an associative array in awk. For case-insensitive comparison, fold the keys to, e.g., lower-case.

When the second file, Lookup.txt, is being read, test whether the second field's lower-cased value is a key in the associative array. If it is, print the whole line.

awk -F '|' 'NR == FNR { key[tolower($0)]; next } (tolower($2) in key)' File.txt Lookup.txt

The variables NR and FNR hold the number of lines read overall and the number of lines read from the current file, respectively. If the values of these variables are the same, we're reading from the first file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.