加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

awk – 删除匹配字符后的所有字符

发布时间:2020-12-13 23:26:09 所属栏目:Linux 来源:网络整理
导读:我有一个包含许多行的文件 http://example.com/part-1 this number 1 one http://example.com/part--2 this is number 21 twohttp://example.com/part10 this is an number 12 tenhttp://example.com/part-num-11 this is an axample number 212 eleven 如何
我有一个包含许多行的文件

http://example.com/part-1   this    number 1 one 
http://example.com/part--2  this is number 21 two
http://example.com/part10   this is an number 12 ten
http://example.com/part-num-11  this is an axample  number 212 eleven

如何删除第一个列和“数字x”之间的“数字x”之后的所有字符…我想要这样的输出

http://example.com/part-1    1
http://example.com/part--2   21 
http://example.com/part10    12
http://example.com/part-num-11   212

另一个案例:
输入:

http://server1.example.com/00/part-1    this    number 1 one 
http://server2.example.com/1a/part--2   this is section 21 two two
http://server3.example.com/2014/5/part10    this is an Part 12 ten  ten ten
http://server5.example.com/2014/7/part-num-11   this is an PARt number 212 eleven

我想要相同的输出….而且数字总是在最后一个数字字段中

解决方法

sed -r 's/^([^0-9]*[0-9]+)[^0-9]*([0-9]+).*/1 2/' file

输出:

http://example.com/part-1 1
http://example.com/part--2 21
http://example.com/part10 12
http://example.com/part-num-11 212

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读