S
S
Sergey Ch2019-08-27 14:08:43
linux
Sergey Ch, 2019-08-27 14:08:43

How to process given text in bash?

Good day!
There was a problem parsing a config file using bash.
An example input file to be parsed:

RF='' 		# свободно в каталоге /
VF='' 		# свободно в каталоге /var


[keys1]
        key1='1'
        key2='2'
        key3='3'
        key4='4'
[keys2]
        key1='1'
        key2='2'
        key3='3'
        key4='4'

I would like to get all the text in the block by key for further processing using grep. For example, the block by key keys2 looks like this:
[keys2]
        key1='1'
        key2='2'
        key3='3'
        key4='4'

As far as I understand, sed cannot cope with this task, because it reads the text line by line. Perhaps there are some other utilities that can be used to do this. Or is it impossible to do without the use of normal EPs?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xibir, 2019-08-27
@ZooMik

sed -rn '/\[keys2\]/,/^[^ ]/{/ +/p}' file.txt

S
Saboteur, 2019-08-27
@saboteur_kiev

You can do this:
We search and display the lines starting from the one where we found "[keys2]" and up to the beginning of the next block.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question