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

Here is the Input File
--------------------
Code:
# 111.46.14.98

Line1
Line2
Line3
Line4
Line5

# 111.46.14.99

Line1
Line2
Line3
Line4
Line5

# 111.46.14.100

Line1
Line2
Line3
Line4
Line5

# 111.46.14.10

Line1
Line2
Line3
Line4
Line5

# 111.46.14.101

Line1
Line2
Line3
Line4
Line5

# 111.46.14.102

Line1
Line2
Line3
Line4
Line5

# 111.46.14.103

Line1
Line2
Line3
Line4
Line5

# 111.46.14.104

Line1
Line2
Line3
Line4
Line5

# 111.46.14.105

Line1
Line2
Line3
Line4
Line5

# 111.46.14.106

Line1
Line2
Line3
Line4
Line5

# 111.46.14.107

Line1
Line2
Line3
Line4
Line5

# 111.46.14.108

Line1
Line2
Line3
Line4
Line5

Expected Output File Should look like below
-----------------------------------------

Here is the Input File
--------------------
Code:
# 111.46.14.98

Line1
Line2
Line3
Line4
Line5

# 111.46.14.99

Line1
Line2
Line3
Line4
Line5

# 111.46.14.100

Line1
Line2
Line3
Line4
Line5

# 111.46.14.101

Line1
Line2
Line3
Line4
Line5

# 111.46.14.102

Line1
Line2
Line3
Line4
Line5

# 111.46.14.103

Line1
Line2
Line3
Line4
Line5

# 111.46.14.104

Line1
Line2
Line3
Line4
Line5

# 111.46.14.105

Line1
Line2
Line3
Line4
Line5

# 111.46.14.106

Line1
Line2
Line3
Line4
Line5

# 111.46.14.107

Line1
Line2
Line3
Line4
Line5

# 111.46.14.108

Line1
Line2
Line3
Line4
Line5


Last edited by Scrutinizer; 06-21-2014 at 06:10 AM.. Reason: code tags; formatting
# 9  
Old 06-20-2014
That's a new format/requirement.
# 111.46.14.108 is not an IP address. I.e. 111.46.14.108 does not exactly match.

---------- Post updated at 01:27 PM ---------- Previous update was at 01:19 PM ----------

Code:
awk ' (NR == FNR) { A[$0]; next } ($0 in A) { c = 8 } !(c && c--) ' ipfile infile

(i.e. $0 instead of $1) takes full lines.
In your last example ipfile must have # 111.46.14.10 that must exactly be in the infile.
# 10  
Old 06-20-2014
ipfile will only have ip address nothing else

For example
Code:
111.46.14.10
111.46.14.101
111.46.14.102

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

Last edited by Scrutinizer; 06-21-2014 at 06:06 AM.. Reason: code tags
# 11  
Old 06-20-2014
Try this:
Code:
awk 'NR==FNR {T[$1];next} $2 in T {LN=NR+7} NR>LN' file1 file2

# 12  
Old 06-20-2014
Thank you

Code:
/usr/xpg4/bin/awk ' (NR == FNR) { A[$0]; next } ($0 in A) { c = 7 } !(c && c--) ' "$ipfile" $infile.old > $infile

This one worked !!!

Last edited by Scrutinizer; 06-21-2014 at 06:17 AM.. Reason: code tags
# 13  
Old 10-30-2014
Can we use this code to search alpha numeric strings as well? instead of IP addresses

If the ipfile contains the following contents
Code:
80:00:1f:0e:03:c0:d7:21:50:20:bf
81:00:1f:02:e3:c0:d7:21:50:20:be

Can I use the code below to search the strings above by using the command below.

Code:
/usr/xpg4/bin/awk ' (NR == FNR) { A[$0]; next } ($0 in A) { c = 7 } !(c && c--) ' "$ipfile" $infile.old > $infile

---------- Post updated at 04:40 PM ---------- Previous update was at 12:13 PM ----------

The following code did work !!!

Code:
/usr/bin/nawk "/$pattern/ {c=4} c && c-- {next}1" $infile.old > $infile

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