Solved

delete lines containing a pattern?

Posted on 1998-07-08
6
474 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
create user in TSM 7 590
cant install flashplugins for firefox in freebsd 8 9 44
Error while installing rpm 1 77
LINUX ZIP - UNCOMPRESS WINDOWS PATH 3 88
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 …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.

895 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

17 Experts available now in Live!

Get 1:1 Help Now