How to extract a string, number, or word from a line or database and save it to a variable? (script in bash)

StackOverflow https://stackoverflow.com/questions/19864380

  •  29-07-2022
  •  | 
  •  

سؤال

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'?

هل كانت مفيدة؟

المحلول 3

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.

نصائح أخرى

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
#!/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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top