我有一个像下面这样的文件
apple lime apple lime apple lime jackfruit papaya apple banana carrot jackfruit papaya banana
apple lime apple lime apple lime jackfruit papaya apple banana carrot jackfruit papaya banana
apple lime apple lime apple lime jackfruit papaya apple banana carrot jackfruit papaya banana现在我只想从档案里得到苹果、酸橙和香蕉。
我用grep找到的
grep‘苹果酸橙香蕉’fruits.txt
现在,它将返回所有条目,而是只需要打印一次。
输出应该类似于
apple
lime
banana尝试了排序uniq,但是都没有多大帮助。
发布于 2020-09-03 10:34:07
grep -o | sort -u可能对您有用,但是如果输入量很大,那么它可能不是很有效。
此gnu-awk可能更适合您:
awk -v s='apple lime banana' -v RS='[[:space:]]+' 'BEGIN {
split(s, a)
for (i in a)
wrds[a[i]]
}
$0 in wrds {
print
delete wrds[$0]
if (length(wrds) == 0)
exit
}' fileapple
lime
banana为了学术兴趣,这里有grep + sort命令:
grep -owE 'apple|lime|banana' file | sort -u发布于 2020-09-03 11:37:38
请您试着用GNU awk中显示的示例进行以下、编写和测试。
awk -v s1="apple lime banana" -v RS='[[:space:]]+' '
BEGIN{
num=split(s1,arr," ")
for(i=1;i<=num;i++){
wordArr[arr[i]]
}
}
($0 in wordArr) && !count[$0]++
' Input_file解释:添加了上面的详细说明。
awk -v s1="apple lime banana" -v RS='[[:space:]]+' ' ##Starting awk program from here, creating variable s1 which has all words which you want to match and get unique values. Then setting record separator as space here.
BEGIN{ ##Starting BEGIN section of this program from here.
num=split(s1,arr," ") ##Splitting s1 into array arr with field separator as a space.
for(i=1;i<=num;i++){ ##Running for loop from i=1 to value of num here.
wordArr[arr[i]] ##Creating wordArr which has index of value of arr with index i here.
}
}
($0 in wordArr) && !count[$0]++ ##Checking condition if current line is present in wordArr and its NOT present in count then print the current line.
' Input_file ##Mentioning Input_file name here.发布于 2020-09-03 12:14:54
这可能对您有用(GNU sed):
sed -E 's/(((apple|banana|lime) ?)|\<\w+\> ?)/\2/g; # keep only the required words
H; # copy remains to hold space
$!d; # delete all lines except last
x; # last line swap to hold space
s/\n/ /g; # separate words by spaces
:a;s/(( \w+).*)\2/\1/;ta; # remove duplicate words
s/.//; # remove leading space
s/ /\n/g' file # separate words by new lineshttps://stackoverflow.com/questions/63721578
复制相似问题