sed print all lines after pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed print all lines after pattern match
# 8  
Old 12-30-2009
Quote:
Originally Posted by redraiment
Smilie, that's right! I take a mistake.
Hi redraiment,

Actually you were right, but if the pattern is on the first line (if there is no MOTD) then it will not work. Besides it is not a general solution as 0,.. only works for GNU sed:

Quote:
0,addr2
Start out in "matched first address" state, until addr2 is found. This is similar to
1,addr2, except that if addr2 matches the very first line of input the 0,addr2 form
will be at the end of its range, whereas the 1,addr2 form will still be at the begin-
ning of its range.
1,/pattern/ can be still be used if we use
Code:
printf '\nPARSE_FROM_HERE\n'

-or-
Code:
"echo;echo PARSE_FROM_HERE;...

instead.

Last edited by Scrutinizer; 12-30-2009 at 08:57 AM..
# 9  
Old 12-30-2009
Not sure if this is what you mean but this gives me MOTD and ls output with the PARSE_FROM_HERE string deleted: -
Code:
ssh [IP ADDRESS] "printf \"\nPARSE_FROM_HERE\n\";ls -lrt " |sed '1,/PARSE_FROM_HERE/d'

# 10  
Old 12-30-2009
Quote:
Originally Posted by Scrutinizer
Hi redraiment,

Actually you were right, but if the pattern is on the first line (if there is no MOTD) then it will not work. Besides it is not a general solution as 0,.. only works for GNU sed:



1,/pattern/ can be still be used if we use
Code:
printf '\nPARSE_FROM_HERE\n'

-or-
Code:
"echo;echo PARSE_FROM_HERE;...

instead.
Thanks! sed range pattern will include more than one line. But, using the line number, can be the same line. like:
Code:
sed '1,1d'

Smilie
# 11  
Old 12-30-2009
Quote:
Originally Posted by steadyonabix
Not sure if this is what you mean but this gives me MOTD and ls output with the PARSE_FROM_HERE string deleted
Normally the MOTD does not get printed if you are executing a remote command with ssh, unless there is something wrong with your config, or perhaps you mean the banner? What happens if you do:
Code:
ssh [IP ADDRESS] "ls -lrt" 2>/dev/null


Last edited by Scrutinizer; 12-30-2009 at 09:24 AM..
# 12  
Old 12-30-2009
Yes sorry I guess it is a banner as it never changes. The redirection does seem to work giving me the ls output with no banner, seems I have been over complicating the issue. I wanted to simplify some code at work that uses a C program to intercept the banner by catching the echo of PARSE_FROM_HERE. I thought it could be done with sed instead but it seems that even that was not necessary as your redirection gives me the desired output. Thanks for the help.

---------- Post updated at 02:36 PM ---------- Previous update was at 01:51 PM ----------

This seems to be the answer to what I wanted in that it gives me just the pure ls -lrt output with no banner and no total string. Thanks for all the help folks...
Code:
ssh [IP ADDRESS] "echo PARSE_FROM_HERE;ls -lrt" 2>/dev/null |sed '/PARSE_FROM_HERE/,/$/d'

# 13  
Old 12-30-2009
Hi, the total string comes from the ls command, so I would expect this to works too:
Code:
ssh [IP ADDRESS] ls -lrt 2>/dev/null | sed 1d

-or-
Code:
ssh [IP ADDRESS] "ls -ltr | sed 1d" 2>/dev/null


Last edited by Scrutinizer; 12-30-2009 at 10:59 AM..
# 14  
Old 12-30-2009
Yes they both work fine thanks, very compact. Much nicer and more readable than the original code. Having said that I don't understand the sed 1d, can you elaborate? Thanks

---------- Post updated at 03:22 PM ---------- Previous update was at 03:17 PM ----------

Ah got it, just deletes the first line. Thanks Smilie
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

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

4. Shell Programming and Scripting

Print lines that do not match the pattern

I need to print the lines that do not match a pattern. I tried using grep -v and sed -n '/pattern/!p', but both of them are not working as I am passing the pattern as variable and it can be null some times. Example ........ abcd...... .........abcd...... .........abcd......... (4 Replies)
Discussion started by: sunny1234
4 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

Print lines before and after pattern match

I am using Solaris, I want to print 3 lines before pattern match pattern 5 lines after pattern match Pattern is abcd to be searched in a.txt. Looking for the solution in sed/awk/perl. Thanks .. Input File a.txt: ================= 1 2 3 abcd 4 5 6 7 8 (7 Replies)
Discussion started by: manuswami
7 Replies

7. Shell Programming and Scripting

print lines with exact pattern match

I have in a file domain.com. 1909 IN A 1.22.33.44 domain.com. 1909 IN A 22.33.44.55 ns1.domain.com. 1699 IN A 33.44.55.66 ns2.domain.com. 1806 IN A 77.77.66.66 I need to "grep" or "awk" out the lines starting with domain.com. as follows. domain.com. 1909 IN A 1.22.33.44 domain.com.... (3 Replies)
Discussion started by: anilcliff
3 Replies

8. Shell Programming and Scripting

print chunk of lines only if there is a pattern match in between them

Hi All, Please find the sample file below: NAME ID NUMBER -------------------------------------------------------------------------------------------------- --------- abcdefgheija;lksdf ... (13 Replies)
Discussion started by: niel.verty
13 Replies

9. Shell Programming and Scripting

Perl script to match a pattern and print lines

Hi I have a file (say 'file1')and I want to search for a first occurence of pattern (say 'ERROR') and print ten lines in the file below pattern. I have to code it in PERL and I am using Solaris 5.9. I appreciate any help with code Thanks Ammu (6 Replies)
Discussion started by: ammu
6 Replies

10. Shell Programming and Scripting

Sed to delete exactly match pattern and print them in other file

Hi there, I need help about using sed. Iam using sed to delete and print lines that match the port number as listed in sedfile. I am using -d and -p command for delete match port and print them respectively. However, the output is not synchonize where the total deleted lines is not similar with... (3 Replies)
Discussion started by: new_buddy
3 Replies
Login or Register to Ask a Question