首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为一个文件中的100个条目标记一个值

为一个文件中的100个条目标记一个值
EN

Stack Overflow用户
提问于 2020-09-03 10:18:36
回答 4查看 73关注 0票数 1

我有一个像下面这样的文件

代码语言:javascript
复制
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

现在,它将返回所有条目,而是只需要打印一次。

输出应该类似于

代码语言:javascript
复制
apple
lime
banana

尝试了排序uniq,但是都没有多大帮助。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-09-03 10:34:07

grep -o | sort -u可能对您有用,但是如果输入量很大,那么它可能不是很有效。

gnu-awk可能更适合您:

代码语言:javascript
复制
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

}' file
代码语言:javascript
复制
apple
lime
banana

为了学术兴趣,这里有grep + sort命令:

代码语言:javascript
复制
grep -owE 'apple|lime|banana' file | sort -u
票数 2
EN

Stack Overflow用户

发布于 2020-09-03 11:37:38

请您试着用GNU awk中显示的示例进行以下、编写和测试。

代码语言:javascript
复制
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

解释:添加了上面的详细说明。

代码语言:javascript
复制
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.
票数 2
EN

Stack Overflow用户

发布于 2020-09-03 12:14:54

这可能对您有用(GNU sed):

代码语言:javascript
复制
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 lines
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63721578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档