0

I have a list of strings, say file1.txt:

a
B
ccc
    D
   E
 f

and another list of strings, i.e. file2.txt:

    a x y z
  43 5 B aa_f
    <|ccc
 |D>
    E
                            FFF

I want to check, for each line, that file2.txt contains the trimmed string (so no whitespaces around) contained in the respective line of file1.txt. For example, line 2 in both files contains B, so this test should evaluate to true. However, the last line in both files are not in the substring relation, since file1.txt contains f and file2.txt does not have any f character in that line.

Kusalananda
  • 333,661

2 Answers2

1

Assuming you have no whitespace contained in your strings in file1.txt, the following awk-based approach should work:

awk 'NR==FNR{patterns[FNR]=$1} FNR<NR{if (index($0,patterns[FNR])>0) print "true"; else print "false"}' file1.txt file2.txt

For your example, this will yield

true
true
true
true
true
false

Some explanation: We use awk to read in both files, but process them in a different way.

  • While processing file1.txt, indicated by FNR, the "per-file line counter", being equal to NR, the global line counter, we simply register all (trimmed) strings ($1, which is the first whitespace-delimited field of the line) in an awk-internal array, with the line number as index (note that these start with 1).

  • While processing file2.txt (FNR is now smaller than NR), we use the index function to look for the string patterns[FNR] in the entire input line ($0). If so, index() will return a start position larger than 0, and we print true, otherwise we print false.

AdminBee
  • 22,803
0
awk 'ARGIND == 1 { a[NR]=$1;     next }
     $0 ~ a[FNR] { print "true"; next }
                 { print "false"      }' ex1 ex2

In Awk, I prefer to write programs in a more "cond {action}" structure. This is very similar to @AdminBee solution.

  • ARGIND ==1 - if this is the first argument: save the first word
  • $0 ~ a[FNR]- if this line ($0) contains the saved homologous word, "true"; else "false"
JJoao
  • 12,170
  • 1
  • 23
  • 45