4

I am trying to read a file and search for a string using grep. Once I find the string, I want to read everything after the string until I match another string. So in my example, I am searching for ...SUMMARY... and I want to read everything until the occurrence of ... Here is an example:

**...SUMMARY...**
   Severe thunderstorms are most likely across north-central/northeast
   Texas and the Ark-La-Tex region during the late afternoon and
   evening. Destructive hail and wind, along with a few tornadoes are
   possible. Severe thunderstorms are also expected across the
   Mid-South and Ohio Valley.

   **...**North-central/northeast TX and southeast OK/ArkLaTex...
   In the wake of a decaying MCS across the Lower Mississippi River
   Valley, a northwestward-extending outflow boundary will continue to
   modify/drift northward with rapid/strong destabilization this
   afternoon particularly along and south of it. A quick
   reestablishment of lower/some middle 70s F surface dewpoints will
   occur into prior-MCS-impacted areas, with MLCAPE in excess of 4000
   J/kg expected for parts of north-central/northeast Texas into far
   southeast Oklahoma and the nearby ArkLaTex. Special 19Z observed
   soundings are expected from Fort Worth/Shreveport to help better
   gauge/confirm this destabilization trend and the degree of capping.

I have tried using the following code but only displays the ...SUMMARY... and the next line.

sed -n '/...SUMMARY.../,/.../p' 

What can I do to solve this?

======================================================================= Followup:

This is the result I am trying to get. Only show the paragraph under ...SUMMARY... and end at the next ... so this is what I should get in the end:

Severe thunderstorms are most likely across north-central/northeast Texas and the Ark-La-Tex region during the late afternoon and evening. Destructive hail and wind, along with a few tornadoes are possible. Severe thunderstorms are also expected across the Mid-South and Ohio Valley.

I have tried the following based on a recommendation Shellter:

sed -n '/...SUMMARY.../,/**...**/p'

But I get everything.

wxmikey
  • 43
  • 7
  • A `.` is a special character in a regex. See: [The Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/3776858) – Cyrus Jun 19 '19 at 17:46
  • 1
    Possible duplicate of [How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?](https://stackoverflow.com/questions/38972736/how-to-print-lines-between-two-patterns-inclusive-or-exclusive-in-sed-awk-or) – tripleee Jun 19 '19 at 17:56
  • Running the following command, I get these results: sed -n '/...SUMMARY.../,/^.../p' dspc ...SUMMARY... Severe thunderstorms are most likely across north-central/northeast So it does not get the entire wording. – wxmikey Jun 19 '19 at 18:40
  • I think your `,/.../p` is matching the same (first) line. I used `,/\*\*...\*\*/p'` and got the whole paragraph after the first line, but it also has the first line of the next paragraph. You could prefilter you text so the second sentinal is on a separate line, but I would just add a trailing filter to clean off that last line. Good first Q, but is always helps reduce ambiguity if you include your expected output from your provided input. Good luck. – shellter Jun 19 '19 at 20:00
  • Thanks Shellter That works well actually too well. I get everything from the ...SUMMARY... including the following text and everything else. I am looking to strip off the ...SUMMARY... and just show the paragraph below that and then nothing else. This below is that I am trying to achieve, Severe thunderstorms are most likely across north-central/northeast Texas and the Ark-La-Tex region during the late afternoon and evening. Destructive hail and wind, along with a few tornadoes are possible. Severe thunderstorms are also expected across the Mid-South and Ohio Valley. – wxmikey Jun 21 '19 at 12:52

1 Answers1

1

You may use

sed -n '/^[[:blank:]]*\.\.\.SUMMARY\.\.\./,/^[[:blank:]]*\.\.\./{//!p;}' file

See this online sed demo.

NOTES:

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank you so much for your help. When I run that command in your example, I get the following: Severe thunderstorms should be most concentrated and intense today over parts of southern Oklahoma and north Texas into the Arklatex region and nearby Arkansas. Damaging hail, severe gusts and a few tornadoes are possible. ...Synopsis... An uncommonly active upper-air pattern for its latitude and time of year, around summer solstice, is in place over the CONUS. The most relevant feature is a strong mid/upper-level shortwave trough -- now – wxmikey Jun 23 '19 at 14:17
  • It continues on to ...Synopsis... and reads the rest of the file. Does not stop. Thoughts? – wxmikey Jun 23 '19 at 14:18
  • @wxmikey Do you mean to say there are NO asterisks in your input text? Then rely only on the fact the `...` appears at the start of the line, use `sed -n '/^\.\.\.SUMMARY\.\.\./,/^\.\.\./{//!p;}'`, see [demo](https://ideone.com/ydxhz2). – Wiktor Stribiżew Jun 23 '19 at 21:03
  • Yes, there are NO asterisks in the text input. I was trying to bold them in the post to highlight the section, but failed. am not sure what is going on. I get everything or nothing at all with that sed command. I am pulling the data from a website: https://www.spc.noaa.gov/products/outlook/day1otlk.html and stripping the html code out first and then going after the ...SUMMARY... section with no success. I am very grateful for everyones help! – wxmikey Jun 24 '19 at 12:40
  • Here is my script: #!/bin/bash wget -O spc_day1 "https://www.spc.noaa.gov/products/outlook/day1otlk.html" cat spc_day1 | sed -e 's/<[^>]*>//g' > day1_spc DAY1=`cat day1_spc` sed -n '/^\.\.\.SUMMARY\.\.\./,/^\.\.\./{//!p;}' <<< "$DAY1" – wxmikey Jun 24 '19 at 13:04
  • @wxmikey I suspect those dots are not at the start of a line, but after leading whitespace. Please check my answer update. – Wiktor Stribiżew Jun 24 '19 at 13:31
  • That was it. Great catch! How did you figure that out? – wxmikey Jun 24 '19 at 13:37
  • @wxmikey Checked the HTML source of the page. – Wiktor Stribiżew Jun 24 '19 at 13:38
  • I have another project that I am working on and may need your assistance – wxmikey Jun 24 '19 at 13:53
  • @wxmikey Just check via Google if there is no such an answer on SO, then post a well explained question, provide all necessary data to repro the issue. – Wiktor Stribiżew Jun 24 '19 at 13:57
  • Will do! Thank you so much! – wxmikey Jun 24 '19 at 14:55