我知道我要求太多了,但也许你也能帮我解决这个问题。
a.txt包含单词,b.txt包含字符串。
我想知道b.txt中有多少字符串以a.txt中的单词结尾
例子:
文本
apple
peach
potato
文本
greenapple
bigapple
rottenapple
pinkpeach
xxlpotatoxxx
输出
3 apple greenapple bigapple rottenapple
1 peach pinkpeach
我想用grep解决这个问题,因为它比awk快得多。
你们能帮帮我吗?
最佳答案
这里有一个awk
解决方案
awk 'FNR==NR{a[$1]++;next} {for (i in a) {if ($0~i"$") {b[i]++;w[i]=w[i]?w[i] FS $0:$0}}} END {for (j in b) print b[j],j,w[j]}' a.txt b.txt
3 apple greenapple bigapple rottenapple
1 peach pinkpeach
使用
grep
进行此操作将不简单或根本不可能它是如何工作的(不是那么复杂)?
awk '
FNR==NR{ # Run this part for first file (a.txt) only
a[$1]++ # Store it in an array a
next} # Skip to next record
{ # Run this part for file b.txt
for (i in a) { # Loop trough all data in array a
if ($0~i"$") { # Does b.txt have some from array a at the end of it?
b[i]++ # Yes , count it
w[i]=w[i]?w[i] FS $0:$0 # and store the record it found it in in array w
}
}
}
END { # When both file has been read do the END part
for (j in b) # Loop trough all element in array b and
print b[j],j,w[j]} # Print array b, index and array w
' a.txt b.txt # Read the two files