sed for first instance of a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed for first instance of a pattern
# 1  
Old 03-29-2013
sed for first instance of a pattern

Hi Everyone, I have the below information from a log file:

Code:
LOAD SUMMARY
============
 
WRT_8036 Target: TGT_1_TAB (Instance Name: [Shortcut_to_TST_1_TAB])
WRT_8039  Inserted rows - Requested: 3929       Applied: 0          Rejected:  3929       Affected: 0          Mutated from update: 3929
WRT_8041 Updated rows  - Requested: 17695      Applied: 17695      Rejected: 0          Affected: 17695
 
 
2013-03-28  17:50:00 : INFO : (30462 | WRITER_1_*_1) : (IS | XXX_XX_XX_XXX_G_XXXX) :  NODE01_XX_XX_xxxxxxxxx : WRT_8043 : *****END LOAD SESSION*****
LOAD SUMMARY
============
 
WRT_8036 Target: TGT_TAB_2 (Instance Name: [sc_TST_TAB_2])
WRT_8038 Inserted rows - Requested: 2          Applied: 2          Rejected: 0          Affected: 2
 
 
2013-03-28  17:50:11 : INFO : (30462 | WRITER_2_*_1) : (IS | XXX_XX_XX_XXX_G_XXXX) :  NODE01_XX_XX_xxxxxxxxx : WRT_8043 : *****END LOAD SESSION*****

i want to extract the first occurance of "LOAD SUMMARY" and "*****END LOAD SESSION*****" i.e output should be like :

Code:
LOAD SUMMARY
============
 
WRT_8036 Target: TGT_1_TAB (Instance Name: [Shortcut_to_TST_1_TAB])
WRT_8039  Inserted rows - Requested: 3929       Applied: 0          Rejected:  3929       Affected: 0          Mutated from update: 3929
WRT_8041 Updated rows  - Requested: 17695      Applied: 17695      Rejected: 0          Affected: 17695
 
 
2013-03-28  17:50:00 : INFO : (30462 | WRITER_1_*_1) : (IS | XXX_XX_XX_XXX_G_XXXX) :  NODE01_XX_XX_xxxxxxxxx : WRT_8043 : *****END LOAD SESSION****

*




Can you guys pls help me in achieving the above?


Regards,

Last edited by Scrutinizer; 03-29-2013 at 06:55 AM.. Reason: code tags
# 2  
Old 03-29-2013
That's a very interesting problem.
Code:
$ sed "/LOAD SUMMARY/,$ { /END LOAD SESSION/ q }" log
LOAD SUMMARY
============

WRT_8036 Target: TGT_1_TAB (Instance Name: [Shortcut_to_TST_1_TAB])
WRT_8039 Inserted rows - Requested: 3929 Applied: 0 Rejected: 3929 Affected: 0 Mutated from update: 3929
WRT_8041 Updated rows - Requested: 17695 Applied: 17695 Rejected: 0 Affected: 17695


2013-03-28 17:50:00 : INFO : (30462 | WRITER_1_*_1) : (IS | XXX_XX_XX_XXX_G_XXXX) : NODE01_XX_XX_xxxxxxxxx : WRT_8043 : *****END LOAD SESSION*****

# 3  
Old 03-29-2013
That works fine if LOAD SUMMARY is in the first line, but then you could skip the address range as well. Should there be lines to be suppressed before the first appearance of the token, try a minor adaption to hanson44's proposal:
Code:
$ sed -n '/LOAD SUMMARY/,$ {p; /END LOAD SESSION/ q }' file

# 4  
Old 03-29-2013
Note: with these sed suggestions, the closing curly brace needs to be preceded by a semi-colon ... q ;} or a newline to get it accepted by more seds.

Last edited by Scrutinizer; 03-29-2013 at 09:09 AM..
# 5  
Old 03-29-2013
Quote:
Originally Posted by Scrutinizer
Note: with these sed suggestions, the closing curly brace needs to be preceded by a semi-colon ... q ;}
I don't think that is true. It's OK to have a semicolon there, but not needed, AFAIK.

If the semicolon is needed, then why does it work without it? I'm ready to be educated.
# 6  
Old 03-29-2013
Quote:
Originally Posted by hanson44
I don't think that is true. It's OK to have a semicolon there, but not needed, AFAIK.

If the semicolon is needed, then why does it work without it? I'm ready to be educated.
Because the version of sed (GNU sed?) you are using happens to accept it without the semicolon or newline, but some other seds do not ..

--
(Not to be confused with awk which does not need one)

Last edited by Scrutinizer; 03-29-2013 at 09:15 AM..
# 7  
Old 03-29-2013
Quote:
Originally Posted by RudiC
Try a minor adaption to hanson44's proposal:
Code:
$ sed -n '/LOAD SUMMARY/,$ {p; /END LOAD SESSION/ q }' file

You are right. I originally wrote it as you suggest above. But I stripped it down to the simpler version because it worked on the test input. Should have considered more realistic test input, as you did. Smilie

---------- Post updated at 07:23 AM ---------- Previous update was at 07:17 AM ----------

Quote:
Originally Posted by Scrutinizer
Because the version of sed (GNU sed?) you are using happens to accept it without the semicolon or newline, but some other seds do not ..
Yes, I do use GNU sed. I had no idea that some other versions require the semicolon between a command and the closing curly bracket. Do you happen to know a particular version with that picky behavior?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

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: sed -n... (7 Replies)
Discussion started by: Lazarix
7 Replies

3. 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

4. Shell Programming and Scripting

Using sed can you specify the last instance of a character on a line?

I was told a way to do this with awk earlier today but is there a way with sed to specify the last instance of a character on a line? You will know what character you're looking for but there could be none or one hundred instances of it on a line say and you ONLY want to specify the last one for... (3 Replies)
Discussion started by: Bashingaway
3 Replies

5. Shell Programming and Scripting

sed: how to limit pattern search to first instance only

I need to reduce a file's size below 50MB by deleting chucks of text. The following sed does this. sed '/^begpattern/,/endpattern/d' myfile However, it's possible that the file size can get below 50MB by just deleting the first instance of the pattern. How do I code that into sed? Or can awk... (8 Replies)
Discussion started by: mariod1049
8 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. 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

8. 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

9. UNIX for Dummies Questions & Answers

Find first instance of pattern

I am using grep to find header info in a file and print it to another file. The header info only shows up on line 3 and 4 so i really don't need to search the remainder of the file. the grep -m option doesn't work with my version of UNIX. Also I'm new to UNIX so could you please explain what... (4 Replies)
Discussion started by: NobluesFDT
4 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