how to delete line with matching text and line immediately after


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to delete line with matching text and line immediately after
# 1  
Old 01-11-2008
how to delete line with matching text and line immediately after

hello experts,

I have a file: File1
Code:
Sample Test1
This is a Test
Sample Test2
Another Test
Final Test3
A final Test

I can use sed to delete the line with specific text

ie:
Code:
sed '/Test2/d' File1.txt > File2.txt

How can I delete the line with the matching text and the line immediately after.
so My resulting file would look like

File2:
Code:
Sample Test1
This is a Test
Final Test3
A final Test

thanks

Last edited by Yogesh Sawant; 05-27-2010 at 04:44 AM.. Reason: added code tags
# 2  
Old 01-11-2008
Code:
# echo "1\n2\n3\n4\n5\n6" | sed -e '/2/{N;d;}'
1
4
5
6

# 3  
Old 01-11-2008
Hi.

Also:
Code:
% echo "1\n2\n3\n4\n5\n6" | sed '/2/,+1d'
1
4
5
6

According to man sed:
Quote:
addr1,+N
Will match addr1 and the N lines following addr1.
cheers, drl
# 4  
Old 01-11-2008
Hmmm yes but that didn't work on my sed (AIX). YMMV!!!
# 5  
Old 01-11-2008
Hi, gus2000.

The man page for the GNU version did not say that that was an extension. I'll try to see if that's POSIX. The version on FreeBSD 4.1 man page:
Quote:
The sed utility is expected to be a super-set of the IEEE Std 1003.2
(``POSIX.2'') specification.
and the "+1" syntax also did not work there ... cheers, drl
# 6  
Old 01-11-2008
shell:
Code:
#!/bin/sh
f=0
while IFS= read -r line
do 
 case $line in
   *pattern* ) f=1; continue ;;
 esac
 if [ "$f" -eq 1 ]; then   
    f=0 
    continue
 else
    echo $line
 fi
done <"file"

awk:
Code:
awk '/pattern/{getline;next}{print}' file

# 7  
Old 01-15-2008
I used the awk suggestion below since I was already using awk for something else...It worked great

Smilie
thanks
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to add text to each line of matching id

The awk below executes as expected if the id in $4 (like in f) is unique. However most of my data is like f1 where the same id can appear multiple times. I think that is the reason why the awk is not working as expected. I added a comment on the line that I can not change without causing the script... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

How to delete text between two characters in line?

Hi, I have a large text file with the following format: >gi|347545744|gb|JN204951.1| Dismorphia spio voucher 5 ATCAAATTCCTTCCTCTCCTTAAA >gi|17544664774|gb|WN204922.32| Rodapara nigens gene region CCGGGCAAATTCCTTCCTCTCCTTAAA >gi|555466400|gb|SG255122.8| Bombyx mandariana genbank 3... (1 Reply)
Discussion started by: euspilapteryx
1 Replies

3. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

4. UNIX for Dummies Questions & Answers

How to delete one line from a text file

PATTERN="" sed "/$PATTERN/d" $file In the file data is stored as ::: ::: I need to delete only the line containing the PATTERN pls help to point out why the above code is not working (4 Replies)
Discussion started by: thomsandy
4 Replies

5. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

6. Shell Programming and Scripting

how to delete a matching line from a file

I have file A with 10 lines and file B with 15 lines I have to read each line from file A and match it with lines in file B If matching lines are found i need to delete it from file B The ouput will be the deleted file B I tried the following in the loop but the deleting part is not... (5 Replies)
Discussion started by: codeman007
5 Replies

7. UNIX for Dummies Questions & Answers

Delete line in text file

Hi, How do I go about deleting a line in a text file. I have used grep to find a particlular word, now how do I delete the line? so far I got: grep -i $keyword file.txt (6 Replies)
Discussion started by: Berserk
6 Replies

8. Shell Programming and Scripting

how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :- <bag name="mybag1" version="1.0"/> <contents id="coins"/> <bag name="mybag2" version="1.1"/> <contents id="clothes"/> <contents id="shoes"/> <bag name="mybag3" version="1.6"/> I want to delete line containing mybag2 and its subsequent... (5 Replies)
Discussion started by: repudi8or
5 Replies

9. Shell Programming and Scripting

Delete first line from any text file ?

What command I can use to delete first line from any text file ? Thk (5 Replies)
Discussion started by: aungomarin
5 Replies

10. Shell Programming and Scripting

delete last line from text file

I have a file with which i must remove the last line of the file (the footer). I know how to copy and redirect with tail -1 etc, but how do i delete it permanentely (4 Replies)
Discussion started by: hcclnoodles
4 Replies
Login or Register to Ask a Question