0

My question can be split in 2. First I have a data file (file.dat) that looks like:

Parameter stuff number 1 (1029847) word index 2 (01293487), bla bla
Parameter stuff number 3 (134123) word index 4 (02983457), bla bla
Parameter stuff number 2 (109847) word index 3 (1029473), bla bla
etc...

I want to extract the number in brackets and save it to a variable for example the first one in line one to be 'x1', the second on the same line to be 'y1', for line 2 'x2' and 'y2', and so on... The numbers change randomly line after line, their position (in columns, if you like) stays the same line after line. The number of lines is variable (0 to 'n'). How can I do this? Please.

I have search for answers and I get lost with the many different commands one can use, however those answers attend to particular examples where the word is at the end or in brackets but only one per line, etc. Anyhow, here is what I have done so far (I am newby):

1) I get rid of the characters that are not part of the number in the string

sed -i 's/(//g' file.dat
sed -i 's/),//g' file.dat

2) Out of frustration I decided to output the whole lines to variables (getting closer?) 2.1) Get the number of lines to iterate for:

numlines=$(wc -l < file.dat)

2.2) Loop to numlines (I havent tested this bit yet!)

for i in {1..$numlines}
do
line${!i}=$(sed -n "${numlines}p" file.dat)
done

2.3) I gave up here, any help appreciated.

The second question is similar and merely out of curiosity: imagine a database separated by spaces, or tabs, or comas, any separator; this database has a variable number of lines ('n') and the strings per line may vary too ('k'). How do I extract the value of the 'i'th line on the 'j'th string, and save it to a variable 'x'?

4

3 回答 3

3

Here is a quick way to store value in bash array variable.

x=("" $(awk -F"[()]" '{printf "%s ",$2}' file))
y=("" $(awk -F"[()]" '{printf "%s ",$4}' file))

echo ${x[2]}
134123

If you are going to use these data for more jobs, I would have done it in awk. Then you can use internal array in awk

awk -F"[()]" '{x[NR]=$2;y[NR]=$4}' file
于 2013-11-08T17:30:30.337 回答
2
#!/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并被y声明为数组。然后循环输入文件并对输入文件中的sed每一个调用一个命令line

x+=(data)将值附加data到数组x。我们没有将我们想要存储在数组中的值写入,而是使用命令替换,这是用 完成的$(command),而不是将字面含义附加$(command)到数组中,而是执行命令并将其返回值存储在数组中。

让我们看一下sed命令:

's' 是替换命令,[^(]*我们想匹配所有内容,除了(,然后我们匹配(。我们想要存储在数组中的以下字符,为此我们使用\(\),我们稍后可以再次引用它(使用\1)。号码与 匹配[0-9]*。最后,我们将右括号)和其他所有内容与.*. 然后我们用 替换我们匹配的所有内容(整行),\1这正是我们在 and 之间\(的内容\)

如果您不熟悉sed,这可能会非常令人困惑,因为阅读sed语法需要一些时间。

第二个sed命令非常相似。

于 2013-11-08T17:16:14.007 回答
1

How do I extract the value of the 'i'th line on the 'j'th string, and save it to a variable 'x'?

Try using awk

x=$(awk -v i=$i -v j=$j ' NR==i {print $j; exit}' file.dat)

I want to extract the number in brackets and save it to a variable for example the first one in line one to be 'x1', the second on the same line to be 'y1', for line 2 'x2' and 'y2', and so on...

Using awk

x=($(awk -F'[()]' '{print $2}' file.dat))
y=($(awk -F'[()]' '{print $4}' file.dat))

x1 can be accessed as ${x[0]} and y1 as ${y[0]}, likewise for other sequence of variables.

于 2013-11-08T17:29:37.817 回答