fetching paragraphs with SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting fetching paragraphs with SED
# 1  
Old 02-02-2009
fetching paragraphs with SED

hi,
i am a SED newbie and i need some help. i have a log file as shown below. and i want to search specific Error Code, and fetch the whole paragraph.

[2009-02-01 15:42:51,630] ...
[2009-02-01 16:52:51,683] ...
.................
....ErrCode...
.................
[2009-02-01 17:49:33,686] ...
[2009-02-01 17:59:00,000] ...
[2009-02-01 18:52:50,000] ...
.................
....ErrCode...





let's say, i want to search "ErrCode" and it will show me that. how can i do this with SED :

[2009-02-02 16:52:51,683] ...
.................
....ErrCode...
.................
[2009-02-01 18:52:50,000] ...
.................
....ErrCode...





thanks..
# 2  
Old 02-02-2009
You'll need a beginning and ending paragraph search string.
This may be easier in awk, also....

But try:

Code:
sed -n '/first_string/,/second_string/p' file_in

# 3  
Old 02-02-2009
If your "paragraphs" are separated by blank lines:

Code:
sed -e '/./{H;$!d;}' -e 'x;/ErrCode/!d;'

(reference: http://sed.sourceforge.net/sed1line.txt. Very good reference for sed Smilie)
# 4  
Old 02-02-2009
i mean timestamps with "paragraphs". this is an error log as you guess.
# 5  
Old 02-02-2009
Input file looks like:

Code:
ant 1
bat 2
cat 3
dog 4
emu 5
fox 6
ant 7
bat 8
cat 9
dog 10
emu 11
fox 12
ant 13
bat 14
cat 15
dog 16
emu 17
fox 18
ant 19
bat 20
cat 21
dog 22
emu 23
fox 24

Substitute 'dog' and 'fox' with your search strings:

Code:

cat a |
awk '
  /dog/{ print_ind = 1; }
  { if ( print_ind == 1 ){ print; }}
  /fox/{ print_ind = 0; }'


Last edited by quirkasaurus; 02-02-2009 at 11:40 AM.. Reason: one too many print statements.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Center image between two text paragraphs.

I want to show a page with an image between 2 any paragraphs. I tried the following script. But the image is not centered. SUSE Paste <!DOCTYPE html> <html> <head> <style> center.center_1 { margin: auto; width: 60%; ... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Extract paragraphs and count them

Hi, I have a text with a number of paragraphs in them. My problem is I need to locate certain errors/warning and extract/count them. Problem is I do not know how many paras are there with that particular type of error/warning. I had thought that somehow if I could count the number of... (25 Replies)
Discussion started by: dsid
25 Replies

3. Shell Programming and Scripting

Formatting paragraphs with fmt with rule

Hi, i know that i can format a whole file using fmt -w 78 file > file_new Now, i would like to implement a rule in the following way: - If a paragraph starts with % (and ends with %), the paragraph should remain unchanged. - Otherwise the command stated above should be applied to the... (4 Replies)
Discussion started by: johnlocke85
4 Replies

4. Shell Programming and Scripting

Need help with sorting in paragraphs

I am very new to shell scripting, current try to do a sorting of a text file in paragraphs with ksh script. example: File content: A1100001 line 1 = "testing" line 2 = something, line 3 = 100 D1200003 line 1 = "testing" line 2 = something, line 3 = 100 B1200003 line 1 =... (3 Replies)
Discussion started by: gavin_L
3 Replies

5. Shell Programming and Scripting

Split text into paragraphs

Hi all! I want to make a code to split sentences into paragraphs maybe 4-5 sentences into one <p>text</p> there are no new lines in the text string any ideas with AWK, SSH? Thank you! (5 Replies)
Discussion started by: sanantonio7777
5 Replies

6. Shell Programming and Scripting

Extract paragraphs under conditions

Hi all, I want to extract some paragraphs out of a file under certain conditions. - The paragraph must start with 'fmri' - The paragraph must contain the string 'restarter svc:/system/svc/restarter:default' My input is like that : fmri svc:/system/vxpbx:default state_time Wed... (4 Replies)
Discussion started by: Armoric
4 Replies

7. Shell Programming and Scripting

file separated into paragraphs or pages

hi, i have file, file is separated into parahgraphs by these line(----------). i want to find out logId = string : "AIALARM", in each parahgraph or page if found then i want to cut next five lines.... ... (3 Replies)
Discussion started by: dodasajan
3 Replies

8. Shell Programming and Scripting

deleting text records with sed (sed paragraphs)

Hi all, First off, Thank you all for the knowledge I have gleaned from this site! Deleting Records from a text file... sed paragraphs The following code works nearly perfect, however each time it is run on the log file it adds a newline at the head of the file, run it 5 times, it'll have 5... (1 Reply)
Discussion started by: Festus Hagen
1 Replies

9. Shell Programming and Scripting

how to filter out some paragraphs in a file

Hi, I am trying to filter out those paragraphs that contains 'CONNECT', 'alter system switch logfile'. That means say the input file is : ------------------------------------------------------- Wed Jun 7 00:32:31 2006 ACTION : 'CONNECT' CLIENT USER: prdadm CLIENT TERMINAL: Wed Jun 7... (7 Replies)
Discussion started by: cnlhap
7 Replies

10. UNIX for Dummies Questions & Answers

Using sed to remove paragraphs with variables

Hi everyone, I have a file with multiple entries and I would like to remove the ones that contain either /A"> or /A/, where A can be any letter of the alphabet. Here's an example of the entries: <Topic r:id="Top/World/Fran"> <catid>476</catid> <link... (1 Reply)
Discussion started by: BlueberryPickle
1 Replies
Login or Register to Ask a Question