Script that takes IP address as an Input and deletes 7 lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script that takes IP address as an Input and deletes 7 lines
# 1  
Old 04-25-2014
Script that takes IP address as an Input and deletes 7 lines

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.

File contents shown below
Code:
166.1.1.1
Line1
Line2
Line3
Line4
Line5
Line6

166.1.1.2
Line1
Line2
Line3
Line4
Line5
Line6

166.1.1.3
Line1
Line2
Line3
Line4
Line5
Line6

166.1.1.33
Line1
Line2
Line3
Line4
Line5
Line6


Last edited by Scott; 04-25-2014 at 07:33 PM.. Reason: Code tags
# 2  
Old 04-25-2014
Here is an awk approach:
Code:
awk '
        NR == FNR {
                I[$1]
                next
        }
        $1 in I {
                F = 1
                next
        }
        F && ++c == 7 {
                F = 0
                c = 0
                next
        }
        !(F)
' ipfile infile

# 3  
Old 04-25-2014
or:
Code:
awk 'FNR==NR{ip[$0];next} !($1 in ip)' ipFile RS= ORS='\n\n' inputFile

# 4  
Old 04-26-2014
Yoda's code optimized
Code:
awk '
(NR == FNR) {
 A[$1]
 next
}
($1 in A) {
 c = 8
}
!(c && c--)
' ipfile infile

---------- Post updated at 02:34 AM ---------- Previous update was at 01:39 AM ----------

If output on terminal looks good you can overwrite the input as follows
Code:
infile=./infile
cp -p $infile $infile.old &&
awk '
(NR == FNR) {
 A[$1]
 next
}
($1 in A) {
 c = 8
}
!(c && c--)
' ipfile $infile.old > $infile


Last edited by MadeInGermany; 04-26-2014 at 04:45 AM.. Reason: delete not print
# 5  
Old 06-20-2014
I tried to set up the script and I am getting teh following error !!!
more script_IPRemove.sh
Code:
#!/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
--------------------------
Code:
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


Last edited by Scrutinizer; 06-21-2014 at 07:05 AM.. Reason: code tags
# 6  
Old 06-20-2014
1. There must be a semicolon between statements within a block (if not on separate lines).
2. In Solaris you better use /usr/xpg4/bin/awk or nawk.
Best practice is to define PATH at the beginning of the script .
3. $ipfile is yet undefined. (You could pass script parameter instead.)
Code:
PATH=/usr/xpg4/bin/:/bin:/usr/bin:/usr/sbin
export PATH
infile=./infile
cp -p $infile $infile.old &&
awk ' (NR == FNR) { A[$1]; next } ($1 in A) { c = 8 } !(c && c--) ' "$ipfile" $infile.old > $infile

# 7  
Old 06-20-2014
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":

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

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove Comment Lines From Script/Input File

Hello, I have a SAS code that predominantly has comments line and the real code like below and i want to remove ONLY THE COMMENTS from the code in the single line or spanned across multiple lines. /******************************************************************** *** This Is a Comment... (4 Replies)
Discussion started by: arooonatr
4 Replies

2. UNIX for Dummies Questions & Answers

sed command that deletes lines with 3-5 digit strings

Hello! My final exam in my Linux class is tomorrow, and I was just reviewing the grep and sed commands. I came across an example, by which I got stumped: it asks to provide a sed command that deletes all lines that contain exactly 3 to 5 digit strings, from a file. In this case, I created a... (3 Replies)
Discussion started by: kalpcalp
3 Replies

3. Shell Programming and Scripting

Help in unix script to join similar lines of input

Hi, I have been thinking of how to script this but i have no clue at all.. Could someone please help me out or give me some idea on this? I would like to group those lines with the same first variable in each line, joining the 2nd variables with commas. Let's say i have the following input. ... (3 Replies)
Discussion started by: rei125
3 Replies

4. Shell Programming and Scripting

Need help to run sql query from a script..which takes input from a file

I need to run sql script from shell script which takes the input from a file and contents of file will be like : 12345 34567 78657 and query will be like : select seq_nbr from bus_event where event_nbr='12345'; select seq_nbr from bus_event where event_nbr='34567'; select seq_nbr... (1 Reply)
Discussion started by: rkrish
1 Replies

5. Shell Programming and Scripting

Script that takes strings as input

Hello all! I need to take a series of inputs passed to one shell script and pass them to a second shell script. For example, the first shell script, script_one.sh, has the following inputs: ./script_one -u username -p password -option1 string1 -option2 -string2 ....Inside of script_one.sh... (4 Replies)
Discussion started by: bashnewbee
4 Replies

6. Shell Programming and Scripting

A script that deletes files.

I want to write a script that deletes files inside the dir. However, the script should also allow the user to confirm by pressing (d) key before deleting files.. #!/bin/bash for file in $1/* do size='ls -l $file | cut -f 5 -d " "' name='ls -l $file | cut -f 9 -d " "' ... (1 Reply)
Discussion started by: herberwz
1 Replies

7. Shell Programming and Scripting

awk script - reading input lines

Can I do something like, if($0==/^int.*$/) { print "Declaration" } for an input like: int a=5; If the syntax is right, it is not working for me, but I am not sure about the syntax. Please help. Thanks, Prasanna (1 Reply)
Discussion started by: prasanna1157
1 Replies

8. Shell Programming and Scripting

how to display an error if input value is not an IP address??

Dears, I need your assistance, I'm creating a shell script to read the IP address of a server and i would like to display an error message in red if the entered value is not an IP. any ideas how can i do it?:confused: #!/bin/sh echo "Please enter the server IP address: \c" read IPadd... (3 Replies)
Discussion started by: Dendany83
3 Replies

9. Programming

My exe takes more time for specied input

My application is coded in C++ and uses pro C to get data from oracle database. My executable file is taking more time to execute for some specified input. Is there any way to check where it spends the maximum time? (1 Reply)
Discussion started by: SamRoj
1 Replies

10. Shell Programming and Scripting

Shell Program which deletes all lines ...

Shell Program which deletes all lines containing the word "UNIX" in the files supplied as argument..please help me to do this task :) (4 Replies)
Discussion started by: abhiseknitd
4 Replies
Login or Register to Ask a Question

Featured Tech Videos