如何删除bash中的字符后的所有文本?
如何删除一个字符后面的所有文本,本例中是冒号(“:”),在bash中? 我也可以去除结肠吗? 我不知道如何。
一个例子可能是有用的,但如果我正确地理解了你,这将工作:
cut -f1 -d":" 这会将“hello:world”转换成“hello”。
 在Bash(和ksh,zsh,破折号等)中,可以使用参数扩展% ,这将从string的末尾或#中删除字符,从string的开头删除字符。 如果您使用这些字符中的单个字符,则最小的匹配string将被删除。 如果你将angular色加倍,最长的将被删除。 
 $ 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 
 $ echo 'hello:world:again' |sed 's/:.*//' hello 
 egrep -o '^[^:]*:' 
假设你有一个文件格式的path:
 /dirA/dirB/dirC/filename.file 
现在你只需要包含四个“/”的path。 types
 $ echo "/dirA/dirB/dirC/filename.file" | cut -f1-4 -d"/" 
和你的输出将是
 /dirA/dirB/dirC 
使用剪切的优点是,你也可以剪切最高的目录以及文件(在这个例子中),所以如果你input
 $ echo "/dirA/dirB/dirC/filename.file" | cut -f1-3 -d"/" 
你的输出将是
 /dirA/dirB 
虽然你可以从string的另一边做同样的事情,但是在这种情况下,打字就没有什么意义了
 $ echo "/dirA/dirB/dirC/filename.file" | cut -f2-4 -d"/" 
结果是
 dirA/dirB/dirC 
在其他一些情况下,最后一种情况也可能有所帮助。 请注意,在上次输出开始时没有“/”。