LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-07-2017, 07:52 AM   #1
ansh007
LQ Newbie
 
Registered: May 2016
Location: Bangalore, India
Posts: 10

Rep: Reputation: Disabled
How to delete everything from a file, once a particular pattern is found, including the pattern


I Have a file say as below :

abcd qwer asd
sjdfhhf 234
jshdfg kkll ierr
Active abcd err
jhhs cbhhf jhsdse
joiyyer ffhh

Pattern to look for : Active
How I want the new file :
abcd qwer asd
sjdfhhf 234
jshdfg kkll ierr

That is delete everything from the point where the pattern is found.

What I tried:
sed -e '/Active/ { N; d; }' FILE > fil1

it only removed the line that contains Active, not all of them.
 
Old 03-07-2017, 08:15 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,294
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
sed needs to be told that you want to continue matching through to the end of the file which is marked by a dollar sign $ :

Code:
sed -e '/Active/,$d' File > fil1
Ranges are specified with a comma, so you can delete between two patterns (including the patterns) like this:

Code:
sed -e '/start/,/stop/d'
Or specify a number, such as the first line, and delete from that line number through to the matched pattern:

Code:
sed -e '1,/stop/d'
Though if the stop pattern is not found, the deletions will carry on to the end of the file.
 
1 members found this post helpful.
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete rows accoring to pattern in a file. Siggloo Programming 3 04-24-2015 09:09 AM
[SOLVED] SED/AWK - Delete all lines until empty line is found after pattern match vikas027 Programming 13 03-28-2012 08:33 AM
How do I delete line including a pattern and an line before that ? amolgupta Programming 3 02-20-2012 09:06 AM
Shell script to delete file if a pattern is found and restart servers sun81 Linux - Newbie 2 07-22-2010 02:24 PM
How to delete the file using pattern match? nishanthhampali Programming 3 04-16-2008 12:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration