sed中的第一个样式后如何删除空间 [英] How to remove space after the first pattern in sed

查看:67
本文介绍了sed中的第一个样式后如何删除空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件

file.txt

Name-6.1.4_Float Time;453802;464204;466345 464481 466131  464283  465065 462561
Name-6.1.4_Logic Good Time;125896;145349;137303 131601  144520 132487  133884

我想在第一个分号;之后用;替换数值之间的空格.数值之间的空格可以有多个空格.同样在Name_Float Time上面可以是任何东西.

I want to replace the spaces between the numerical value by ; after the first semicolon ; The spaces between the numerical value can have more than one space. Also on above Name_Float Time can be anything.

预期输出:

Name-6.1.4_Float Time;453802;464204;466345;464481;466131;464283;465065;462561;
Name-6.1.4_Logic Good Time;125896;145349;137303;131601;144520;132487;133884;

这是我尝试过的:

sed 's/ /;/g;s/;;/;/g;s/$/;/g' file.txt

输出:

Name-6.1.4_Float;Time;453802;464204;466345;464481;466131;464283;465065;462561;
Name-6.1.4_Logic;Good;Time;125896;145349;137303;131601;144520;132487;133884;

在上面的输出中,名称之间的空格也被替换为;.请建议我如何避免使用;替换名称之间的空格.谢谢.

On above output, the spaces between the Name also get replaced by ;. Please suggest me how to avoid the spaces between the name to get replaced by ;. Thanks.

推荐答案

您可以使用

sed -E ':a;s/(;[^ \t]+)[ \t]+/\1;/;ta' file 

请参见在线演示

详细信息

  • :a;-标签
  • s/(;[^ \t]+)[ \t]+/\1;:
    • (;[^ \t]+)-查找并<捕获>捕获到第1组...
      • ;-一个;字符
      • [^ \t]+-除空格和制表符之外的一个或多个字符
      • :a; - label
      • s/(;[^ \t]+)[ \t]+/\1;:
        • (;[^ \t]+) - find and capture into Group 1...
          • ; - a ; char
          • [^ \t]+ - one or more chars other than space and tab

          这篇关于sed中的第一个样式后如何删除空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆