Solved

delete lines containing a pattern?

Posted on 1998-07-08
6
476 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AIX  NFS  question 1 136
UNiX Script filesystem space usage Part2 54 86
Linux: disable vim auto-comment 7 116
How can i set a Environment Variable in my Unix script ? 3 67
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…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

808 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