bash 如何删除bash中字符后的所有文本?

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

How can I remove all text after a character in bash?

bash

提问by Nathan

How can I remove all text after a character, in this case a colon (":"), in bash? Can I remove the colon, too? I have no idea how to.

如何在 bash 中删除字符后的所有文本,在本例中为冒号(“:”)?我也可以去掉结肠吗?我不知道该怎么做。

采纳答案by Eldad Mor

An example might have been useful, but if I understood you correctly, this would work:

一个例子可能很有用,但如果我理解正确,这将起作用:

echo "Hello: world" | cut -f1 -d":"

This will convert "hello: world" into "hello".

这会将“hello: world”转换为“hello”。

回答by Paused until further notice.

In Bash (and ksh, zsh, dash, etc.), you can use parameter expansion with %which will remove characters from the end of the string or #which will remove characters from the beginning of the string. If you use a single one of those characters, the smallest matching string will be removed. If you double the character, the longest will be removed.

在 Bash(以及 ksh、zsh、dash 等)中,您可以使用参数扩展%来删除字符串末尾的字符或#删除字符串开头的字符。如果您只使用这些字符中的一个,那么最小的匹配字符串将被删除。如果将字符加倍,最长的将被删除。

$ a='hello:world'
$ b=${a%:*}
$ echo "$b"
hello
$ a='hello:world:of:tomorrow'
$ echo "${a%:*}"
hello:world:of
$ echo "${a%%:*}"
hello
$ echo "${a#*:}"
world:of:tomorrow
$ echo "${a##*:}"
tomorrow

回答by sk8asd123

$ echo 'hello:world:again' |sed 's/:.*//'
hello

回答by cdhowie

egrep -o '^[^:]*:'

回答by Nico Feulner

Let's say you have a path with a file in this format:

假设您有一个包含以下格式文件的路径:

/dirA/dirB/dirC/filename.file

Now you only want the path which includes four "/". Type

现在您只需要包含四个“/”的路径。类型

$ echo "/dirA/dirB/dirC/filename.file" | cut -f1-4 -d"/"

and your output will be

你的输出将是

/dirA/dirB/dirC

The advantage of using cut is that you can also cut out the uppest directory as well as the file (in this example), so if you type

使用 cut 的好处是你还可以剪切最上层目录以及文件(在这个例子中),所以如果你输入

$ echo "/dirA/dirB/dirC/filename.file" | cut -f1-3 -d"/"

your output would be

你的输出将是

/dirA/dirB

Though you can do the same from the other side of the string, it would not make that much sense in this case as typing

虽然你可以从字符串的另一边做同样的事情,但在这种情况下,它没有像打字那样有意义

$ echo "/dirA/dirB/dirC/filename.file" | cut -f2-4 -d"/"

results in

结果是

dirA/dirB/dirC

In some other cases the last case might also be helpful. Mind that there is no "/" at the beginning of the last output.

在其他一些情况下,最后一种情况也可能有帮助。请注意,最后一个输出的开头没有“/”。

回答by S Hunter Simpson

trim off everything after the last instance of ":"

在“:”的最后一个实例之后修剪所有内容

cat fileListingPathsAndFiles.txt | grep -o '^.*:'

and if you wanted to drop that last ":"

如果你想删除最后一个“:”

cat file.txt | grep -o '^.*:' | sed 's/:$//'

@kp123: you'd want to replace :with /(where the sed colon should be \/)

@kp123:你想替换:/(sed 冒号应该在哪里\/