Linux 如何删除特定字符后的字符串中的所有内容?

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

How to delete everything in a string after a specific character?

linuxbash

提问by knightOfSpring

Example:

例子:

    before: text_before_specific_character(specific_character)text_to_be_deleted
    after: text_before_specific_character

I know that it can be done with 'sed'. But i'm stuck. Can someone help me out?

我知道它可以用'sed'来完成。但我被困住了。有人可以帮我吗?

采纳答案by Charles Duffy

There's no reason to use an external tool such as sed for this; bash can do it internally, using parameter expansion:

没有理由为此使用外部工具,例如 sed;bash 可以在内部完成,使用参数扩展

If the character you want to trim after is :, for instance:

如果您要修剪的字符是:,例如:

$ str=foo_bar:baz
$ echo "${str%%:*}"
foo_bar

You can do this in both greedy and non-greedy ways:

你可以用贪婪和非贪婪的方式做到这一点:

$ str=foo_bar:baz:qux
$ echo "${str%:*}"
foo_bar:baz
$ echo "${str%%:*}"
foo_bar

Especially if you're calling this inside a tight loop, starting a new sed process, writing into the process, reading its output, and waiting for it to exit (to reap its PID) can be substantial overhead that doing all your processing internal to bash won't have.

特别是如果你在一个紧密的循环中调用它,启动一个新的 sed 进程,写入进程,读取它的输出,并等待它退出(以获取它的 PID)可能是大量的开销,因为在内部进行所有处理bash 不会有。



Now -- often, when wanting to do this, what you might reallywant is to split a variable into fields, which is better done with read.

现在 - 通常,当想要执行此操作时,您可能真正想要的是将变量拆分为字段,最好使用read.

For instance, let's say that you're reading a line from /etc/passwd:

例如,假设您正在阅读以下内容中的一行/etc/passwd

line=root:x:0:0:root:/root:/bin/bash
IFS=: read -r name password_hashed uid gid fullname homedir shell _ <<<"$line"
echo "$name" # will emit "root"
echo "$shell" # will emit "/bin/bash"


Even if you want to process multiple lines from a file, this too can be done with bash alone and no external processes:

即使您想处理一个文件中的多行,也可以单独使用 bash 而无需外部进程来完成:

while read -r; do
  echo "${REPLY%%:*}"
done <file

...will emit everything up to the first :from each line of file, without requiring any external tools to be launched.

...:将从 的每一行发出直到第一行的所有内容file,而无需启动任何外部工具。

回答by Nit

What you're looking for is actually really easy:

你要找的东西其实很简单:

sed 's/A.*//'

Where Amarks the specific character. Note that it is case sensitive, if you want to catch multiple characters use

whereA标记特定字符。请注意,它区分大小写,如果您想捕获多个字符,请使用

sed 's/[aAbB].*//'

回答by thb

If TEXT contains your text, as in

如果 TEXT 包含您的文本,如

TEXT=beforexafter

and the specific character happens (for example) to be x, then

并且特定字符恰好(例如)是x,然后

echo "${TEXT%x*}"

does what you want.

做你想做的。

To Bash, "$TEXT"or "${TEXT}"is the whole beforexafter, but "${TEXT%xafter}"is just before, with the xafterchopped off the end. To chop off the xand anything that might follow it, one writes "${TEXT%x*}".

对 Bash 来说,"$TEXT"或者"${TEXT}"是整个beforexafter,但"${TEXT%xafter}"只是before,用xafter砍掉的结尾。为了砍掉它x以及可能跟随它的任何东西,有人写道"${TEXT%x*}"

There is also "${TEXT%%x*}", incidentally. This differs from the other only if there is more than one x. With the %%, Bash chops off all x, whereas with the %it chops off only from the last x. You can remember this by observing loosely that the longer %%chops off more text.

还有"${TEXT%%x*}",顺便说一句。仅当存在多个 时,这才与另一个不同x。使用%%, Bash 砍掉所有x,而使用 ,%它只从最后一个砍掉x。您可以通过松散地观察到越长会%%切断更多文本来记住这一点。

You can do likewise with Sed, of course, if you prefer:

当然,如果您愿意,您也可以使用 Sed:

echo "$TEXT" | sed 's/x.*//'