【问题标题】:shell - remove numbers from a string column [closed]shell - 从字符串列中删除数字[关闭]
【发布时间】:2016-06-03 10:45:31
【问题描述】:
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

【问题讨论】:

  • 欢迎来到 Stack Overflow sri。请阅读 how to ask。搜索和研究 - 这个网站上有很多正则表达式示例。请展示您到目前为止所做的尝试。 SO 不是代码编写服务。
  • 是的,我什么都试过了
  • 包括一些会影响谁提供帮助的例子。

标签: shell awk sed


【解决方案1】:

[我]什么都试过了

...除了,说:

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

【讨论】:

  • 谢谢...但我的问题是这样的:- 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
【解决方案2】:

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

你可以试试这个,我已经将第三个字段修改为最后一个。但归功于@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

【讨论】:

  • 谢谢 LOTTT...请您解释一下,好吗?
猜你喜欢
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-18
  • 2021-03-17
相关资源
最近更新 更多