Computing.Net > Forums > Programming > Script to remove lines from a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Script to remove lines from a file

Reply to Message Icon

Name: Justin
Date: July 8, 2003 at 08:31:13 Pacific
OS: Solaris 8
CPU/Ram: SPARC 64
Comment:

was wondering if you could help me with a script to remove ALL the lines in a file that contain the word "BOUNDARY". I have the script like this so far, but It will only remove the first occurance of BOUNDRY, not all of them like I want.

#!/bin/bash
line=$(grep -n "BOUNDARY" foo.c | cut -d: -f1 | sed '1q' )
sed ${line}d foo.c > foo_new.c


thanks for your help.



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: July 8, 2003 at 09:48:42 Pacific
Reply:

I did a similar script like this to upload files from forms. It relied heavily on regular expressions, which is what I would use here. If you want the whole script, I'll e-mail it to you. But for now, here is a line of code that removes all lines that contain the word BOUNDARY (case-sensitive) from a scalar:

$myfile =~ s/^.*BOUNDARY.*$//g;

s - search and replace
^ - match beginning of a line
.* - match any (non line ending) character 0 or more times
BOUNDARY - match BOUNDARY
.* - match any (non line ending) character 0 or more times
$ - match end of line
// - replace the matches with nothing
g - do it globally (everywhere)

Judging by the context, it may be helpful for you in whatever project you're doing to split the file into separate scalars using a line with the word BOUNDARY as a delimiter. If so, then you could use

@mygoodstuff =~ split/^.*BOUNDARY.*$/$myfile/;

The regular expression may need a little adjustment, post back if it doesn't work. Or if it does, for that matter, for curiosity's sake.

As a disclaimer, remember that if you're dealing with large files, there is a relatively good chance that the word BOUNDARY may pop up where you don't want it to.

Good luck,
-SN


0

Response Number 2
Name: FishMonger
Date: July 8, 2003 at 20:55:28 Pacific
Reply:

How about a perl (command line) script.

perl -pi -e "s/^.*BOUNDARY.*$//i" foo.c

If you want a back-up copy of the original file, modify it to:

perl -pi.old -e "s/^.*BOUNDARY.*$//i" foo.c

this will create a back-up file named foo.c.old

The i in the regex tells it to ignore case.


0

Response Number 3
Name: John
Date: July 14, 2003 at 08:52:42 Pacific
Reply:

We have these files that are backed up on the serber into files. Sometimes it comments the first couples lines in the file causing an error. We need to completely remove the // and /*'s from the file.

I put into the pl file

$filename =~ s/^.*//.*$//g;
$filename =~ s/^.*/*.*$//g;

Is this right am I going about this all wrong?

Please email me!! I need help ASAP!!


0

Response Number 4
Name: Justin
Date: July 18, 2003 at 07:10:16 Pacific
Reply:

Thanks for your help guys the perl line and $myfile variable worked equally well. appreciate the help ;-)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


What's Up With All The VB vb6 dirlistbox



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Script to remove lines from a file

Get first line from a file www.computing.net/answers/programming/get-first-line-from-a-file/14984.html

To get the last 10 lines of a file. www.computing.net/answers/programming/to-get-the-last-10-lines-of-a-file/20114.html

To cut and paste a set of lines from a file www.computing.net/answers/programming/to-cut-and-paste-a-set-of-lines-from-a-file/20198.html