grep specific info from a file

M

Mr_Noob

Hi all,

What is the best way to grep into a file, only lines beginning with
"[" and ending with "]" ?
And then, how can I feed an array with the information contained
between the 2 brackets, without the 2 brackets?

here is a sample of my file :

....
[blah blah blah]
some stuff here

[blabla again]
here is some other stuff here too
....


What I'd like to obtain is :

....
bla blah blah
blabla again
....


thanks in advance for ur help
Regards
 
J

Jürgen Exner

Mr_Noob said:
What is the best way to grep into a file, only lines beginning with
"[" and ending with "]" ?
And then, how can I feed an array with the information contained
between the 2 brackets, without the 2 brackets?

Just loop through the file. For each line if it m//atches the condition (no
need for grep()) then push() the substr()ing portion into the array.

Other ways:
- to check for first/last character just extract them via substr() and
compare to "[" resp. "]"
- if you inlcude then center portion in your RE for m//, then you can group
it and just push $1 into the array.
- slurp the whole file into the array, then grep() for the right lines, then
discard leding/trailing square brackets

I am sure there are many more variation.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top