Link to home
Start Free TrialLog in
Avatar of vePortal
vePortalFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of vikas_madhusudana
vikas_madhusudana
Flag of India image

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

Avatar of ozo
#!/bin/sh
perl -i.bak -ne "print unless /\Q$*/"  domain.com.hosts
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?
Avatar of vePortal

ASKER


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>
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
ASKER CERTIFIED SOLUTION
Avatar of zlobcho
zlobcho
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
./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?

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.

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
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: https://www.experts-exchange.com/questions/24997810/PERL-SED-AWK-How-do-I-remove-a-zone-from-named-conf-in-a-bash-script.html. This might help you solve my problem.
Thank You,
Happy Holidays.
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
Hi vePortal!

There are 2 files: infile.txt and outfile.txt.
The result is in outfile.txt.
Avatar of ghostdog74
ghostdog74

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

Thanks...it's helped me get a good start.to accomplish my task.