sed problem - delete all lines until a match on 2 lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed problem - delete all lines until a match on 2 lines
# 1  
Old 09-17-2009
Bug sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible )

I got a file (trace file to recreate the control file from oracle for the dba boys)
which contains

Code:
some lines
another line
...
STARTUP NOMOUNT
some lines
another line
...
a line ending with ;
STARTUP NOMOUNT
some lines
another line
...
a line ending with ;

The file contains also some lines in comment in between the others. These lines start with '#'

I want the part from the first "STARTUP NOMOUNT" untill the line before the second "STARTUP NOMOUNT" in 1 file and the part from the second "STARTUP NOMOUNT" untill the end in another file.

I know how to do this in ksh and I guess I would quickly find a solution in perl also but, now it's time to sharpen my sed skills Smilie

I already gor this

Code:
sed '/^STARTUP NOMOUNT/,/^STARTUP NOMOUNT/!d' ikke | sed '/^#/d' | sed '$d'

This will delete all the lines except those between the 2 occurences on "STARTUP NOMOUNT", next it will delete the lines in comment and finally it will delete the last line which is the second occurence of "STARTUP NOMOUNT". So, this gives me already 1 file with the first part.

But, how do I get the second part ?

I already tried
Code:
sed '/^STARTUP NOMOUNT/,$!d' ikke | sed '/^#/d' | sed '1,/^STARTUP NOMOUNT/d'

But, this deletes 1 line too many

Anyone an idea ?
# 2  
Old 09-17-2009
I couldnt get your question very clearly...

If you give input and expected output, it would be useful for us.
# 3  
Old 09-17-2009
Simply with awk:

Code:
awk '/STARTUP NOMOUNT/{i++}i{print > "file_" i}' infile

# 4  
Old 09-17-2009
Franklin52,
nice trick, can you give some explication ?
Having the code is nice but, I would like to understand it also :-)
This makes it eassier to build on if other situations should occure

By the way, do you know a way to do this with sed ?
# 5  
Old 09-17-2009
The awk code is very good. I modified it to not print the comments, as I think you had indicated you don't want those to print, should they show up between the STARTUP NOMOUNT and the line with the ";".

Code:
 
awk '/STARTUP NOMOUNT/{i++} i && $1 !~ /^#/ {print > "file_" i}' infile

# 6  
Old 09-17-2009
Quote:
Originally Posted by plelie2
Franklin52,
nice trick, can you give some explication ?
Having the code is nice but, I would like to understand it also :-)
This makes it eassier to build on if other situations should occure

By the way, do you know a way to do this with sed ?
To get the second section with sed you can first delete the first section with:
Code:
sed '/STARTUP NOMOUNT/,/STARTUP NOMOUNT/!d' ikke > temp

Get the second section:

Code:
sed -n '/STARTUP NOMOUNT/,/STARTUP NOMOUNT/p' temp > file2

With sed it's much more complicated then awk, so I think awk is *the* tool for such tasks:

Code:
awk '/STARTUP NOMOUNT/{i++}i{print > "file_" i}' infile

Explanation:

Code:
/STARTUP NOMOUNT/{i++}

If the pattern is match we increase a counter i

Code:
i{print > "file_" i}

If i is true (not 0) print the current line to the file file_i

Regards
# 7  
Old 09-17-2009
sed -n `grep -n <reg_pattern> <filename> | head -2 | cut -d":" -f1`,`wc -l <filename> | cut -d" " -f1`p <filename>

This is for your satisfaction,.... this would work fine but not recommended... ppl hav given ideal solutions

Last edited by know d unknown; 09-17-2009 at 12:55 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete lines based on pattern match

BASH in Solaris 10 I have a log file like below. Whenever the pattern ORA-39083 is encountered, I want to delete the line which has this pattern and 3 lines below it. $ cat someLogfile.txt ORA-39083: Object type OBJECT_GRANT failed to create with error: ORA-01917: user or role 'CMPA' does... (4 Replies)
Discussion started by: kraljic
4 Replies

2. Shell Programming and Scripting

sed read X lines after match

I want to do something like sed -n '/PATTERN/,+10p' and get the ten lines following PATTERN. However, this throws an "expected context address" with the sed that comes with OSX Lion. If that + is a GNUism, can I do this, or do I have to find another tool? (2 Replies)
Discussion started by: jnojr
2 Replies

3. UNIX for Dummies Questions & Answers

sed, join lines that do not match pattern

Hello, Could someone help me with sed. I have searched for solution 5 days allready :wall:, but cant find. Unfortunately my "sed" knowledge not good enough to manage it. I have the text: 123, foo1, bar1, short text1, dat1e, stable_pattern 124, foo2, bar2, long text with few lines, date,... (4 Replies)
Discussion started by: petrasl
4 Replies

4. Shell Programming and Scripting

Sed delete blank lines upto first pattern match

Hi Im trying to do the following in sed. I want to delete any blank line at the start of a file until it matches a pattern and then stops. for example: Input output: I have got it to work within a range of two patterns with the following: sed '/1/,/pattern/{/^]*$/d}' The... (2 Replies)
Discussion started by: duonut
2 Replies

5. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

6. Shell Programming and Scripting

Delete lines line by match data 2 file.

i need to delete the lines is match from file data 1 & data 2 please help? data 1 4825307 4825308 4825248 4825309 4825310 4825311 4825336 data 2 4825248 0100362210 Time4Meal 39.00 41.73 MO & MT MT SMS 4825305 0100367565... (2 Replies)
Discussion started by: ooilinlove
2 Replies

7. Shell Programming and Scripting

delete block of lines when pattern does not match

I have this input file that I need to remove lines which represents more than 30 days of processing. Input file: On 11/17/2009 at 12:30:00, Program started processing...argc=7 Total number of bytes in file being processed is 390 Message buffer of length=390 was allocated successfully... (1 Reply)
Discussion started by: udelalv
1 Replies

8. Shell Programming and Scripting

sed print all lines after pattern match

HiCan someone show me how to print all lines from a file after a line matching a pattern using sed?Thanks (13 Replies)
Discussion started by: steadyonabix
13 Replies

9. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies

10. UNIX for Dummies Questions & Answers

How to delete lines do NOT match a pattern

On Unix, it is easy to get those lines that match a pattern, by grep pattern file or those lines that do not, by grep -v pattern file but I am editing a file on Windows with Ultraedit. Ultraedit support regular expression based search and replace. I can delete all the lines that match a... (1 Reply)
Discussion started by: JumboGeng
1 Replies
Login or Register to Ask a Question