PERL: Remove a line from file.

Hello Experts:

I will give anyone 250 points if they provide a solution that removes a line from a file and can provide an explanation of the source. I just started learning PERL and am totally lost.

I would like to be able to execute ./removeA.sh 1.domain.com A 127.0.0.1 and be able to remove line one.


domain.com.hosts
-----------------
1.domain.com. IN A 127.0.0.1
2.domain.com. IN A 127.0.0.1
3.domain.com. IN A 127.0.0.1
4.domain.com. IN A 127.0.0.1

Open in new window

vePortalAsked:
Who is Participating?
 
zlobchoConnect With a Mentor Commented:
#!/usr/bin/perl -w

use strict;

unlink('outfile.txt');
if ($#ARGV==-1){
printf STDOUT "Usage:perl removeA.pl <RECORD> <TYPE> <IP>\n";
}
my $record=$ARGV[0];
my $type=$ARGV[1];
my $ip=$ARGV[2];

open (INFILE,"infile.txt") or die;
open (OUTFILE,">>outfile.txt") or die;
 while (<INFILE>){
   my $line=$_;
    if (!(grep /$record/&&/$type/&&/$ip/,$line)){
    print OUTFILE $line;
   }
 }
close (INFILE);
close (OUTFILE);
0
 
vikas_madhusudanaCommented:
here tail -n +1 is used to print all the lines except line number 1

it is redirected to a temporary file tmp.txt and you rename the original file as .bak you can even remove if you want using 'rm' command
Next thing what you do is you rename the tmp file to filename


#!/bin/sh

tail -n +1 filename tmp.txt
mv filename filename.bak
mv tmp.txt filename

Open in new window

0
 
ozoCommented:
#!/bin/sh
perl -i.bak -ne "print unless /\Q$*/"  domain.com.hosts
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
ozoCommented:
did you mean you would like to be able to execute
./removeA.sh 1.domain.com. IN A 127.0.0.1
and be able to remove line one?
0
 
vePortalAuthor Commented:

Sorry that I explained myself poorly. I want to me able to pass variables to a perl script and remove a specific line based on earlier input. So If I excecute the following:
./removeA.sh 1.domain.com A 127.0.0.1
It should remove the line that has all three identical strings. If I execute the following
./removeA.sh 1.domain.com1 A 127.0.0.1
It should remove the line that has domain1.com, an A record, and 127.0.0.1

So the perl script should function in the following format sh removeA.sh <RECORD><TYPE><IP>
0
 
ozoCommented:
sh ./removeA.sh 1.domain.com. A 127.0.0.1

#!/bin/sh
perl -i.bak -ane 'print unless "@F[0,2,3]" eq '"'$*'"  domain.com.hosts
0
 
ozoCommented:
./removeA.sh 1.domain.com A 127.0.0.1
It should remove the line that has all three identical strings.
domain.com.hosts
-----------------
1.domain.com. IN A 127.0.0.1

  is 1.domain.com. identical to  1.domain.com?


grep /$record/&&/$type/&&/$ip/

  is 1.domain.com1 identical to  1.domain.com?
0
 
vePortalAuthor Commented:

Hi Zlobcho:

I copied my date into infile.txt
.
If I execute just perl test.pl I could the following:
Usage:perl removeA.pl <RECORD> <TYPE> <IP>
Use of uninitialized value in regexp compilation at test.pl line 17, <INFILE> line 1.
Use of uninitialized value in regexp compilation at test.pl line 17, <INFILE> line 1.
Use of uninitialized value in regexp compilation at test.pl line 17, <INFILE> line 1.
Use of uninitialized value in regexp compilation at test.pl line 17, <INFILE> line 2.
Use of uninitialized value in regexp compilation at test.pl line 17, <INFILE> line 2.
Use of uninitialized value in regexp compilation at test.pl line 17, <INFILE> line 2.
Use of uninitialized value in regexp compilation at test.pl line 17, <INFILE> line 3.
Use of uninitialized value in regexp compilation at test.pl line 17, <INFILE> line 3.
Use of uninitialized value in regexp compilation at test.pl line 17, <INFILE> line 3.&nbsp;

If I execute: perl test.pl 1.domain.com A 127.0.0.1
I don't get any error messages but the line containing 1.domain.com, A, & 127.0.0.1 is not removed.

0
 
ozoCommented:
Did you execute
sh ./removeA.sh 1.domain.com. A 127.0.0.1

with

#!/bin/sh
perl -i.bak -ane 'print unless "@F[0,2,3]" eq '"'$*'"  domain.com.hosts
0
 
vePortalAuthor Commented:
Ozo:
They are not identical. Though I did make a typo.
# ./removeA.sh 1.domain.com A 127.0.0.1  
> 1.domain.com. IN A 127.0.0.1 (Line should be removed)
# ./removeA.sh 2.domain.com A 127.0.0.1  
> 2.domain.com. IN A 127.0.0.1 (Line should be removed)
# ./removeA.sh 3.domain.com A 127.0.0.1  
> 3.domain.com. IN A 127.0.0.1 (Line should be removed)
# ./removeA.sh 4.domain.com A 127.0.0.1  
> 4.domain.com. IN A 127.0.0.1 (Line should be removed)

etc...
Everyone: Please take a look at the following question: http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_24997810.html. This might help you solve my problem.
Thank You,
Happy Holidays.
0
 
ozoCommented:
So ignore . at the end of <RECORD> when comparing?

#!/bin/sh
perl -i.bak -ane '$F[0]=~s/\.$//;print unless "@F[0,2,3]" eq '"'$*'"  domain.com.hosts
0
 
zlobchoCommented:
Hi vePortal!

There are 2 files: infile.txt and outfile.txt.
The result is in outfile.txt.
0
 
ghostdog74Commented:
since you specify bash tag, here's a bash solution




#!/bin/bash

arg1=$1
arg2=$2
arg3=$3
while read -r line
do
    case "$line" in
        *$arg1*$arg2*$arg3* ) continue;;
        * )
        echo $line
        ;;
    esac
done <"file"

Open in new window

0
 
vePortalAuthor Commented:
Thanks...it's helped me get a good start.to accomplish my task.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.