?
Solved

delete lines containing a pattern?

Posted on 1998-07-08
6
Medium Priority
?
480 Views
Last Modified: 2010-04-21
HI, how can delete all the lines contains certain string using sed or other tools?
Shane
0
Comment
Question by:xz02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2008534
sed -e '/certain string/ d' < oldfile > newfile
grep -v 'certain string' < oldfile > newfile
perl -i -ne 'print unless /certain string/' file
 . .
0
 
LVL 2

Expert Comment

by:richrussell
ID: 2008535
As ozo said, you can do with sed, grep, perl, awk etc.

Personally I'd use grep -v (find all lines not containing string).

cat infile | grep -v 'certain string' >outfile ; mv -r outfile infile

But there's lots and lots of ways of doing this.
0
 

Author Comment

by:xz02
ID: 2008536
Thank you very much. It works. Suppose I want to do the thing for
all *.dat files under the directory, what should I do then?
Thanks again.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 84

Expert Comment

by:ozo
ID: 2008537
perl -i -ne 'print unless /certain string/' *.dat

  or in sh

for name in *.dat ;do
  grep -v 'certain string' <$name >outfile
  mv outfile $name
done

  or in csh

foreach name ( *.dat )
  sed -e '/certain string/ d' <$name >outfile
  mv outfile $name
end
0
 
LVL 3

Expert Comment

by:braveheart
ID: 2008538
ping
0
 
LVL 1

Accepted Solution

by:
ksb earned 40 total points
ID: 2008539
The answers above (grep -v, sed -e '/pat/d', and Perl) are all correct if you want to replicate the file or do the task in a stream (pipe line).

If you really want to delete the lines from the file you can use the ed or ex text editor with a here document for input.  For example to delete the pat "kill" from the file /etc/motd

  cd /etc
  ed - motd <<!
  1,$g/kill/d
  w
  q
  !

Would do the trick.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question