Linux 在括号前的每一行末尾添加数字

Linux 在括号前的每一行末尾添加数字,linux,string,awk,vim,sed,Linux,String,Awk,Vim,Sed,需要在linux中向主机文件的特定位置添加连续编号 awk '0==NR%2{$0} 1' FILE 正在尝试awk,但无法四处走动 [tab-edge] 101.55.12.199 [tab-edge] 101.55.12.193 [tab-edge] 101.55.12.194 [tab-edge] 101.55.12.192 预期结果 [tab-edge1] 101.55.12.199 [tab-edge2] 101.55.12.193 [tab-edge3] 101.55.12.19

需要在linux中向主机文件的特定位置添加连续编号

awk '0==NR%2{$0} 1' FILE
正在尝试awk,但无法四处走动

[tab-edge]
101.55.12.199
[tab-edge]
101.55.12.193
[tab-edge]
101.55.12.194
[tab-edge]
101.55.12.192
预期结果

[tab-edge1]
101.55.12.199
[tab-edge2]
101.55.12.193
[tab-edge3]
101.55.12.194
[tab-edge4]
101.55.12.192

你能试试下面的吗

awk '/\[tab/{sub(/\]$/,++count"&")} 1' Input_file
awk '/^\[/{sub(/\]$/,++count"&")} 1'  Input_file
awk 'FNR%2!=0{sub(/\]$/,++count"&")} 1'  Input_file
或者,如果每一行都从要插入数字的
[
开始,而不是从
[tab
开始,那么请尝试以下操作

awk '/\[tab/{sub(/\]$/,++count"&")} 1' Input_file
awk '/^\[/{sub(/\]$/,++count"&")} 1'  Input_file
awk 'FNR%2!=0{sub(/\]$/,++count"&")} 1'  Input_file


或者在OP的尝试下,如果您想为奇数行插入计数,请尝试以下操作

awk '/\[tab/{sub(/\]$/,++count"&")} 1' Input_file
awk '/^\[/{sub(/\]$/,++count"&")} 1'  Input_file
awk 'FNR%2!=0{sub(/\]$/,++count"&")} 1'  Input_file