#!/usr/bin/env bash
x=()
y=()
while read line; do
x+=("$(sed 's/[^(]*(\([0-9]*\)).*/\1/' <<< $line)")
y+=("$(sed 's/[^(]*([^(]*(\([0-9]*\)).*/\1/' <<< $line)")
done < "data"
echo "${x[@]}"
echo "${y[@]}"
x
and y
are declared as arrays. Then you loop over the input file and invoke a sed
command to every line
in your input file.
x+=(data)
appends the value data
to the array x
. Instead of writing the value we want to store in the array, we use command substitution, which is done with $(command)
, instead of appending the literal meaning of $(command)
to the array, the command is executed and its return value is stored in the array.
Let's look at the sed
commands:
's' is the substitute command, with [^(]*
we want to match everything, except (
, then we match (
. The following characters we want to store in the array, to do that we use \(
and \)
, we can later reference to it again (with \1
). The number is matched with [0-9]*
. In the end we match the closing bracket )
and everything else with .*
. Then we replace everything we matched (the whole line), with \1
, which is just what we had between \(
and \)
.
If you are new to sed
, this might be highly confusing, since it takes some time to read the sed
syntax.
The second sed
command is very similar.