Grep在每一行中首次出现

时间:2016-12-13 23:10:14

标签: regex awk grep

我有一个带有ID列表的文件,如下所示

OG1: apple|fruits_1 cucumber|veg_1 apple|fruits_1  carrot|veg_2
OG2: apple|fruits_5 cucumber|veg_1 apple|fruits_1  pineapple|fruit_2
OG3: cucumber|veg_1 apple|fruits_9  carrot|veg_2
OG4: apple|fruits_3 cucumber|veg_1 apple|fruits_4  pineapple|fruit_7
OG5: pineapple|fruit_2 pineapple|fruit_2 apple|fruits_1 pineapple|fruit_2
OG6: apple|fruits_5 apple|fruits_1 apple|fruits_6  apple|fruits_7

现在,我想提取第一次出现的apple |在每一行给我

 OG1: apple|fruits_1
 OG2: apple|fruits_5
 OG3: apple|fruits_9
 OG4: apple|fruits_3
 OG5: apple|fruits_1
 OG6: apple|fruits_5

我试过

  grep -w -m 1 "apple" sample.txt

只给了我

  OG1: apple|fruits_1 cucumber|veg_1 apple|fruits_1  carrot|veg_2

2 个答案:

答案 0 :(得分:3)

如果awk适合您:

将输入行保存到sample.csv文件中。

 awk '{for(x=1;x<=NF;x++){if(substr($x,0,6)=="apple|"){print $1, $x; next}}}' sample.csv
  • 使用for循环来迭代每行的字段
  • 检查子串substr($x, 0, 6)是否等于“apple |”或不。如果按print $1, $x打印字段并使用next忽略当前行的其余字段

输出:

OG1: apple|fruits_1
OG2: apple|fruits_5
OG3: apple|fruits_9
OG4: apple|fruits_3
OG5: apple|fruits_1
OG6: apple|fruits_5

答案 1 :(得分:1)

Sed版

sed 's/\([[:blank:]]apple|[^[:blank:]]*\).*/\1/;s/:.*[[:blank:]]apple/: apple/;/apple/!d' YourFile

# assuming blank are space
sed 's/\( apple|[^ ]*\).*/\1/;s/:.* apple/: apple/;/apple/!d' YourFile