shell - 从字符串列中删除数字

时间:2016-06-03 10:45:31

标签: shell awk sed

SER1828-ZXC-A1-10002 
SER1878-IOP-B1-98989 
SER1930-QWE-A2-10301
SER1930-QWE-A2-10301
SER1930-QWS_GH-A2-10301
SER1930-REM_PH-A2-10301

从上面的数据我的要求是删除任何数字表格,例如“ZXC-A1”.. 输出要求是

SER1828-ZXC-A-10002
SER1878-IOP-B-98989
SER1930-QWE-A-10301
SER1930-QWE-A-10301
SER1930-QWS_GH-A-10301
SER1930-REM_PH-A-10301

2 个答案:

答案 0 :(得分:1)

  

[我]已经尝试了一切

......除了,说:

awk -F- 'BEGIN { OFS="-" } { sub(/[0-9]/,"",$3); print }' yourdata

答案 1 :(得分:0)

当你提出问题时,你应该更加清楚。以后不要添加测试用例

你可以尝试这个,我已经修改了第三个字段,但只有一个。但归功于@Kaz

~> more test
SER1828-ZXC-A1-10002
SER1878-IOP-B1-98989
SER1930-QWE-A2-10301
SER1930-QWE-A2-10301
SER1930-QWS_GH-A2-10301
SER1930-REM_PH-A2-10301
SER1930-REM-SEW-PH-A2-10301
SER1940-REM-SPD-PL-D3-10301


~> awk -F- 'BEGIN { OFS="-" } { sub(/[0-9]/,"",$(NF-1)); print }' test
SER1828-ZXC-A-10002
SER1878-IOP-B-98989
SER1930-QWE-A-10301
SER1930-QWE-A-10301
SER1930-QWS_GH-A-10301
SER1930-REM_PH-A-10301
SER1930-REM-SEW-PH-A-10301
SER1940-REM-SPD-PL-D-10301

修改

<强>解释

-F- -> Define the input field separator 
OFS="-" ->  Set the output field separator 
sub( -> Substitute    
/[0-9]/ -> Select all the numbers 
"" -> substitute with nothing
$(NF-1) -> For the last but one field
Print -> Print All results