Computing.Net > Forums > Unix > Search text between...text

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Search text between...text

Reply to Message Icon

Name: maxlux
Date: March 5, 2003 at 10:32:31 Pacific
OS: linux
CPU/Ram: k6II-400/256
Comment:

Hello,

i've to read option between two points, like that :
[AA]
option1 = text
option2 = text
[/AA]

i don't know read text which is multiline, i only read text between two points...but on the same line .

thank you,

Max



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: March 6, 2003 at 08:24:36 Pacific
Reply:

Max:

It sounds like you need to use a range pattern either in sed or awk. Since your begin flag is [AA] and your end flag is [/AA]
you have to ecape with a \, the special characters [ ] /


sed -n "/\[AA\]/,/\[\/AA\]/p" data.file

or

awk ' /\[AA\]/,/\[\/AA\]/ ' data.file

Regards,

Nails


0

Response Number 2
Name: James Boothe
Date: March 6, 2003 at 12:28:00 Pacific
Reply:

Those range searches come in handy. But they include the beginning and ending lines that contain the patterns. When you want only what is in between, you need more work. Another warning is that both sed and awk will start printing a group not knowing if it will be terminated by the end range or not.

The following awk solution will print only what is in between your range lines, and I took the easy road and hard-coded the search patterns. Like awk and sed, it starts printing not knowing if it will reach an end-range line or not.

awk '{\
if (inprog==1)
  if (match($0,"\[/AA\]"))
     inprog=0   # or "exit" at end of first group
  else
     print
else
   if (match($0,"\[AA\]"))
      inprog=1
}' data.file

As coded, like the awk and sed range search, it will process as many groups as it finds in the file, but if you want it to quit after printing the first group, just change the inprog=0 statement to exit.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


How to change IPs in SCO ... printer emulation



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Search text between...text

AWK or some search www.computing.net/answers/unix/awk-or-some-search/8114.html

sed truncate the result www.computing.net/answers/unix/sed-truncate-the-result/7426.html

unix or korn www.computing.net/answers/unix/unix-or-korn/6388.html