Deleting the lines exist between the Mattched Patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting the lines exist between the Mattched Patterns
# 1  
Old 01-29-2013
RedHat Deleting the lines exist between the Mattched Patterns

Hi,

I have a file with the content:

Code:
 
for name in \
sree\
rama\
laila\
srihari\
vicky\
john
do
   echo $name
done

I need to remove all the name lines that exist between for (first line) and do line so that i can replace with new names.

Output file should look like:
Code:
 
for name in \
do
echo $name
done

when i use the below command, it is deleting for line and do lines as well.

Code:
sed -i '/for/,/do/d' file

Please take for line and do line as a reference because we may not sure about the number of lines between for line and do line. Those lines get changed every time.

Thanks in advance!!

-SREE
# 2  
Old 01-29-2013
Code:
awk '/^for/{print;f=1;next;}/^do/{print;f=0;next;}f!=1{print}' filename

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-29-2013
Hi Bipin,

Thanks a lot for the quick reply. It works!!

-SREE
# 4  
Old 01-29-2013
Just extend your sed code slightly:
Code:
$ sed '/^for/,/^do/{/^for/p;/^do/p;d}' file
for name in \
do
   echo $name
done

or even shorter:
Code:
$ sed '/for/,/do/{/for\|do/p;d}' file

You need to decide if anchoring the regexs with ^is advantageous or not.

Last edited by RudiC; 01-30-2013 at 05:37 AM.. Reason: shorter version
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

Script for Deleting a process which exist every day in hold state

Hi All , There always exist one process in hold state every day which will cause savior impact if i didn't kill it. i will do it manually . Manul process is this. sudo -iu root/opt/app/root/cdlinux/ndm/bin/direct -- it will take me connect direct prompt Sel proc ; -- it will displays... (9 Replies)
Discussion started by: Phani369
9 Replies

3. Debian

Extract Lines Between 2 patterns if exist...

Hello. I am not having luck with sed or awk today. $ echo "$BrackListFinal" DSCF3649-DSCF3651_Brkt DSCF3649.JPG 2014-07-21 13:34:44 On 1 DSCF3649.RAF 2014-07-21 13:34:44 On 1 DSCF3650.JPG 2014-07-21 13:34:45 On 2 DSCF3650.RAF 2014-07-21 13:34:45 On 2... (3 Replies)
Discussion started by: DSommers
3 Replies

4. Shell Programming and Scripting

Deleting lines above the patterns

I want to delete 1 line above the paatern and 3 line below the pattern and the pattern line itself, on the whole 5 lines. If there are three patterns what to do and the final text file to be captured in a new file. (3 Replies)
Discussion started by: razen
3 Replies

5. Shell Programming and Scripting

Deleting lines above the patterns

I want to delete 1 line above the paatern and 3 lines below the pattern and the pattern line itself, on the whole 5 lines. If there are three patterns to be deleted what to do and the final text file to be captured in a new file. (1 Reply)
Discussion started by: razen
1 Replies

6. Shell Programming and Scripting

Deleting lines of a file if they exist in another file

I have a reference file that needs to remain static and another file that may or may not have duplicate rows that match the reference file. I need help with a command that will delete any duplicate rows from the second file while leaving reference file intact For example reference file would... (4 Replies)
Discussion started by: bjdamon
4 Replies

7. Shell Programming and Scripting

deleting lines between patterns using sed or awk

hi, Here is excerpt from my xml file <!-- The custom module to do the authentication for LDAP --> </login-module> <login-module code="com.nlayers.seneca.security.LdapLogin" flag="sufficient"> <module-option... (1 Reply)
Discussion started by: sunrexstar
1 Replies

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. Shell Programming and Scripting

Script for searching a pattern in 5 files and deleting the patterns given

Hi All, I have written the below script that searches for the pattern in a file and delete them if present. please can some one have a look and suggest the changes in the script. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read... (4 Replies)
Discussion started by: Shazin
4 Replies

10. Shell Programming and Scripting

Delete lines between two patterns without deleting the second pattern

I want to delete lines like this sed '/FROM_HERE/,/TO_HERE/d' but I would like to *not* delete the second match, i.e. the TO_HERE line. How can I achieve this? Thank you! (1 Reply)
Discussion started by: Ilja
1 Replies
Login or Register to Ask a Question