There are some good answers here,
but I see only one that incorporates the
Val part of the problem,
and it’s ambiguous whether that is correct.
I agree that
awk is “an amazing tool”, but it’s not necessary here;
I believe that this
sed -n '/\[part1\]/,/\[part2\]/s/.*Val.*=//p' "$file"
probably does what’s desired.
Like the other
sed -e '/\[part1\]/,/\[part2\]/p' solutions
this is trivially adaptable to select any section.
(You do, of course, need to know its name; if you know only its ordinal number,
you can adapt terdon’s answer
or glenn jackman’s answer,
both of which count sections rather than looking for a specific name.)
If you don’t know the name of section following, you can do
sed -n '/\[part42\]/,/\[part/s…' "$file"
My only meta-question is regarding the
cut -d'=' -f2 part of the question.
If an input line that we’re extracting data from contains multiple
Val (i.e., the field value contains
= character(s)), e.g.,
Einstein.Val = E=mc^2
then the above
cut command will extract only the text between the first and second
(i.e., the field value, up to (but not including) the first
sed command I have presented above will extract only the text
after the last
To get everything after the first
sed -n '/\[part1\]/,/\[part2\]/s/.*Val[^=]*=//p' "$file"
To mimic the behavior of the
sed -n '/\[part1\]/,/\[part2\]/s/.*Val[^=]*=\([^=]*\).*/\1/p' "$file"
Note that my approach assumes that the data look
at least generally like the illustration in the question;
i.e., at least one
= appears somewhere to the right of the
Accordingly, all of my solutions will ignore input like
Girl.Name = Valerie
even if it falls between