Visit Our UNIX and Linux User Community


Print lines between two strings multiple occurencies (with sed, awk, or grep)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines between two strings multiple occurencies (with sed, awk, or grep)
# 1  
Old 02-02-2012
Print lines between two strings multiple occurencies (with sed, awk, or grep)

Hello,

I can extract lines in a file, between two strings but only one time.
If there are multiple occurencies, my command show only one block.

Example, monfichier.txt contains :
Code:
debut_sect
texte L1
texte L2
texte L3
texte L4
fin_sect

donnees inutiles 1
donnees inutiles 2

debut_sect
texte L5
texte L6
texte L7
texte L8
fin_sect

I want to extract blocks which are between debut_sect and fin_sect.
Result expected is :
Code:
texte L1
texte L2
texte L3
texte L4
texte L5
texte L6
texte L7
texte L8

I've tried : sed -e '1,/debut_sect/d' -e '/fin_sect/,$d' monfichier.txt

But it shows only last block :
Code:
texte L5
texte L6
texte L7
texte L8

Thanks
# 2  
Old 02-02-2012
Code:
sed -n '/debut_sect/,/fin_sect/{/debut_sect\|fin_sect/d;p}' infile

--ahamed
# 3  
Old 02-02-2012
Hi theclem35,

Using perl:
Code:
$ cat infile
debut_sect
texte L1
texte L2
texte L3
texte L4
fin_sect

donnees inutiles 1
donnees inutiles 2

debut_sect
texte L5
texte L6
texte L7
texte L8
fin_sect
$ perl -ne 'if ( $flip_flop = ( m/debut_sect/ .. m/fin_sect/ ) ) { print if $flip_flop !~ m/(?:1|E0)/ }' infile
texte L1
texte L2
texte L3
texte L4
texte L5
texte L6
texte L7
texte L8

Regards,
Birei.
# 4  
Old 02-02-2012
Thanks a lot!
How can I add a string after each block ?
I tried sed -ne '/debut_sect/,/fin_sect/{/debut_sect\|fin_sect/d;p}' -ne '$a\\n---- END OF BLOCK ------\n' infile

But it add ---- END OF BLOCK ------ only one time, at the end of the file !
# 5  
Old 02-02-2012
The more complicated you make it, the tougher it gets to do it in sed, which doesn't have things programmers want like variables and conditional statements. awk does, though.

Code:
awk '
# If we get a line containing fin_sect, and P is nonzero,
# print --- END OF BLOCK ---, and set P=0.
# We have this before the P statement, so that this runs
# first, preventing fin_sect itself from being printed.
P && /fin_sect/ { print "--- END OF BLOCK ---" ; P=0 }

# Print only when variable P set to nonzero value.
P
# If we get a line containing debut_sect, set P=1.
/debut_sect/ { P=1 }' infile

# 6  
Old 02-02-2012
Code:
sed -n '/debut_sect/,/fin_sect/{s/fin_sect/--END OF BLOCK--/;/debut_sect/d;p}' infile

--ahamed
# 7  
Old 02-03-2012
Thank you so much Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #110
Difficulty: Easy
As of 2014, Apple's macOS was the largest Unix installed base globally.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or sed or grep filter a line and/or between strings

Hi, I have multiple files on a directory with the following content: blahblah blahblah hostname server1 blahblah blahblah ---BEGIN--- aaa bbb ccc ddd ---END--- blahblah blahblah blahblah I would like to filter all the files with awk or sed or something else so I can get below... (6 Replies)
Discussion started by: bayupw
6 Replies

2. UNIX for Beginners Questions & Answers

How to find=grep or maybe sed/awk for multiple lines of text?

Hi, I am running the following: PASS="username/password" sqlplus -s << EOF | grep -v "^$" $PASS set feedback off set heading off set termout off select name from v\$database ; exit EOF Which gives ERROR: ORA-28002: the password will expire within 5 days PSMP1 (1 Reply)
Discussion started by: newbie_01
1 Replies

3. Shell Programming and Scripting

How to print the lines between the pattern using awk/grep/sed?

Hi, I need a help to search a pattern and print the multiple lines between them. Input file: Tue May 29 12:30:33 EDT 2012:threadWebContainer : 357:com.travimp.hotelierlinks.abba.service.RequestHandler.requestService(String, ITICSDataSet): hotelCancelReservation request: ... (4 Replies)
Discussion started by: aroragaurav.84
4 Replies

4. Shell Programming and Scripting

Sed or Awk for lines between two strings multiple times and keep the last one

Hi, I am trying to get lines between the last occurrences of two patterns. I have files that have several occurrences of “Standard” and “Visual”. I will like to get the lines between “Standard” and “Visual” but I only want to retain only the last one e.g. Standard Some words Some words Some... (4 Replies)
Discussion started by: damanidada
4 Replies

5. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

6. UNIX for Dummies Questions & Answers

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys, say I have a few files in a directory (58 text files or somthing) each one contains mulitple strings that I wish to replace with other strings so in these 58 files I'm looking for say the following strings: JAM (replace with BUTTER) BREAD (replace with CRACKER) SCOOP (replace... (19 Replies)
Discussion started by: rich@ardz
19 Replies

7. Shell Programming and Scripting

print multiple lines using the grep command.

Hi All, Please find my piece of code below. I am trying to grep the word SUCCESS from $LOGFILE and storing in the grepvar variable. And i am placing that variable in a file. Now if i open the file, i can see the four lines but not in seperate four line s but in a paragraph. If am mailing that log... (8 Replies)
Discussion started by: intiraju
8 Replies

8. Shell Programming and Scripting

How to get lines started with matched strings using sed or grep for loop?

I have a huge file and want to separate it into several subsets. The file looks like: C1 C2 C3 C4 ... (variable names) 1 .... 2 .... 3 .... : 22 .... 23 .... I want to separate the huge file using the column 1, which has numbers from 1 to 23 (but there are different amount of... (8 Replies)
Discussion started by: AMBER
8 Replies

9. UNIX for Dummies Questions & Answers

print multiple lines with awk

Hi everyone! I'm not new to Unix, but I've never used awk before. I tried to look up this information on several sites and forums, I also looked in the documentation but I haven't found a solution yet. I would like to print the previous 3 lines before and the following 4 lines after the... (6 Replies)
Discussion started by: djcsabus
6 Replies

10. Shell Programming and Scripting

How to print only lines in between two strings using awk

Hi, I want to print only lines in between two strings and not the strings using awk. Eg: OUTPUT top 2 bottom 1 left 0 right 0 page 66 END I want to print into a new file only top 2 bottom 1 left 0... (4 Replies)
Discussion started by: jisha
4 Replies

Featured Tech Videos