简体   繁体   English

如何使用bash在字符串中的特定字符之后打印字符

[英]How to print character just after a particular character in a string using bash

I have a string like我有一个像

abc def 0/1 dls dl 9/0 1//o

I want to take 0 and 1 (the characters just after and before the first / in the string) into two variables, say $NUM1 and $NUM2 , and do some further logic on them.我想将01 (字符串中第一个/之后和之前的字符)放入两个变量中,比如$NUM1$NUM2 ,并对它们做一些进一步的逻辑。

Using just shell parameter expansion :仅使用shell 参数扩展

$ str='abc def 0/1 dls dl 9/0 1//o'
$ num1=${str%%/*}    # Remove first "/" and everything after it
$ echo "$num1"
abc def 0
$ num1=${num1##* }   # Remove last space and everything before it
$ echo "$num1"
0
$ num2=${str#*/}     # Remove first "/" and everything before it
$ echo "$num2"
1 dls dl 9/0 1//o
$ num2=${num2%% *}   # Remove first space and everything after it
$ echo "$num2"
1

# , ## , % and %% have to be used carefully here. # , ## , %%%在这里必须小心使用。 A single repetition removes the shortest match of the pattern following, and the double usage removes the longest pattern match.单次重复删除后面的模式中最短的匹配,重复使用删除最长的模式匹配。


Alternatively, after the first expansion, you just want the first or last character of the string, which can be solved with substring expansion:或者,在第一次扩展后,您只需要字符串的第一个或最后一个字符,这可以通过子字符串扩展来解决:

$ echo "$num1"
abc def 0
$ echo "${num1:(-1)}"  # Extract last character
0
$ echo "$num2"
1 dls dl 9/0 1//o
$ echo "${num2:0:1}"   # Extract first character
1

Negative indices requires at least Bash 4.2.负索引至少需要 Bash 4.2。 For older Bash versions, we can use对于较旧的 Bash 版本,我们可以使用

echo "${num1:${#num1}-1}"

And finally, we could use Bash regular expressions, as demonstrated in glenn jackman's answer .最后,我们可以使用 Bash 正则表达式,如glenn jackman's answer 所示

You can use bash regular expressions, which populate the BASH_REMATCH array with the captured subexpressions:您可以使用 bash 正则表达式,它使用捕获的子表达式填充BASH_REMATCH数组:

if [[ $a =~ (.)/(.) ]]; then
  num1=${BASH_REMATCH[1]}
  num2=${BASH_REMATCH[2]}
fi
echo $num1 $num2
0 1

Don't get into the habit of using ALL_CAPS_VARNAMES: one day you'll accidentally write PATH=/my/path and then wonder why your script is broken.不要养成使用 ALL_CAPS_VARNAMES 的习惯:有一天你会不小心写到PATH=/my/path然后想知道为什么你的脚本被破坏了。 Leave upper case variables for the shell & OS为外壳和操作系统保留大写变量

You can use sed to parse the line with regex to extract the numbers:您可以使用 sed 用正则表达式解析该行以提取数字:

NUM1=$(echo 'abc def 0/1 dls dl 9/0 1//o' | sed 's@[^0-9]*\([0-9]*\)/[0-9]*.*@\1@')
NUM2=$(echo 'abc def 0/1 dls dl 9/0 1//o' | sed 's@[^0-9]*[0-9]*/\([0-9]*\).*@\1@')
echo $NUM1
0
echo $NUM2
1

I assume you are implicitly separating characters based on spaces.我假设您是基于空格隐式分隔字符。 So you want the characters from the space to the first '/' and from the first '/' to the next space.因此,您希望字符从空格到第一个 '/' 以及从第一个 '/' 到下一个空格。 I would probably use sed:我可能会使用 sed:

echo "abc def 0/1 dls dl 9/0 1//o" | sed -r 's%^[^/]* ([^ ]*)/([^ ]*) .*%\1 \2%'

But note this won't work if there are no spaces preceding the first '/'.但请注意,如果第一个“/”之前没有空格,这将不起作用。 You may wish to have another pattern for that case.您可能希望为这种情况使用另一种模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM