Sponsored Content
Top Forums Shell Programming and Scripting Awk, if line after string does not match insert Post 302862919 by Akshay Hegde on Friday 11th of October 2013 04:03:55 PM
Old 10-11-2013
Quote:
Originally Posted by numele
I have a large file with interface records. I need to check every record that has the string "encapsulation bridge1483" and if the next line after this does not have "ip description" then I need to insert a line to add "ip description blah_blah_blah.

Sample file:
Code:
interface atm 1/0.190158 point-to-point
 atm pvc 100190158 19 158 aal5snap 768 768 10
 encapsulation pppoe
! 
interface atm 1/0.190158.1
 encapsulation ppp
 ppp authentication pap
 profile any "prof-pppoe"
! 
interface atm 1/0.20203 point-to-point
 atm pvc 100020203 2 203 aal5snap 1500 1500 10
 encapsulation bridge1483
 ip address 10.132.1.1 255.255.255.248
!
interface atm 1/0.80180 point-to-point
 atm pvc 100080180 8 180 aal5snap 3000 3000 10
 encapsulation bridge1483
 ip description won_ton_soup_company
 ip address 10.42.3.1 255.255.255.248
!
interface atm 1/0.100115 point-to-point
 atm pvc 100100115 10 115 aal5snap 6000 6000 10
 encapsulation bridge1483
 ip description dickandjanes
 ip address 10.2.3.1 255.255.255.248
!

What I need to have output:
Code:
interface atm 1/0.190158 point-to-point
 atm pvc 100190158 19 158 aal5snap 768 768 10
 encapsulation pppoe
! 
interface atm 1/0.190158.1
 encapsulation ppp
 ppp authentication pap
 profile any "prof-pppoe"
! 
interface atm 1/0.20203 point-to-point
 atm pvc 100020203 2 203 aal5snap 1500 1500 10
 encapsulation bridge1483
ip description blah_blah_blah
 ip address 10.132.1.1 255.255.255.248
!
interface atm 1/0.80180 point-to-point
 atm pvc 100080180 8 180 aal5snap 3000 3000 10
 encapsulation bridge1483
 ip description won_ton_soup_company
 ip address 10.42.3.1 255.255.255.248
!
interface atm 1/0.100115 point-to-point
 atm pvc 100100115 10 115 aal5snap 6000 6000 10
 encapsulation bridge1483
 ip description dickandjanes
 ip address 10.2.3.1 255.255.255.248
!



Try

Code:
$ awk '/encapsulation bridge1483/{print;getline;if($0!~/ip description/)print "ip description blah_blah_blah"}1'  file

Code:
interface atm 1/0.190158 point-to-point
 atm pvc 100190158 19 158 aal5snap 768 768 10
 encapsulation pppoe
! 
interface atm 1/0.190158.1
 encapsulation ppp
 ppp authentication pap
 profile any "prof-pppoe"
! 
interface atm 1/0.20203 point-to-point
 atm pvc 100020203 2 203 aal5snap 1500 1500 10
 encapsulation bridge1483
ip description blah_blah_blah
 ip address 10.132.1.1 255.255.255.248
!
interface atm 1/0.80180 point-to-point
 atm pvc 100080180 8 180 aal5snap 3000 3000 10
 encapsulation bridge1483
 ip description won_ton_soup_company
 ip address 10.42.3.1 255.255.255.248
!
interface atm 1/0.100115 point-to-point
 atm pvc 100100115 10 115 aal5snap 6000 6000 10
 encapsulation bridge1483
 ip description dickandjanes
 ip address 10.2.3.1 255.255.255.248
!

This User Gave Thanks to Akshay Hegde For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failed to insert string into file before line

Hi folks, I have the following program: #! /bin/ksh set -xv export grepDataSource="</data-sources>" export DB_HOST=tornado export SCHEMA_NAME=IAS10G export dataSource=data-sources.xml sourceString="<data-source class=\"oracle.jdbc.pool.OracleConnectionCacheImpl\" name=\"RiGHTvDS\"... (7 Replies)
Discussion started by: nir_s
7 Replies

2. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

3. Shell Programming and Scripting

KSH - search for string and insert line above

Hi,, Please excuse my lack of knowledge here. I think I am tring to do something fairly advanced yet am a bit of a beginner..... I want to use a korn shell (as is the norm with others in my company) to write a script to search a file for a string and insert some text above that line. Sudo... (5 Replies)
Discussion started by: HugoDarley
5 Replies

4. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

5. Shell Programming and Scripting

AWK Script Issue insert newline for a regular expression match

Hi , I am having an issue with the Awk script to insert newline for a regular expression match Having a file like this FILE1 #################### RXOER , RXERA , RXERC , RXERD .RXEA(RXBSN), RXERD , REXCD input RXEGT buffer RXETRY ####################### Want to match the RXE... (38 Replies)
Discussion started by: jaita
38 Replies

6. Shell Programming and Scripting

how to insert a string as a first line of the file

Hi I need to update a number of existing files and insert #!/bin/ksh line a the first line of the file. Is there any awk or sed command which would help me to do that instead of doing it manually? Thanks a lot -A (10 Replies)
Discussion started by: aoussenko
10 Replies

7. Shell Programming and Scripting

How to insert line with between two consecutive lines that match special pattern?

I have following pattern in a file: 00:01:38 UTC abcd 00:01:48 UTC 00:01:58 UTC efgh 00:02:08 UTC 00:02:18 UTC and I need to change something like the following 00:01:38 UTC abcd 00:01:48 UTC XXXX 00:01:58 UTC efgh 00:02:08 UTC XXXX (6 Replies)
Discussion started by: jjnight
6 Replies

8. UNIX for Dummies Questions & Answers

insert string at end of line if it found from list

Hi all, can some one help me out file 1 i have 06/01 3:14 d378299 06/01 8:10 d642036 06/01 10:51 d600441 06/01 10:52 d600441 06/01 11:11 d607339 06/01 11:49 d398706 something like this and in file named list i have ( there is space btwn 06/01 and 11:49 and d398706) d607339... (5 Replies)
Discussion started by: zozoo
5 Replies

9. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

10. Shell Programming and Scripting

Insert line based on found string

Hi All I'm trying to insert a new line at the before each comment line in a file. Comment lines start with '#-----' there are other comments with in lines but I don't want a new line there. Example file: blah blah #do not insert here #this is a comment blah #some more #another comment... (10 Replies)
Discussion started by: Mudshark
10 Replies
All times are GMT -4. The time now is 04:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy