I have a file formatted like this:
[SITE1]
north
west
[MOTOR]
west
south
north
[AREA]
west
east
north
[CLEAR]
What I need to be able to do is read all values from a specific section.
Eg: read AREA and be returned:
west
east
north
The examples I've found online are for ini files, which have key value pairs. Can anyone help advise how I can do this ?
Thanks
Using sed
:
category=MOTOR; sed -nE "/^\[$category\]$/{:l n;/^(\[.*\])?$/q;p;bl}" /path/to/your/file
It doesn't do anything until it matches a line that consists of your target category, at which point it enters a loop. In this loop, it consumes a line, exits if it's an empty line or another category (or the end of the file) and otherwise prints the line.
The sed
commands used are the following :
/pattern/
executes the next command or group of commands when the current line matches the pattern{commands}
regroups commands, for instance to execute them conditionally.:l
defines a label named "l", to which you'll be able to jump to.n
asks sed
to start working on the next line.q
exitsp
prints the current linebl
jumps to the "l" labelYou can try it here.