Search from 1st match and end 2nd match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search from 1st match and end 2nd match
# 1  
Old 01-29-2016
Hammer & Screwdriver Search from 1st match and end 2nd match

I've been looking through the forums for awhile now and looking at the man page for grep and egrep and not seeming to find this scenario so it might not be possible but figured I'd throw it out to get some ideas.

I'm looking for a way to search a file for 1st match (example below net self) and end search when it runs into 2nd match (example below } character) and output those results. Eventually I will want to edit those lines (either delete each line OR replace matching IP Address)

Code:
net self /Common/198.74.92.130 {
    address 198.74.92.130/25
    traffic-group /Common/traffic-group-local-only
    vlan /Common/VLAN221
}
net self /Common/vPC-Interconnect-Floating {
    address 10.47.194.100/27
    allow-service {
        default
    }
    traffic-group /Common/traffic-group-1
    vlan /Common/VLAN233
}
net self /Common/10.47.32.2 {
    address 10.47.32.2/23
    traffic-group /Common/traffic-group-local-only
    vlan /Common/VLAN215
}
net self /Common/10.47.194.40 {
    address 10.47.194.40/27
    allow-service all
    traffic-group /Common/traffic-group-1
    vlan /Common/Interconnect
}

Since I'm far from being knowledgeable but love to learn I have tried what I do know which includes some awful ideas like
Code:
grep ^net self * }

Code:
egrep ‘^net self+}$

Code:
grep ‘^net self.*}$'

I found an example of possibly using awk and tried
Code:
awk ‘/net self/{if (NR!=1)print "";next}{print $0}END{print "";}

# 2  
Old 01-29-2016
If I understood your request better, I perhaps could be of better help. Try
Code:
sed -n '/net self/,/}/p' file

which doesn't feel right for the second record, but without better specs?
# 3  
Old 01-29-2016
wow, impressive. This gave me a great start which worked. I made a minor change to only include IP Addresses

Code:
sed -n '/net self \/Common\/[0-9]/,/}/p' file

Now it lists everything, how do we do the same thing but delete what we find?

For example, I perform the search and using the example above I would like to delete a result that contains vlan /Common/VLAN215 as well as vlan /Common/VLAN233

Would it be best to search and put results in an array to delete? Just not sure what the best approach would be.

---------- Post updated at 01:46 PM ---------- Previous update was at 01:23 PM ----------

I'm way over my head and looks like I may have went in the wrong direction for the correct solution.

I need to remove VLAN215 from this configuration file but its difficult to see the pattern to make that happen. Sometimes its on a line of its own and others its multiple lines. Really difficult to wrap the head around.
Code:
net route-domain /Common/0 {
    id 0
    vlans {
        /Common/Interconnect
        /Common/Sync-Failover
        /Common/VLAN215
        /Common/VLAN221
        /Common/VLAN233
        /Common/VLAN252
        /Common/VLAN216
    }
}           

net self /Common/10.47.32.2 {
    address 10.47.32.2/23
    traffic-group /Common/traffic-group-local-only
    vlan /Common/VLAN215
}  

net self /Common/10.47.32.1 {
    address 10.47.32.1/23
    traffic-group /Common/traffic-group-1
    vlan /Common/VLAN215
} 

net stp /Common/cist {
    interfaces {
        1.1 {
            external-path-cost 20000
            internal-path-cost 20000
        }
    }
    trunks {
        Bonded.Pair {
            external-path-cost 20000
            internal-path-cost 20000
        }
    }
    vlans {
        /Common/Interconnect
        /Common/Sync-Failover
        /Common/VLAN215
        /Common/VLAN216
        /Common/VLAN221
        /Common/VLAN233
        /Common/VLAN252
    }
}         

net vlan /Common/VLAN215 {
    interfaces {
        Bonded.Pair {
            tagged
        }
    }
    tag 215
}   

net fdb vlan /Common/VLAN215 { } 

net route-domain /Common/0 {
    id 0
    vlans {
        /Common/Interconnect
        /Common/Sync-Failover
        /Common/VLAN215
        /Common/VLAN221
        /Common/VLAN233
        /Common/VLAN252
        /Common/VLAN216
    }
}       

net self /Common/10.47.32.2 {
    address 10.47.32.2/23
    traffic-group /Common/traffic-group-local-only
    vlan /Common/VLAN215
}   

net self /Common/10.47.32.1 {
    address 10.47.32.1/23
    traffic-group /Common/traffic-group-1
    vlan /Common/VLAN215
} 

net stp /Common/cist {
    interfaces {
        1.1 {
            external-path-cost 20000
            internal-path-cost 20000
        }
    }
    trunks {
        Bonded.Pair {
            external-path-cost 20000
            internal-path-cost 20000
        }
    }
    vlans {
        /Common/Interconnect
        /Common/Sync-Failover
        /Common/VLAN215
        /Common/VLAN216
        /Common/VLAN221
        /Common/VLAN233
        /Common/VLAN252
    }
}      

net vlan /Common/VLAN215 {
    interfaces {
        Bonded.Pair {
            tagged
        }
    }
    tag 215
}      

net fdb vlan /Common/VLAN215 { }

net route-domain /Common/0 {
    id 0
    vlans {
        /Common/Interconnect
        /Common/Sync-Failover
        /Common/VLAN215
        /Common/VLAN221
        /Common/VLAN233
        /Common/VLAN252
        /Common/VLAN216
    }
}

# 4  
Old 01-29-2016
I wonder if this does what you want?

Code:
perl -00 -ne '/vlan\s+\/Common\/VLAN2(15|33)/ and next; s/\s+\/Common\/VLAN2(15|33)//g; print' djzah.vlan

Code:
net route-domain /Common/0 {
    id 0
    vlans {
        /Common/Interconnect
        /Common/Sync-Failover
        /Common/VLAN221
        /Common/VLAN252
        /Common/VLAN216
    }
}

net stp /Common/cist {
    interfaces {
        1.1 {
            external-path-cost 20000
            internal-path-cost 20000
        }
    }
    trunks {
        Bonded.Pair {
            external-path-cost 20000
            internal-path-cost 20000
        }
    }
    vlans {
        /Common/Interconnect
        /Common/Sync-Failover
        /Common/VLAN216
        /Common/VLAN221
        /Common/VLAN252
    }
}

net route-domain /Common/0 {
    id 0
    vlans {
        /Common/Interconnect
        /Common/Sync-Failover
        /Common/VLAN221
        /Common/VLAN252
        /Common/VLAN216
    }
}

net stp /Common/cist {
    interfaces {
        1.1 {
            external-path-cost 20000
            internal-path-cost 20000
        }
    }
    trunks {
        Bonded.Pair {
            external-path-cost 20000
            internal-path-cost 20000
        }
    }
    vlans {
        /Common/Interconnect
        /Common/Sync-Failover
        /Common/VLAN216
        /Common/VLAN221
        /Common/VLAN252
    }
}

net route-domain /Common/0 {
    id 0
    vlans {
        /Common/Interconnect
        /Common/Sync-Failover
        /Common/VLAN221
        /Common/VLAN252
        /Common/VLAN216
    }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

3. Shell Programming and Scripting

Search file containing ps results for a match "my.cnf" and then for a second match . "ok:" and

I need to find two matches in the output from ps. I am searching with ps -ef |grep mysql for: my.cnf /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/mysql/master/agis_core/etc/my.cnf after this match I want to search back and match the hostname which is x number of lines back, above the... (2 Replies)
Discussion started by: bash_in_my_head
2 Replies

4. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

5. Shell Programming and Scripting

Grep from match to the end of file

Hi i have this script : #!/bin/bash DATE=$(date '+%Y-%m-%d %H:%M' -d "1 hour ago") PASS=$(grep -A99999999999 '$DATE' /var/log/asterisk/full| grep -i 'Wrong password') it a script that ment to go over log file a hour back from now until the end of file. right now im using... (5 Replies)
Discussion started by: batchenr
5 Replies

6. Shell Programming and Scripting

Conversion if 1st column is match (awk '{s+=$1} END

Hi im trying to add numbers, got no problem with it im using awk '{s+=$1} END {print s, "MB"}', but what if the numbers are like mention below. who will i add them 2000 KB 1 MB Answer: 2001 Desired: 2000 KB 1 MB Answer: 3000 (4 Replies)
Discussion started by: invinzin21
4 Replies

7. Shell Programming and Scripting

Pattern match till the end of the file.

I have a file which is like this ……………………………………….. ………………………………… ………………………………… …………………………………… ……………………………………. ……………………………… <<<from_here>>> ……………………………… ………………………………. I want a script which would fetch the data starting from <<<from_here>>> in the file till the end... (2 Replies)
Discussion started by: halfafringe
2 Replies

8. Programming

sed no match word end with hyphen

Hi, This is my first post.It has two parts first part: I want to match a line that starts with whitespace tab or similar followed by must start with specific word and not match same word start with hyphen like this: grep height file1: height: 150px; line-height: 1.5em; height:... (4 Replies)
Discussion started by: medium_linux
4 Replies

9. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

10. UNIX for Dummies Questions & Answers

Regular Expression - match 'b' that follows 'a' and is at the end of a string

Hi, I'm struggling with a regex that would match a 'b' that follows an 'a' and is at the end of a string of non-white characters. For example: Line 1: aba abab b abb aab bab baa I can find the right strings but I'm lacking knowledge of how to "discard" the bits that precede bs.... (2 Replies)
Discussion started by: machinogodzilla
2 Replies
Login or Register to Ask a Question