[英]How to extract values from a text file and rearrange them using Bash?
[英]How to extract text from columns and feed them to a code on Linux (bash)?
假設我有以下文本文件,這些文件之間有空格,制表符,無論這些詞之間是什么:
1 test.1
2 word.2
3 whatever.5
我想提取第二列(不適用於我無法理解的cut -d' ' -f2 test.txt
),然后提取小數點前的那一部分,並將結果分開輸入某些代碼。 因此,上面的文本文件應創建三個命令:
mycommand test
mycommand word
mycommand whatever
我嘗試了以下構造:
cat test.txt | gawk '{print $2;}' | gawk -F[.] '{print $1;}' | xargs -0 mycomand
這不會產生預期的結果(即創建三個不同的作業)。
Questios:1.如何正確執行? 2.如何簡化表達式3. cut
表達式有什么問題?
$> grep -P -o "(?<= )[^\ \.]+" test.txt | xargs mycommand
Perl回答您是否需要另一種方法:
perl -ne'print (((/^\d+\s+([^\.]+)/)[0])."\n")'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.