sed to grab first instance of a range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to grab first instance of a range
# 1  
Old 04-08-2015
sed to grab first instance of a range

Hi all,
I'm new to the forum and also relatively new to sed and other such wonderfully epic tools.
I'm attempting to grab a section of text between two words, but it seems to match all instances of the range instead of stopping at just the first.

This occurs when I use:
Code:
sed -n "/Index/,/<\/tr>/p" index.html

It returns:
Code:
 Index of Consumer Sentiment
          </td>
          <td class="em">
            93.0
          </td>
          <td class="">
            95.4
          </td>
          <td class="">
            80.0
          </td>
          <td class="">
            -2.50%
          </td>
          <td class="">
            16.30%
          </td>
      </tr>
            Index of Consumer Expectations
          </td>
          <td class="em">
            85.3
          </td>
          <td class="">
            88.0
          </td>
          <td class="">
            70.0
          </td>
          <td class="">
            -3.10%
          </td>
          <td class="">
            21.90%
          </td>
      </tr>

When I seperate out the p option and add a q (which I believe is the right way to quit after the first instance), it seems to quit after the first word match instead of the first range match:

Code:
sed -n "/Index/,/<\/tr>/{p;q}" index.html

Code:
Index of Consumer Sentiment

What I'm expecting is for it to stop at the first instance of the word Index, and print until the next instance of </tr>.

I guess I could be more literal and search for "Index of Consumer Sentiment" instead of just Index but I was hoping to keep the code as short as possible and rather not rely on a large literal string if possible.


Thanks in advance!
# 2  
Old 04-08-2015
Hi,
Try this (not tested):
Code:
sed -n '/Index/,/<\/tr>/p;/<\/tr>/q' index.html

Regards.
# 3  
Old 04-08-2015
returns nothing unfortunately :\

Not to worry, I'll just have to go with the literal string for now and hope that it doesn't change too often.
# 4  
Old 04-08-2015
Work fine at home, are you sure that your file index.html has newline between "Index" and "</tr>" ?

Regards.
# 5  
Old 04-08-2015
I might try converting the file to unix format first to make sure it parses correctly.
# 6  
Old 04-08-2015
But, either way, does it really have a newline between index and tr? Or did you pretty up the HTML before posting it?
# 7  
Old 04-08-2015
It does have a newline there yep, but it might not be reading it properly at the moment
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to grab data in range then search for pattern

im using the following code to grab data, but after the data in the range im specifying has been grabbed, i want to count how many instances of a particular pattern is found? awk 'BEGIN{count=0} /parmlib.*RSP/,/seqfiles.*SSD/ {print; count++ } /103 error in ata file/ END { print count }'... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Grab nth occurence in between two patterns using awk or sed

Hi , I have an issue where I want to parse through the output from a file and I want to grab the nth occurrence of text in between two patterns preferably using awk or sed ! TICKET NBR : 1 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2... (8 Replies)
Discussion started by: OTNA
8 Replies

3. Shell Programming and Scripting

sed for first instance of a pattern

Hi Everyone, I have the below information from a log file: LOAD SUMMARY ============ WRT_8036 Target: TGT_1_TAB (Instance Name: ) WRT_8039 Inserted rows - Requested: 3929 Applied: 0 Rejected: 3929 Affected: 0 Mutated from update: 3929 WRT_8041 Updated rows ... (7 Replies)
Discussion started by: galaxy_rocky
7 Replies

4. Shell Programming and Scripting

sed command to print first instance of pattern in range

The following text is in testFile.txt: one 5 two 10 three 15 four 20 five 25 six 10 seven 35 eight 10 nine 45 ten 50 I'd like to use sed to print the first occurance of search pattern /10/ in a given range. This command is to be run against large log files, so to optimize efficiency,... (9 Replies)
Discussion started by: uschaafm
9 Replies

5. Shell Programming and Scripting

Grab from file with sed

Hello All I have a file with this type of records: =LDR 01157nas a22003011a 4500 =001 vtls000000013 =003 VRT =005 20111020150800.0 =008 100128c19699999sp\a|||||\||||0\\\||spa| =037 \\$a1327$i090$j090$k03 =039 ... (14 Replies)
Discussion started by: ldiaz2106
14 Replies

6. Shell Programming and Scripting

sed appending needed only after first instance

Hi, Here is my piece of code used with sed in shell script: sed -i '/<falsemodule-option>/ a\<LdapLogin>' myxmlfile The problem that i am facing with the above is that in 'myxml' file i have mulitple instances of <falsemodule-option> so when i execute the above sed command, it is appending... (10 Replies)
Discussion started by: sunrexstar
10 Replies

7. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

8. Shell Programming and Scripting

sed command - substitue first instance

hi i have one file where i want to substitute only first instance of swap with swap1 i want to replcae only first instance of swap in my script i know we can do this with awk. but i need to do this with sed only i tried follwoing code sed 's/swap/swap1' filename but here all... (15 Replies)
Discussion started by: d_swapneel14
15 Replies

9. Shell Programming and Scripting

Sed on first instance only

Hi, I've been trying to figure this one out and found a post about this on the forum here but the solution didn't seem to work for me. Basically what I have is a file that looks something like: stuff morestuff 0 otherthing 0 etc I want to substitute for the 0 but what I want to... (9 Replies)
Discussion started by: eltinator
9 Replies

10. Shell Programming and Scripting

sed replace 2nd instance

Hello, I want to replace 2nd instance of "foo" in a file use sed. Any suggestions? (2 Replies)
Discussion started by: katrvu
2 Replies
Login or Register to Ask a Question