Sed Help.To Search Between Pattern1 And Pattern2 Containing Certain Text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed Help.To Search Between Pattern1 And Pattern2 Containing Certain Text
# 8  
Old 12-06-2008
One last question. Is there a way to print the lines excluding the pattern from the above example.
# 9  
Old 12-06-2008
Which lines shall be excluded? If you want the lines
containg "ALTER TABLE" and ";" deleted, try this:

Code:
/ALTER TABLE/{
N
/MOVE/!d
} 
/ALTER TABLE/,/;/{
/ALTER TABLE/D
/;/d
p
}

Save the code in a file and repeat the procedure from above.
# 10  
Old 12-06-2008
Hi,

Nope not quite this one is just removing the ALTER TABLE and ; lines from the pattern and displying this rest.

Sorry for the dup post but i have the problem complete description on this post

https://www.unix.com/shell-programmin...rint-rest.html

Here is the requirement.What we just got was a bunch of ALTER TABLE commands that have MOVE we identified all these lines from my input file TestFile as in the begin of thread.Now that is great.What i want is i want a sed script that would go after the my TestFile look for all the ALTER TABLE lines that contain MOVE and remove these lines starting from ALTER TABLE till it finds a ; and print the rest of the contents of my TestFile.

I have been trying this for 8 hrs now and no sucess any help is greatly appreciated.

Thanks and Best Regards,
Rajan.S
# 11  
Old 12-06-2008
Any responses please i am really stuck with this for a long time and in a fatal need of a resolution to this issue.

Thanks a ton in advance,

Thanks and kind regards,
Rajan
# 12  
Old 12-07-2008
This should work. It deletes everything from Alter Table entries
followed by move on the next line until "));".

Code:
sed '$!N;/ALTER.*\n.*MOVE/,/));/d' TestFile

or as sedfile

Code:
$!N
/ALTER.*\n.*MOVE/,/));/d

# 13  
Old 12-08-2008
Hi,

Hope it can help you.

getLinesAfterString: get certain number of lines after a matching pattern
getLinesBetweenString: get lines betweem two matching patterns
getLinesBetweenStringContainPattern: get lines between two matching patterns when the inside lines match one give pattern once or more. ( This function seems can address your request)

Code:
package LeoFile;
sub new{
	return bless {};
}
sub _open{
	my $file=shift;
	open FH,"<$file";
}
sub _close{
	close FH;
}
sub _checkPattern{
	my($ref,$pat)=(@_);
	@tmp=@{$ref};
	print "@{$ref}" if($matched==1);
}	
sub getLinesAfterString{
	shift;
	my($file,$str,$line)=(@_);
	_open($file);
	my $cnt;
	while(<FH>){
		$flag=1 if(m/$str/);
		if($flag && $cnt<$line){
			print $_;
			$cnt++;
		}
		else{
			exit;
		}
	}
	_close;
}
sub getLinesBetweenString{
	shift;
	my($file,$str1,$str2)=(@_);
	_open($file);
	while(<FH>){
		$flag=1 if(m/$str1/);
		print if ($flag==1);
		$flag=0 if(m/$str2/);
	}
	_close;
}
sub getLinesBetweenStringContainPattern{
	shift;
	my($file,$str1,$str2,$pat)=(@_);
	_open($file);
	while(<FH>){
		$flag=1 if(m/$str1/);
		push @arr,$_ if($flag==1);
		$matched=1 if(m/$pat/);
		if(m/$str2/){
			$flag=0;
			_checkPattern(\@arr,$matched);
			undef @arr;
			$matched=0;
		}
	}
	_close;
}
1

# 14  
Old 12-08-2008
Hi,

I am new to package concept in shell script could you quickly help me out how to use the subroutines.I am running these shell scripts on K shell.

Thanks,
Rajan
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get lines which has pattern1, pattern2 and pattern3 in it

Version: RHEL 6.5 In the below text file, I want to find the lines which has the string JOHN , KATE and STEVE in it. The logic is to grep with an AND condition ie. get all lines with JOHN AND KATE AND STEVE $ cat sometext.txt PHILIP worked in HR JOHN along with KATE fixed several IT... (4 Replies)
Discussion started by: John K
4 Replies

2. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

3. Shell Programming and Scripting

Delete Lines : after pattern1 and between pattern2 and pattern3 using awk/sed/perl

Hi I need to delete lines from a file which are after pattern1 and between pattern 2 and patter3, as below: aaaaaaaa bbbbbbbb pattern1 cdededed ddededed pattern2 fefefefe <-----Delete this line efefefef <-----Delete this line pattern3 adsffdsd huaserew Please can you suggest... (6 Replies)
Discussion started by: vk2012
6 Replies

4. Shell Programming and Scripting

Append text to line if begins with pattern1 AND does not end with pattern2

Hello, I'm looking for sed solution to change ... <li>keyword</li> <li>keyword <li>keyword</li> <li>keyword <li>keyword</li> ... to ... <li>keyword</li> <li>keyword</li> <li>keyword</li> <li>keyword</li> <li>keyword</li> ... I.e., if lines beginning with <li> do not end with... (3 Replies)
Discussion started by: pioavi
3 Replies

5. Shell Programming and Scripting

delete part of file from pattern1 to pattern2

Hi all! How can I delete all the text starting from <string1> to <string2> in all the .txt files of the folder "FOLDER" ? Thanks a lot! mjomba ... </s> <s> <w></w> </s> <s> ... to get: (1 Reply)
Discussion started by: mjomba
1 Replies

6. Shell Programming and Scripting

Search file for pattern2 starting from occurence of pattern1

Hi folks, I have a file which contains several occurences of 2 different patterns. I need to find out the line of first occurence of pattern2 starting after the position of first occurence of pattern1. example file: aaaa pattern2 bbbb pattern1 ccc pattern2 ddd pattern1 eee pattern2... (9 Replies)
Discussion started by: sameucho
9 Replies

7. Shell Programming and Scripting

How to remove a specific line matching pattern1 and pattern2 ?

Hi, I have a file like below: . . . . Jack is going home Jack is going to school Jack is sleeping Jack is eating dinner John is going home John is eating breakfast . . . The specific line is: Jack is going home (2 Replies)
Discussion started by: salih81
2 Replies

8. Shell Programming and Scripting

SED Exclude Matches Between Pattern1 And Pattern2 Containing Pattern3 Print The Rest

Hi, From the sample file below Conditions 1) Pattern Range must start with "ALTER TABLE" 2) Pattern Range ends when it finds ";" 3) Between this range i want to select all the patterns that contain pattern " MOVE " Note : I would like to exclude the above pattern matches and print... (1 Reply)
Discussion started by: rajan_san
1 Replies

9. Shell Programming and Scripting

grep all lines from PATTERN1 to PATTERN2

Hi! From a file like this one : hello ... PATTERN1 ... lines between patterns .. PATTERN2 ... I would like to extract only the lines between patterns, probably with awk I think? Thanks a lot for your help, Tipi (5 Replies)
Discussion started by: tipi
5 Replies

10. Shell Programming and Scripting

how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :- <bag name="mybag1" version="1.0"/> <contents id="coins"/> <bag name="mybag2" version="1.1"/> <contents id="clothes"/> <contents id="shoes"/> <bag name="mybag3" version="1.6"/> I want to delete line containing mybag2 and its subsequent... (5 Replies)
Discussion started by: repudi8or
5 Replies
Login or Register to Ask a Question