Insert between lines.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert between lines.
# 1  
Old 11-04-2009
Insert between lines.

Hello...I'm here again.
This is the situation

I want check if exist text between two lines:

Example:

interface
description --text to verify
ip adress

if not exists text between 'interface' and 'ip address' i want insert the word 'no description'

interface
no description --if not exists text here
ip address

Thanks.
# 2  
Old 11-04-2009
Something like this?

Code:
awk '
/interface/{
  print
  getline
  if($0=="ip address"){
    print "no description"
  }
}
{print}
' file

# 3  
Old 11-04-2009
Quote:
Originally Posted by Franklin52
Something like this?

Code:
awk '
/interface/{
  print
  getline
  if($0=="ip address"){
    print "no description"
  }
}
{print}
' file

Not exactly...

I have this text in the file:

interface
description
ip address

But I also have this text:

interface
ip address

then I want insert the word "no description" when no exists text between "interface" and "description".

Help me please!!
# 4  
Old 11-04-2009
What's wrong with this output?

Code:
$ cat file
interface
description
ip address
interface
ip address
interface
description
ip address
interface
ip address
interface
description
ip address
$ awk '
/interface/{
  print
  getline
  if($0=="ip address"){
    print "no description"
  }
}
{print}
' file
interface
description
ip address
interface
no description
ip address
interface
description
ip address
interface
no description
ip address
interface
description
ip address
$

# 5  
Old 11-05-2009
Quote:
Originally Posted by Franklin52
What's wrong with this output?

Code:
$ cat file
interface
description
ip address
interface
ip address
interface
description
ip address
interface
ip address
interface
description
ip address
$ awk '
/interface/{
  print
  getline
  if($0=="ip address"){
    print "no description"
  }
}
{print}
' file
interface
description
ip address
interface
no description
ip address
interface
description
ip address
interface
no description
ip address
interface
description
ip address
$

Sorry....it works...thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed to insert text between lines

Hello, I am trying to insert a section of text between lines in another text file. The new lines to be inserted are: abcd.efgh.zzzz=blah abcd.efgh.xxxx=blah Where N = 0 to 2 Original File: abcd.efgh.wwxx=aaaaa abcd.efgh.yyzz=bbbbb abcd.efgh.wwxx=aaaaa abcd.efgh.yyzz=bbbbb... (3 Replies)
Discussion started by: tsu3000
3 Replies

2. Shell Programming and Scripting

Insert text before first 'n' lines

I want to put a particular text, say, the hash '#' before each of the first n lines of a file. How can I do that? (4 Replies)
Discussion started by: hbar
4 Replies

3. Shell Programming and Scripting

Insert a string instead of blank lines

how can i insert a string sush as "###" instead of blank lines in a file? i try this code but it doesn't work! awk 'NF<1 {$1=="###" ; print$0}' in_file > out_file (13 Replies)
Discussion started by: oreka18
13 Replies

4. Shell Programming and Scripting

sed - insert two lines

I have done this sed command to insert one line after a specific string is found: sed '/patternstring/ a\ new line string' file1 But how do I insert two lines? This is not possible: sed '/patternstring/ a\ new line string \a new line string 2' file1 (2 Replies)
Discussion started by: locoroco
2 Replies

5. Shell Programming and Scripting

Insert string in alternate lines

Hi All, In continuation of my previous thread 'Add text at the end of line conditionally', I need to further modfiy the file after adding text at the end of the line. Now, I need to add a fixed charater string at alternate lines starting from first line using awk or sed.My file is now as below:... (10 Replies)
Discussion started by: angshuman
10 Replies

6. Shell Programming and Scripting

[Perl] Insert lines before lines.

Hi, New problem, or challenge as they prefer in the US. I need to insert some lines in a file before certain other lines. To make it more clear: Original file: aaaa bbbbb ccccc ddddd bbbbb fffff ggggg Now I want to insert the line "NEW_NEW_NEW" when I match "fffff", but I want... (7 Replies)
Discussion started by: ejdv
7 Replies

7. Shell Programming and Scripting

Insert Title To Each Lines

I have a command that returns following, but it's missing a title for each line. YOUR FULL NAME YOUR ID YOUR EMAIL YOUR ADDRESS YOUR PHONE Now, I want to add its title to each line: Name: YOUR FULL NAME ID: YOUR ID Email: YOUR EMAIL Address: YOUR ADDRESS Phone:... (2 Replies)
Discussion started by: tqlam
2 Replies

8. Shell Programming and Scripting

Insert lines between delimiters

I'm working with a file like: somestuff somemorestuff ... someadditionalstuff STARTTAG ENDTAG someotherstuff somecoolstuff ... somefinalstuffI've got some text (either in a file or piped) to put between STARTTAG and ENDTAG. I was thinking something like grepping for the line number of... (2 Replies)
Discussion started by: BMDan
2 Replies

9. Shell Programming and Scripting

insert multiple lines into a file

Hi all, I've got some problems with editing a big configuration file .. its about 2k lines long.. anyway what I need is to place certain text to certain line number.. lets say I need to place "Something" on line 980 .. "something" else on line number 1500 and so on without tempering the rest of... (12 Replies)
Discussion started by: c0mrade
12 Replies

10. Shell Programming and Scripting

how to insert few lines in the called script

I have a situation where in a few lines have to be added into a script at some particular location. It is to be done from a file in another location just by running this file we should move the desired content into the target file. ne one tell me..whats d solution for this (7 Replies)
Discussion started by: suri
7 Replies
Login or Register to Ask a Question