bash 如何删除bash中的前导部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2680274/
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
How to removing leading section in bash
提问by Kyle Van Koevering
How can I remove parts of a string up to a certain character?
如何将字符串的一部分删除到某个字符?
Ex.) If I have the string testFile.txt.1and testFile.txt.12345how can I remove the 1and 12345?
例如)如果我有字符串testFile.txt.1,testFile.txt.12345我该如何删除1和12345?
EDIT: I meant to remove and throw away the first part of a string up to a certain character and keep the end of it.
编辑:我的意思是删除并丢弃字符串的第一部分直到某个字符并保留它的结尾。
回答by ghostdog74
using just bash facilities
只使用 bash 工具
$ s=testFile.txt.1
$ echo ${s%.*}
testFile.txt
$ s=testFile.txt.12345
$ echo ${s%.*}
testFile.txt
to remove before leading zero
在前导零之前删除
$ echo ${s#*.}
txt.12345
Other method, you can split your string up using IFS
其他方法,您可以使用 IFS 拆分字符串
$ s=testFile.txt.12345
$ IFS="."
$ set -- $s
$ echo
testFile
$ echo
txt
$ echo
12345
回答by Michael Mrozek
If you just want to remove a known suffix from a filename, you can use basename:
如果您只想从文件名中删除已知后缀,您可以使用basename:
basename testFile.txt.1 .1
basename testFile.txt.12345 .12345
回答by John Kugelman
You could use sed to do a search and replace. The <<<operator will pass in a string on stdin.
您可以使用 sed 进行搜索和替换。该<<<运营商将传递标准输入的字符串。
$ sed 's/\.[0-9]*$//' <<< "testFile.txt.1"
testFile.txt
$ sed 's/\.[0-9]*$//' <<< "testFile.txt.12345"
testFile.txt
回答by DVK
The question is a bit unclear - the example provided may mean you want to remove all #s, or remove the part after the last ".", or remove the part after the first "1", or even remove all charcters after character 13. Please clarify.
问题有点不清楚 - 提供的示例可能意味着您要删除所有#s,或者删除最后一个“.”之后的部分,或者删除第一个“1”之后的部分,甚至删除字符 13 之后的所有字符。 请说清楚。
If you mean that you want to remove first N characters in a string (e.g. "up to a character # 13"), do echo testFile.txt.1 | cut -c14-. To retain the chars 1-13, on the other hand, do echo testFile.txt.1 | cut -c1-13
如果您的意思是要删除字符串中的前 N 个字符(例如“最多一个字符 # 13”),请执行echo testFile.txt.1 | cut -c14-. 另一方面,要保留字符 1-13,请执行echo testFile.txt.1 | cut -c1-13
If you mean that you want to remove the beginning characters until the first occurence of a specific character (in your example that seems to be "1"), do echo testFile.txt.1 | perl -e 's/^[^1]*//;'. To remove everything AFTER the first "1", do echo testFile.txt.1 | perl -e 's/1.*$//;'
如果您的意思是要删除开始字符,直到第一次出现特定字符(在您的示例中似乎是“1”),请执行echo testFile.txt.1 | perl -e 's/^[^1]*//;'. 要删除第一个“1”之后的所有内容,请执行echo testFile.txt.1 | perl -e 's/1.*$//;'
If you want to remove all the #s, do echo testFile.txt.1 | perl -e 's/\d//g;'or without Perl, echo testFile.txt.1 | tr -d "[0-9]"
如果你想删除所有的#s,echo testFile.txt.1 | perl -e 's/\d//g;'无论是否使用 Perl,echo testFile.txt.1 | tr -d "[0-9]"
If you want to remove everything after the last ".", do echo testFile.txt.1 | perl -e 's/\.[^.]+/./;'
如果要删除最后一个“.”之后的所有内容,请执行 echo testFile.txt.1 | perl -e 's/\.[^.]+/./;'
回答by yabt
# remove string prefix up to the first digit
var='testFile.txt.12345'
var='test1File.txt.12345'
var='testFile.txt.1'
var='testFile.txt'
echo "${var#"${var%%[[:digit:]]*}"}"

