Pull matching names in one word from text files

I have a list of many users, and I would only like to combine them with one word in them

**txt1.txt**
charles
first
charles users
user sample
sample

**txt2.txt**
mike
charles users
charles
welcome
first

      

I am using grep to find matches in files

grep -xF -f txt1.txt txt2.txt

      

What do I need to add or use to match only words with one letter?

+3


source to share


4 answers


It's much easier with awk:

awk 'FNR==NR {a[$1];next} $0 in a' txt1.txt txt2.txt
charles
first

      



  • Array creation is done with a[$1]

    to ensure that only the first word is used in the array
  • $0 in a

    is executed to match the full line from file2 to the full line from file1
+3


source


As I understand it, you can do this with



join <(awk NF==1 txt1.txt | sort -u) <(awk NF==1 txt2.txt | sort -u)

      

0


source


You can do it this way

awk NF==1 txt1.txt txt2.txt | sort | uniq -c | awk '$1==2{print $2}'

      

0


source


I think hek2mgl's answer matches all words, but not single word strings. Here is the line version:

grep -o '^[^\s]+$' file1 file2 ...

      

0


source







All Articles