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

Script to remove lines from a file

Reply to Message Icon

Original Message
Name: Justin
Date: July 8, 2003 at 08:31:13 Pacific
Subject: Script to remove lines from a file
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.


Report Offensive Message For Removal

Response Number 1
Name: SN
Date: July 8, 2003 at 09:48:42 Pacific
Subject: Script to remove lines from a file
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 2
Name: FishMonger
Date: July 8, 2003 at 20:55:28 Pacific
Subject: Script to remove lines from a file
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 3
Name: John
Date: July 14, 2003 at 08:52:42 Pacific
Subject: Script to remove lines from a file
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 4
Name: Justin
Date: July 18, 2003 at 07:10:16 Pacific
Subject: Script to remove lines from a file
Reply: (edit)

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Script to remove lines from a file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software