bash 如何使用 sed 从字符串中删除前 X 个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11469989/
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-09 22:25:29  来源:igfitidea点击:

How can I strip first X characters from string using sed?

bashshellsed

提问by Kokesh

I am writing shell script for embedded Linux in a small industrial box. I have a variable containing the text pid: 1234and I want to strip first X characters from the line, so only 1234 stays. I have more variables I need to "clean", so I need to cut away X first characters and ${string:5}doesn't work for some reason in my system.

我正在一个小型工业盒子中为嵌入式 Linux 编写 shell 脚本。我有一个包含文本的变量pid: 1234,我想从行中删除前 X 个字符,所以只有 1234 保留。我有更多的变量需要“清理”,所以我需要删除 X 个第一个字符,${string:5}并且由于某种原因在我的系统中不起作用。

The only thing the box seems to have is sed.

盒子里似乎只有sed.

I am trying to make the following to work:

我正在尝试使以下内容起作用:

result=$(echo "$pid" | sed 's/^.\{4\}//g')

Any ideas?

有任何想法吗?

采纳答案by Arnaud F.

This will do the job too:

这也将完成这项工作:

echo "$pid"|awk '{print }'

回答by chepner

The following should work:

以下应该工作:

var="pid: 1234"
var=${var:5}

Are you sure bashis the shell executing your script?

你确定bashshell 正在执行你的脚本吗?

Even the POSIX-compliant

即使是符合 POSIX 的

var=${var#?????}

would be preferable to using an external process, although this requires you to hard-code the 5 in the form of a fixed-length pattern.

比使用外部过程更可取,尽管这需要您以固定长度模式的形式对 5 进行硬编码。

回答by Randy the Dev

Here's a concise method to cut the first X characters using cut(1). This example removes the first 4 characters by cutting a substring starting with 5th character.

这是使用cut(1). 此示例通过剪切从第 5 个字符开始的子字符串来删除前 4 个字符。

echo "$pid" | cut -c 5-

回答by Mark Longair

Use the -roption ("use extended regular expressions in the script") to sedin order to use the {n}syntax:

使用-r选项(“在脚本中使用扩展正则表达式”)sed以使用以下{n}语法:

$ echo 'pid: 1234'| sed -r 's/^.{5}//'
1234

回答by dtp70

Cut first two characters from string:

从字符串中剪切前两个字符:

$ string="1234567890"; echo "${string:2}"
34567890

回答by Ben

pipe it through awk '{print substr($0,42)}'where 42 is one more than the number of characters to drop. For example:

将它通过管道,awk '{print substr($0,42)}'其中 42 比要删除的字符数多 1。例如:

$ echo abcde| awk '{print substr(
[me@home]$ echo "pid: 1234" | cut -d" " -f2
1234
,2)}' bcde $

回答by Shawn Chin

Chances are, you'll have cutas well. If so:

很有可能,你也会有cut。如果是这样的话:

$ echo "pid: 1234" | tail -c +6
1234

回答by Mecki

Well, there have been solutions here with sed, awk, cutand using bashsyntax. I just want to throw in another POSIX conform variant:

那么,有这里已经解决方案sedawkcut以及使用bash的语法。我只想加入另一个符合 POSIX 的变体:

result=`echo $pid | cut -c 5-`

-ctells tail at which byte offset to start, counting from the end of the input data, yet if the the number starts with a +sign, it is from the beginning of the input data to the end.

-c告诉tail从哪个字节偏移开始,从输入数据的末尾开始计数,但如果数字以+符号开头,则是从输入数据的开头到结尾。

回答by Evgeny

Another way, using cutinstead of sed.

另一种方式,使用cut代替sed.

result=\`echo "$pid" | sed '/./ { s/pid:\ //g; }'\``

回答by treehead

I found the answer in pure sed supplied by this question(admittedly, posted after this question was posted). This does exactly what you asked, solely in sed:

我在这个问题提供的纯 sed 中找到了答案(诚​​然,在发布这个问题之后发布)。这完全符合您的要求,仅在 sed 中:

# Uncomment a line:
sed -i '/#\ COMMENTED_LINE_TO_MATCH/ { s/#\ //g; }' /path/to/target/file

The dot in sed '/./) is whatever you want to match. Your question is exactly what I was attempting to, except in my case I wanted to match a specific line in a file and then uncomment it. In my case it was:

) 中的点sed '/./是您想要匹配的任何内容。你的问题正是我想要的,除了在我的情况下,我想匹配文件中的特定行然后取消注释。就我而言,它是:

##代码##

The -iafter sedis to edit the file in place (remove this switch if you want to test your matching expression prior to editing the file).

-i之后sed是编辑而不是文件(如果你想编辑文件之前,测试你的匹配表达式删除此开关)。

(FTR, I posted this answer not for credit, but simply because I wanted to do this entirely with sed as this question asked and none of the previous answered solved that problem.)

(FTR,我发布这个答案不是为了信用,而仅仅是因为我想在提出这个问题时完全使用 sed 来做到这一点,而且之前的回答都没有解决这个问题。)