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.

3
  • Does the second file contain ccc or D, or are you only interested in strings delimited by whitespace?
    – Kusalananda
    Feb 3 '20 at 7:45
  • Are all strings in file1.txt free from "internal" whitespace?
    – AdminBee
    Feb 3 '20 at 7:47
  • So you want as a result a file of five line true and one line false?
    – Philippos
    Feb 3 '20 at 7:47
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.

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"

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.