Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern
# 8  
Old 06-04-2014
All you need is egrep -A piped into grep -v. Set the -A for the number of rows that you want to see after the row you are looking for and use grep -v to filter the row that you were looking for. you don't need sed or a for loop.

Code:
$ egrep -A1 "alias top" .bashrc
alias top='xtitle Processes on $HOST && top'
alias make='xtitle Making $(basename $PWD) ; make'

$ egrep -A1 "alias top" .bashrc | grep -v "alias top"
alias make='xtitle Making $(basename $PWD) ; make'

# 9  
Old 06-05-2014
My server doesn't support -A on grep. Besides, the statement that I've written is already a complex one and I want to use sed as part of multiple piped commands. Using 3-4 lines of code there would not be feasible.

Quote:
Originally Posted by rbatte1
Does your server support the -A flag on grep? That might be a neater solution:-
Code:
grep -A10 pattern  file

Maybe I've missed the point. If you want all lines after a certain point, perhaps you could use grep to get the record number and then tail to get you the output you need:-
Code:
#!/bin/ksh
 
grep -n pattern  file|tr ":" " "|read line xxx       # Get the interesting line number
wc -l file | read total xxx                          # Get total lines
((readrecs=$total-$line))                            # Work out how many lines from end to read
tail -$readrecs   file


Do either of these help?


Robin
---------- Post updated at 02:04 AM ---------- Previous update was at 01:51 AM ----------

Made it work somehow.. although still looking for a better solution. Here's what I am using for the time being:

Code:
sed -n '/Conn.*User/,${//!p;}'|sed '1d'

# 10  
Old 06-05-2014
Quote:
Originally Posted by essem
[..]

Made it work somehow.. although still looking for a better solution. Here's what I am using for the time being:

Code:
sed -n '/Conn.*User/,${//!p;}'|sed '1d'

So have the requirements changed? This will remove the line that contains the pattern plus the line after that line, so effectively the first two lines.....
# 11  
Old 06-05-2014
The first sed command searches for the pattern and prints the lines after the pattern match (including the line that contains the pattern).

The second sed command (after pipe) deletes the first line from the output of the first sed command.

The requirement is still the same. This is just a workaround until I find a real fix.

Quote:
Originally Posted by Scrutinizer
So have the requirements changed? This will remove the line that contains the pattern plus the line after that line, so effectively the first two lines.....
# 12  
Old 06-05-2014
No, the first sed command (sed -n '/Conn.*User/,${//!p;}') prints the lines after the pattern match excluding the line(s) that contain the pattern.

What is your OS and version?

Last edited by Scrutinizer; 06-05-2014 at 06:08 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX awk pattern matching and printing lines

I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt Page 2015-01-01 2000 Colors 2015-02-01 3000 Landing 2015-03-02 4000 #!/bin/sh LOG=/tmp/maillog.txt... (1 Reply)
Discussion started by: close2jay
1 Replies

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

3. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

4. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

5. UNIX for Dummies Questions & Answers

Sed: Adding new line after matching pattern

Hi I just wanted to add a new line after every matching pattern: The method doing this doesn't matter, however, I have been using sed and this is what I tried doing, knowing that I am a bit off: sed 'Wf a\'/n'/g' Basically, I want to add a new line after occurrence of Wf. After the line Wf... (5 Replies)
Discussion started by: MIA651
5 Replies

6. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

7. Shell Programming and Scripting

print lines up to pattern excluding pattern

11 22 33 44 55 66 77 When pattern 55 is met, print upto it, so output is 11 22 33 44 (1 Reply)
Discussion started by: anilcliff
1 Replies

8. Shell Programming and Scripting

How to use sed to modify a line above or below matching pattern?

I couldn't figure out how to use sed or any other shell to do the following. Can anyone help? Thanks. If seeing a string (e.g., TODAY) in the line, replace a string in the line above (e.g, replace "Raining" with "Sunny") and replace a string in the line below (e.g., replace "Reading" with... (7 Replies)
Discussion started by: sprinner
7 Replies

9. Shell Programming and Scripting

Printing previous line based on pattern using sed

Hi, I have a written a shell script to get the previous line based on the pattern. For example if a file has below lines: ---------------------------------------------- #UNBLOCK_As _per #As per 205.162.42.92 #BLOCK_As_per #----------------------- #input checks abc.com... (5 Replies)
Discussion started by: Anjan1
5 Replies

10. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies
Login or Register to Ask a Question