Solved

delete lines containing a pattern?

Posted on 1998-07-08
6
471 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
AIX  NFS  question 5 91
UNiX Script filesystem space usage Part2 54 73
NTP Server in VMware 5 115
MarkLogic 1 51
Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now