The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete line in text file Berserk UNIX for Dummies Questions & Answers 6 12-16-2008 03:44 PM
how to delete text from line starting pattern1 up to line before pattern2? repudi8or Shell Programming and Scripting 5 04-15-2008 09:25 PM
Delete first line from any text file ? aungomarin Shell Programming and Scripting 5 05-16-2006 09:42 PM
delete a line based on first character of the line borncrazy UNIX for Dummies Questions & Answers 2 12-06-2005 03:27 PM
delete last line from text file hcclnoodles Shell Programming and Scripting 4 06-25-2002 09:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-11-2008
orahi001 orahi001 is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 48
how to delete line with matching text and line immediately after

hello experts,

I have a file: File1

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: 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:
Sample Test1
This is a Test
Final Test3
A final Test




thanks
  #2 (permalink)  
Old 01-11-2008
gus2000 gus2000 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 157
Code:
# echo "1\n2\n3\n4\n5\n6" | sed -e '/2/{N;d;}'
1
4
5
6
  #3 (permalink)  
Old 01-11-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
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 (permalink)  
Old 01-11-2008
gus2000 gus2000 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 157
Hmmm yes but that didn't work on my sed (AIX). YMMV!!!
  #5 (permalink)  
Old 01-11-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
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 (permalink)  
Old 01-11-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
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 (permalink)  
Old 01-15-2008
orahi001 orahi001 is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 48
I used the awk suggestion below since I was already using awk for something else...It worked great


thanks
Sponsored Links
Closed Thread

Bookmarks

Tags
sed delete line

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0