How to print the lines between two pattern in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print the lines between two pattern in a file?
# 1  
Old 10-10-2014
How to print the lines between two pattern in a file?

file like:
Code:
 services:
        XXXXXXXX:
          XXXXXXX:
           XXXXXXXXX1
         XXXXX
          XXXXXXXXX
      DDDDDDDDDDD
     BBBBB:
      version: 11.2.0.4
      services:
      YYYYYYYYYY
         XXXXXXXXXX:
           XXXXXXXXX
         XXXXXXXX
           XXXXXXXXX

I tried:
Code:
 sed -n  "/services/,/version/p"  test.txt

expect
Code:
 services:
        XXXXXXXX:
          XXXXXXX:
           XXXXXXXXX1
         XXXXX
          XXXXXXXXX
      DDDDDDDDDDD
     BBBBB:
      version: 11.2.0.4

but got all lines.

can anyone help please?

Last edited by Franklin52; 10-10-2014 at 07:30 AM.. Reason: Please use code tags
# 2  
Old 10-10-2014
Quote:
Originally Posted by netbanker
file like:

services:
XXXXXXXX:
XXXXXXX:
XXXXXXXXX1
XXXXX
XXXXXXXXX
DDDDDDDDDDD
BBBBB:
version: 11.2.0.4
services:
YYYYYYYYYY
XXXXXXXXXX:
XXXXXXXXX
XXXXXXXX
XXXXXXXXX

I tried:

sed -n "/services/,/version/p" test.txt


expect

services:
XXXXXXXX:
XXXXXXX:
XXXXXXXXX1
XXXXX
XXXXXXXXX
DDDDDDDDDDD
BBBBB:
version: 11.2.0.4

but got all lines.

can anyone help please?
Hello netbanker,

Kindly try the following for same and let me know if this helps. Also please always use code tags in your posts for commands and codes,
go through the rules of forum on same link is as follows.
https://www.unix.com/misc.php?do=cfrules

Code:
awk '/version/ {B=B?B ORS $0:$0;f=0}  /services/ {f=1;} f{B=B?B ORS $0:$0} !f{print B;B=""}'  Input_file

Output will be as follows.
Code:
services:
XXXXXXXX:
XXXXXXX:
XXXXXXXXX1
XXXXX
XXXXXXXXX
DDDDDDDDDDD
BBBBB:
version: 11.2.0.4

Thanks,
R. Singh
# 3  
Old 10-10-2014
Singh,

Thank you so much, it did the trick!

btw, any hint why sed not working in my case?
# 4  
Old 10-10-2014
You tell sed to print everything between services and version.
But after the version there is another services, so sed prints from that one, too, until it reaches the next version or the end of the file.
A more simple way of only printing the first block, is to exit at the block end.
Code:
awk '/services/ {f=1} f==1; /version/ {exit}' test.txt

sed does not have variables to store a state; a work-around is to read the next lines in a loop.
Code:
sed -e '/services/!d' -e ':L1' -e 'n;/version/q;bL1' test.txt


Last edited by MadeInGermany; 10-10-2014 at 06:09 AM..
# 5  
Old 10-10-2014
Try

Code:
awk 'function p(){if(s && /version/){ print l ; s = l = "" }}{s += /services/ ; if(s)l = ( length(l) ) ? l RS $0 : $0; p() }END{ p() }' file

# 6  
Old 10-10-2014
Or
Code:
sed -n '/services/,$p;/version/q' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 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. 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

4. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

5. Shell Programming and Scripting

Want to print out lines with a matching pattern from file

Hi all, I want to search for strings in file1 that can be found in file2 and print out the whole line when matching pattern is found. I have used the below command, but this is not working for me, because it is writing out only the matching patterns from file2, not the whole line. fgrep -o... (2 Replies)
Discussion started by: MonikaB
2 Replies

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

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. UNIX for Dummies Questions & Answers

print multiple lines from text file based on pattern list

I have a text file with a list of items/patterns: ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig12238 ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig34624... (1 Reply)
Discussion started by: Oyster
1 Replies

9. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies
Login or Register to Ask a Question