从 Bash 中的行中删除中间的 n 个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18186481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:13:09  来源:igfitidea点击:

Remove the middle n characters from lines in Bash

bashunixsedcut

提问by Felixia

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

我试图剪掉文件中每一行的中间部分。所有的行都是这样的:

79.472850   97 SILENCE 

and I need to end up with:

我需要结束:

79.472850 SILENCE

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:

由于每一行都有不想要的部分,从第 10 个字符开始到第 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?

sed "s/\(.\{9\}\).\{6\}//"但我最终得到了字符 14 之后的所有内容。每个文件中制表符空间后面的数字都会发生变化。我该怎么做才能让 sed 只剪掉标签和两位数字?

Thanks for your help.

谢谢你的帮助。

回答by fedorqui 'SO stop harming'

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 " "deletes repeated spaces.
  • cut -d" " -f1,3prints 1st and 3rd field based on splitting by spaces.
  • tr -s " "删除重复的空格。
  • cut -d" " -f1,3根据空格分割打印第一个和第三个字段。

With sed:

sed

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

回答by devnull

Looks like you need the first and the third fieldsfrom the input:

看起来您需要输入中的第一个和第三个字段

$ echo "79.472850   97 SILENCE" | awk '{print , }'
79.472850 SILENCE

回答by cdarke

No need to call external programs like sedor cut, or other languages like awk, bashcan do it for you:

不需要调用像外部程序sedcut类似,或其他语言awk庆典能为你做:

var="79.472850   97 SILENCE"
echo ${var:0:9}${var:14}
79.472850 SILENCE

${var:0:9}copies 9 characters starting at position 0 (start of text).

${var:0:9}从位置 0(文本开头)开始复制 9 个字符。

${var:14}copies from character position 14 to the end of text.

${var:14}从字符位置 14 复制到文本末尾。

Alternatively, if it is space-delimited fields you need:

或者,如果是空格分隔的字段,您需要:

read one two three <<< "$var"
echo "$one $three"
79.472850 SILENCE

Again, that uses pure bash.

同样,它使用纯 bash。

回答by svante

Here is an awk solution that works even if you have spaces in your third column :

这是一个 awk 解决方案,即使您的第三列中有空格也能正常工作:

awk '{=""; print}' file

Where $2=""empties the second column and printoutputs all columns.

其中$2=""清空第二列并 print输出所有列。

回答by Kent

if you want to extract exactly like what you described with sed:

如果你想完全像你用 sed 描述的那样提取:

kent$  echo "79.472850 97 SILENCE"|sed -r 's/(^.{9})(.{4})(.*)/ /'
79.472850 SILENCE

if you want to extract the 1st and 3rd/last columns, from your space separated input, you could use awk: awk '$2="";7'

如果要从空格分隔的输入中提取第一列和第三列/最后一列,可以使用 awk: awk '$2="";7'

kent$  echo "79.472850 97 SILENCE"|awk '="";7'
79.472850  SILENCE