2017-07-03 42 views
0

我有一個文本文件是這樣的:用AWK或SED獲得每一行的第一個單詞嗎?

aaaa bbbb cccc 
dddd eeee ffff 
gggg hhhh iiii 
... 
.. 
. 

如何創建只能與awksed這樣每行的第一列中的文本文件?

aaaa 
dddd 
gggg 
... 
.. 
. 

我看過類似的話題,但我無法解決我的問題!

+2

[打印只有第一場的可能的複製的字符串](https://stackoverflow.com/questions/15024561/printing-only-the-first-field-in-a-string) – Sundeep

+0

可能重複的[如何獲得CS中每行的第一列V文件?](https://stackoverflow.com/questions/11668621/how-to-get-the-first-column-of-every-line-from-a-csv-file)或https:// stackoverflow。 com/questions/19959746/extract-columns-space-or-tab-delimited-from-text-file-in-linux或https://stackoverflow.com/questions/8299553/how-to-get-the-first-列的-COMM-輸出 – Jedi

回答

2

如果輸入是

 
aaaa bbbb cccc 
dddd eeee ffff 
gggg hhhh iiii 

和你想要的是:

 
aaaa 
dddd 
gggg 

那麼你可以使用任何的:

心連心

0

使用awk :通過設置字段數爲1:

awk '{NF=1}1' inputfile 

使用grep,並期待變通:

grep -oP '^.[^ ]+' inputfile 

使用sed backrefrencing:

sed -r 's/(^.[^ ]+).*/\1/' inputfile 
相關問題