English 中文(简体)
从血管的弹壳中,一行的弹 par数
原标题:From the bash command shell, parsing numbers from a line
  • 时间:2011-09-07 18:36:38
  •  标签:
  • bash

我的航向是一条航道,正从像:

There are 5 apples and 7 oranges

我也想把5和7个变量带入冲变数,并存在问题。

awk{印刷3,6 }由于如果存在香蕉,其地位可能发生变化而赢得了t work。 两位数如果能够与他们的成果挂钩,就可点数。

预 收

最佳回答

并取得果实:

echo  There are 5 apples and 7 oranges  | grep -o -E  [[:digit:]]+ [[:alpha:]]+ 
问题回答

a. 只使用一种双管齐下的方法:

str=" There are 5 apples, 12 bananas and 7 oranges"

i=0
while [[ "$str" =~ ([0-9]+)" "([a-z]+)(.*) ]] ; do
    num[$i]=${BASH_REMATCH[1]}
    type[$i]=${BASH_REMATCH[2]}
    str=${BASH_REMATCH[3]}
    (( i++ ))
done

for (( i=0; i<${#num[@]}; i++ )); do
    printf "%4d	%s	%s
" $i ${num[$i]} ${type[$i]}
done

产出

   0    5       apples
   1    12      bananas
   2    7       oranges

回答:

echo  There are 5 apples and 7 oranges  | grep -o -E  [[:digit:]]+ 

缩略语:

<代码> 共有5个 app子和7个 or子——o [0-9]+w+。

但请注意,<代码>w为[:alnum:]的同义词,因此也将与“appl3s”等同。

彩礼是一种奇怪的语言,你可以做冰。 例如:

a="there are 7 apples"
for i in $a; do
  case $i in
    [0-9]*)
      value=$i
      expectname=1
    ;;
    *)
      test -n "$expectname" && name=$i
      expectname=
    ;;
  esac
done

echo $value
echo $name

如果你不止一次,你就可以填写一个阵列或一个巴什图。





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签