2016-12-13 59 views
2

我有ID的列表的文件如下grep的每一行

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 

現在,我想提取蘋果的第一次出現第一次出現|在每一行,給我

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 

回答

3

如果awk是好的爲您提供:

保存輸入線爲樣本.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)等於 「蘋果|」或不。如果它是由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

桑達版本

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