I have a flat file that contains a list of IP address following 6 additional lines.
I would like to get a help in the following. a shell script that would take ip address or list of ip addresses as input, search one by one the ip address in the file and as soon as it find the exact match it deletes 7 lines including the line that has the ip address. it should then save the file
for example lets say we want to find 166.1.1.3 , the script should be able to find 166.1.1.3 and delete 7 lines including 166.1.1.3.
I tried to set up the script and I am getting teh following error !!!
more script_IPRemove.sh
#!/usr/bin/ksh -x
infile=./infile
/usr/bin/cp -p $infile $infile.old &&
/usr/bin/awk ' (NR == FNR) { A[$1] next } ($1 in A) { c = 8 } !(c && c--) ' $ipfile $infile.old > $infile
Error that I am getting
--------------------------
script_IPRemove.sh 1xx.3x.5x.1x
+ infile=./infile
+ /usr/bin/cp -p ./infile ./infile.old
+ /usr/bin/awk (NR == FNR) { A[$1] next } ($1 in A) { c = 8 } !(c && c--) ./infile.old
+ 1> ./infile
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
First off, an observation: if you want to delete the line containing the IP address and the next 6 lines you will accumulate empty lines. In your input sample there are always 7 lines in a block separated by a single blank line. If you want to preserve this structure you'd have to delete 8 lines, not seven.
Second, the probably easiest way to do this is "sed":
#!/bin/ksh
typeset search=""
typeset fIn="./7lines.in"
typeset fTmp="./7lines.tmp"
while [ -n "$1" ] ; do
search="$1"
shift
sed '/^'"$search"'[^0-9]*/,+6 d' "$fIn" > "$fTmp"
mv "$fTmp" "$fIn"
done
exit 0
You may notice that the script lacks seriously in checks: checks for enough disk space, free inodes to create files, write privileges, successful operation of sed, sanity checks for the command line arguments, ... These are left as an exercise for the avid reader.
If you want to go for eight instead of seven lines in light of what i wrote above, change the "+6" to "+7" in the sed-script above.
Let me please reiterate my issue, From the Input file, I need to search for an exact Match of the IP address, for example If I search for 111.46.14.10, it should only find 111.46.14.10 IP address not 111.46.14.101, once it finds the IP address (Exact Match) then it should delete the Line that contains the IP address and the next 7 lines (there are always going to be two blank lines). So all together 8 lines should be deleted
Lets say we are searching for 111.46.14.10 from the Input file below the Output file should look like
the script should take these as the input and then look for each ip address in teh infile and then remove the line it out the ip address and the next 7 lines, when I say remove I mean delete the following 7 lines