I have this kind of text file that I want to parse:
Host server1 option1 value1 option2 value2 option3 value3 ... Host server2 option1 value1 option2 value2 option3 value3 ... Host server3 option1 value1 ...
The goal is when I provide the name to the input, for example server2, it should return:
Host server2 option1 value1 option2 value2 option3 value3 ...
Then I tried to write the below regex and tested it on an online regex playground, it works as expected.
/((Host server2\n)(^ +.+\n*)+)/gm
But then I have no clue on how to apply this regex to some linux tools like
awk. I have tried
grep but it won't apply on multiline search. It does by using
-z flag but then it still won't make the regex work (tried using
-Pzo, didn't work). And I'm not that familiar with
awk (for regex use).
So my current solution is using
sed -n '/foo/,/bar/p' input_file:
string="server1" search=$(cat input_file | grep Host | grep -w -A1 "$string") foo=$(head -1 <<< "$search") bar=$(tail -1 <<< "$search") sed -n "/$foo/,/$bar/p" input_file | head -n -1
This works as expected but I want to see the regex approach. And one more thing, I don't want to install any additional tools.