我有很多的用戶列表,我想只匹配一個詞的那些在他們拉從文本一個字匹配文件名的文件
**txt1.txt**
charles
first
charles users
user sample
sample
**txt2.txt**
mike
charles users
charles
welcome
first
我使用grep來查找文件匹配
grep -xF -f txt1.txt txt2.txt
爲了只匹配單個字母,我需要添加或使用什麼?
我有很多的用戶列表,我想只匹配一個詞的那些在他們拉從文本一個字匹配文件名的文件
**txt1.txt**
charles
first
charles users
user sample
sample
**txt2.txt**
mike
charles users
charles
welcome
first
我使用grep來查找文件匹配
grep -xF -f txt1.txt txt2.txt
爲了只匹配單個字母,我需要添加或使用什麼?
它與AWK容易得多:
awk 'FNR==NR {a[$1];next} $0 in a' txt1.txt txt2.txt
charles
first
a[$1]
做是爲了確保只有第一個字用於數組$0 in a
用於確保file2的完整字符串與file1的字符串匹配謝謝,非常有幫助 – user3297875 2014-10-09 22:20:42
不客氣,很高興它解決了。 – anubhava 2014-10-09 22:25:43
雖然我明白了,你能做到這一點與
join <(awk NF==1 txt1.txt | sort -u) <(awk NF==1 txt2.txt | sort -u)
你能做到這樣
awk NF==1 txt1.txt txt2.txt | sort | uniq -c | awk '$1==2{print $2}'
我覺得hek2mgl答案匹配所有的話,但沒有一個字線。繼承人的行版本:
grep -o '^[^\s]+$' file1 file2 ...
什麼是您預期的輸出? – anubhava 2014-10-09 22:04:50
查爾斯和第一個,他們是在這兩個文件中的單個字母的單詞 – user3297875 2014-10-09 22:08:24