从Bash中的行中删除中间的n个字符 [英] Remove the middle n characters from lines in Bash

查看:85
本文介绍了从Bash中的行中删除中间的n个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试剪切文件中每一行的中间部分.所有行都是这样的:

I am trying to cut out the middle of each line in a file. All the lines are like this:

79.472850   97 SILENCE 

最后我要结束:

79.472850 SILENCE

由于每行都有不想要的部分,从字符10开始到字符14结束 我试图以这种方式使用sed:

As each line has the undesired portion starting at character ten and ending at character 14, I was trying to use sed in this way:

sed "s/\(.\{9\}\).\{6\}//",但我最终得到的字符都是14号之后的.制表符空间后面的数字在每个文件中都会变化.我该怎么做才能让sed只剪掉制表符和两位数字?

sed "s/\(.\{9\}\).\{6\}//" but I just end up with everything after character 14. The numbers following the tab space change in every file. What can I do to make sed just cut out the tab and two digits?

感谢您的帮助.

推荐答案

根据您的输入和期望的输出,这可以是一种方法:

As per your input and expected output, this can be a way:

$ echo "79.472850   97 SILENCE" | tr -s " " | cut -d" " -f1,3
79.472850 SILENCE

  • tr -s " "删除重复的空格.
  • cut -d" " -f1,3根据空格分割打印第一和第三字段.
    • tr -s " " deletes repeated spaces.
    • cut -d" " -f1,3 prints 1st and 3rd field based on splitting by spaces.
    • 使用sed:

      $ sed 's#\([^ ]*\)[ ]*\([^ ]*\)[ ]*\([^ ]*\)#\1 \3#g' <<< "79.472850   97 SILENCE"
      79.472850 SILENCE
      

      这篇关于从Bash中的行中删除中间的n个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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