如何从Bash中的字符串中删除最后n个字符? [英] How to remove last n characters from a string in Bash?

查看:183
本文介绍了如何从Bash中的字符串中删除最后n个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Bash脚本中有一个变量var,其中包含一个字符串,例如:

I have a variable var in a Bash script holding a string, like:

echo $var
"some string.rtf"

我想删除该字符串的最后4个字符,并将结果分配给新变量var2,这样

I want to remove the last 4 characters of this string and assign the result to a new variable var2, so that

echo $var2
"some string"

我该怎么做?

推荐答案

首先,您应该明确想要的内容.如果您知道字符串以.rtf结尾并且想要删除.rtf,则可以使用var2=${var%.rtf}.如果您不知道后缀是什么,但是您想删除最后一个.开头的所有内容,则可以使用var2=${var%.*}.如果只想将所有内容保持在第一个.以内,则可以使用var2=${var%%.*}.如果只有一个期间,则后两个选项具有相同的结果,但是如果可能有多个,则可以决定要哪个.

First, you should be explicit about what you want. If you know the string ends in .rtf and you want to remove the .rtf, you can use var2=${var%.rtf}. If you don't know what the suffix is but you want to remove everything starting with the last ., then you can use var2=${var%.*}. If you only want to keep everything up to the first ., you can use var2=${var%%.*}. Those last two options have the same result if there's only one period, but if there might be more than one, you get to decide which you want.

如果您确实要始终删除确切数目的字符,则可以使用以下一些选项.

If you really want to always remove an exact number of characters, here are some options.

您专门标记了此bash,因此我们将从bash内置插件开始.工作时间最长的是我上面使用的相同的后缀删除语法:要删除四个字符,请使用var2=${var%????}.您需要为每个字符删除一个问号,因此对于较大的子字符串长度来说这很麻烦.

You tagged this bash specifically, so we'll start with bash builtins. The one which has worked the longest is the same suffix-removal syntax I used above: to remove four characters, use var2=${var%????}. You need one question mark per character removed, so this gets unwieldy for larger substring lengths.

略微更新的是子字符串提取:var2=${var::${#var}-4}.您可以在此处用任意数字代替4来删除其他数量的字符. (${#var}被替换为字符串的长度,因此这实际上是要求保留前(长度-4)个字符.)

Slightly newer is substring extraction: var2=${var::${#var}-4}. Here you can put any number in place of the 4 to remove a different number of characters. (The ${#var} is replaced by the length of the string, so this is actually asking to keep the first (length - 4) characters.)

较新版本的bash(特别是4+,这意味着MacOS附带的版本将无法正常工作),可以将其简化为var2=${var::-4}.

Newer versions of bash (specifically 4+, which means the one that ships with MacOS won't work) let you simplify that to just var2=${var::-4}.

如果您实际上不是在使用bash,而是在使用其他POSIX类型的shell,则即使在普通的旧破折号中,后缀删除仍将起作用(其余的都不起作用).在zsh中,它们都可以工作,但是您必须在冒号之间放置0:var2=${var:0:-4}等.在ksh中,您需要0,还必须使用显式的length-4表达式:var2=${var:0:${#var}-4}.

If you're not actually using bash but some other POSIX-type shell, the suffix removal will still work, even in plain old dash (where none of the rest will). In zsh, they all work but you have to put a 0 between the colons: var2=${var:0:-4} etc. In ksh, you need the 0 and also have to use the explicit length-4 expression: var2=${var:0:${#var}-4}.

您当然可以在实用程序的帮助下使用命令替换来完成此操作;有很多方法可以工作,但是var2=$(cut -c -4 <<<"$var")之类的东西可能是最短的选择.

You can of course use command substitution to do it with the help of a utility program; there are plenty that will work, but something like var2=$(cut -c -4 <<<"$var") is probably the shortest option.

这篇关于如何从Bash中的字符串中删除最后n个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆