Mc Kevin
Mc Kevin

Reputation: 972

String Manipulation in UNIX

I have a script that after running, will shows:

The square 1 with the name Square with area 10 is created
The square 2 with the name Box with area 20 is created
The circle with the name Spinny with area 22 is not created
The rectangle with the name Tri with area 30 is created.

How can I retrieve all the numbers between area and is created and store it into an array?

stored array will be:

10
20
30

The tricky part is that, I cannot just find the first number, as square (1) will be read, I cannot also do take the number between "area" and "is" as it might take in shapes that are not created. any idea how to do this?

Upvotes: 2

Views: 100

Answers (4)

kojiro
kojiro

Reputation: 77059

awk

awk '/area [[:digit:]]+ is created/{ print $(NF-2); }'

Prints the third-to-last field on matching lines.

array=( $(awk '/area [[:digit:]]+ is created/{ print $(NF-2); }' <<< "$input") )
printf '%s\n' "${array[@]}"
10
20
30

pure bash

array=()
while read -a line; do
  if [[ "${line[@]: -2:1}" = is && "${line[@]: -1}" = created ]]; then
    array+=( "${line[@]: -3:1}" )
  fi
done <<< "$input"
printf '%s, ' "${array[@]}" # Will output 10, 20 if there's a period on the last line...

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 753465

array=( $(sed -n 's/.*with area \([0-9][0-9]*\) is created.*/\1/p' data) )

The array=( ... ) notation is an array assignment in bash. The $(...) is command substitution. The sed command does not print by default, but when a line matches 'with area XX is created' (where XX is a number of one or more digits), then the line is replaced with the value of XX and printed.

Upvotes: 1

BMW
BMW

Reputation: 45223

Using gnu grep (Lookahead and Lookbehind Zero-Length Assertions)

grep -Po '(?<=area )[0-9]* (?=is created)' file

Using sed

sed -n 's/.* area \([0-9]*\) is created.*/\1/p' file

Upvotes: 0

falsetru
falsetru

Reputation: 368894

Using grep:

$ cat testfile
The square 1 with the name Square with area 10 is created
The square 2 with the name Box with area 20 is created
The circle with the name Spinny with area 22 is not created
The rectangle with the name Tri with area 30 is created.
$ grep -Eo 'area [0-9]+ is created' testfile | grep -Eo '[0-9]+'
10
20
30

NOTE POSIX grep doesn't support -o, but this should work on BSD and GNU greps.

Upvotes: 2

Related Questions