[英]How to display the first word of each line in my file using the linux commands?
[英]how to print each word in each line using linux terminal?
假設有一個這樣的文件。.(6個ID)
k0012, k0013
k40035, k3089
Glc, 3-PGA
我想使用linux終端在每一行中打印每個ID。 提前致謝
您可以使用tr:
tr ' ,' '\0\n' file
或gnu-sed:
sed 's/, */\n/g' <file
或gnu awk:
awk -F, '{print $1}' RS='[[:space:]]+' file
使用grep
:
$ grep -oP "\b[^\s,]*\b" inputfile
k0012
k0013
k40035
k3089
Glc
3-PGA
從您的“輸入”中,尚不清楚ID是由空格還是由空格和逗號分隔。 如果后者是正確的,那么您可能需要同時處理以下兩者:
tr ' ,' '\n\n' < myfile | uniq
(您需要使用uniq
刪除那些多余的空行,盡管使用awk
可能會更好一些):
tr ' ,' '\n\n' < myfile | awk 'NF > 0'
你在追嗎 使用此輸入: (6 Ids) k0012, k0013 k40035, k3089 Glc, 3-PGA
$ awk -F'[, ]*' '{for(i=3;i<=NF;i++)print $i}' input
k0012
k0013
k40035
k3089
Glc
3-PGA
如果輸入是:
k0012, k0013
k40035, k3089
Glc, 3-PGA
嘗試:
$ awk -F'[, ]*' '{for(i=1;i<=NF;i++)print $i}' input
k0012
k0013
k40035
k3089
Glc
3-PGA
...或者只有兩列:
awk -F'[, ]*' '{printf("%s\n%s", $1, $2)}' input
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.