[英]How to extract the number after specific word using awk?
我有幾行文字。 我想使用 awk 提取特定單詞后的數字。
我嘗試了以下代碼,但它不起作用。
首先,通過vi test.text
創建測試文件。 有 3 列(這 3 個字段是由其他一些使用 awk 的管道命令生成的)。
Index AllocTres CPUTotal
1 cpu=1,mem=256G 18
2 cpu=2,mem=1024M 16
3 4
4 cpu=12,gres/gpu=3 12
5 8
6 9
7 cpu=13,gres/gpu=4,gres/gpu:ret6000=2 20
8 mem=12G,gres/gpu=3,gres/gpu:1080ti=1 21
請注意,此文件中有幾個空字段。 我想要實現的是使用如下管道提取每行中第一個gres/gpu=
之后的數字(如果此行中沒有出現gres/gpu=
,則默認數字為0
): cat test.text | awk '{some_commands}'
cat test.text | awk '{some_commands}'
輸出 4 列:
Index AllocTres CPUTotal GPUAllocated
1 cpu=1,mem=256G 18 0
2 cpu=2,mem=1024M 16 0
3 4 0
4 cpu=12,gres/gpu=3 12 3
5 8 0
6 9 0
7 cpu=13,gres/gpu=4,gres/gpu:ret6000=2 20 4
8 mem=12G,gres/gpu=3,gres/gpu:1080ti=1 21 3
首先: awk
不需要cat
,它可以自己讀取文件。 通常不鼓勵將cat
和awk
結合使用,因為 cat 無用。
對於此任務,我將使用 GNU AWK
以下方式,讓file.txt
內容為
cpu=1,mem=256G
cpu=2,mem=1024M
cpu=12,gres/gpu=3
cpu=13,gres/gpu=4,gres/gpu:ret6000=2
mem=12G,gres/gpu=3,gres/gpu:1080ti=1
然后
awk 'BEGIN{FS="gres/gpu="}{print $2+0}' file.txt
輸出
0
0
0
3
0
0
4
3
說明:我通知 GNU AWK
字段分隔符 ( FS
) 是gres/gpu=
然后對於每一行我打印的第二個字段增加了零。 對於沒有gres/gpu=
$2
的行是空字符串,當在算術上下文中使用時,這與零相同,因此零加零等於零。 對於至少有一個gres/gpu=
增加零的行,GNU AWK
會找到合法數字的最長前綴,因此3
(第 4 行)變為3
, 4,
(第 7 行)變為4
, 3,
(第 8 行)變為3
.
(在 GNU Awk 5.0.1 中測試)
使用您在 GNU awk
中顯示的示例,您可以嘗試以下代碼。 用 GNU awk
編寫和測試。 簡單的解釋是使用awk
的match
函數,其中使用正則表達式gres\/gpu=([0-9]+)
(在此處轉義/
)並創建一個且唯一的捕獲組來捕獲=
之后的所有數字。 一旦找到匹配,則在此處打印當前行,然后是數組的 arr 的第一個元素+0
(在沒有找到任何行的匹配的情況下打印零)。
awk '
FNR==1{
print $0,"GPUAllocated"
next
}
{
match($0,/gres\/gpu=([0-9]+)/,arr)
print $0,arr[1]+0
}
' Input_file
使用sed
$ sed '1s/$/\tGPUAllocated/;s~.*gres/gpu=\([0-9]\).*~& \t\1~;1!{\~gres/gpu=[0-9]~!s/$/ \t0/}' input_file
Index AllocTres CPUTotal GPUAllocated
1 cpu=1,mem=256G 18 0
2 cpu=2,mem=1024M 16 0
3 4 0
4 cpu=12,gres/gpu=3 12 3
5 8 0
6 9 0
7 cpu=13,gres/gpu=4,gres/gpu:ret6000=2 20 4
8 mem=12G,gres/gpu=3,gres/gpu:1080ti=1 21 3
awk '
BEGIN{FS="\t"}
NR==1{
$(NF+1)="GPUAllocated"
}
NR>1{
$(NF+1)=FS 0
}
/gres\/gpu=/{
split($0, a, "=")
gp=a[3]; gsub(/[ ,].*/, "", gp)
$NF=FS gp
}1' test.text
Index AllocTres CPUTotal GPUAllocated
1 cpu=1,mem=256G 18 0
2 cpu=2,mem=1024M 16 0
3 4 0
4 cpu=12,gres/gpu=3 12 3
5 8 0
6 9 0
7 cpu=13,gres/gpu=4,gres/gpu:ret6000=2 20 4
8 mem=12G,gres/gpu=3,gres/gpu:1080ti=1 21 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.