Solved

delete lines containing a pattern?

Posted on 1998-07-08
6
479 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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 20 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

724 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