[Solved] Printing a part of the last line of the specific part of a file

Hi,

I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this:

Name =A
xxxxxx
yyyyyy
zzzzzz
aaaaaa
bbbbbb
Value = 57

This is necessary because in a file there are written more lines which contains a string "Value =[...]"

Next, I want to get the value 57 and assign it to a variable (this is an example because each file has different value)

I tried to use this command:

VALUE=`sed -n '/Name = A/,/Value =/p' $file | tail -1 | gawk '{printf $3"\n"}'`

Unfortunately, this command runs very slow for 80 files.

I tried also to use this command:

VALUE=`sed -n '/Name = A/{n;n;n;n;n;n;s/\(.*\)= //;p;}'`

However, I'd rather not define the amount of lines which should be skipped because it is possible to insert a newline in that file and in this case the script won't work.

Can anyone help me with this?
Thanks

awk -F= '$1 ~ "Name" {n=$2;next} n && $1~"Value"{print n,$2;n=""} OFS== myFile

Thank you for your quick reply

However, there is unmatched ' , wherever I try to put the second ' i receive the output which says that there is an syntax error. Could you please help me with this?

sed -n -r '/Name =A/,/Value =/{/Value = (.*)/ s//\1/p}' $file
1 Like
awk 'BEGIN{RS="Name";} {for(i=1;i<=NF;i++){if($i=="Value"){i+=2;print $i;break;}}}' input_file
1 Like

sorryy, fat fingers...

1 Like

Thank you very much, now it works fine