[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 600
  • Last Modified:

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

0
vePortal
Asked:
vePortal
  • 6
  • 4
  • 2
  • +2
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
zlobchoCommented:
#!/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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now