Solved

Insert Line between patterns in shell

Posted on 2013-06-17
3
257 Views
Last Modified: 2013-10-03
Hi,
I have following file:

#Start Line: Block about dogs
#End Line: Block about dogs

#Start Line: Block about cats
#End Line: Block about cats


I need  to insert  some text lines (value of variables)  between  pattern "Block about dogs"  and some between "Block about cats"


Thanks
0
Comment
Question by:IKeystone
3 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 39255424
Hi, what have you got already? Pls. post that.
0
 
LVL 5

Accepted Solution

by:
DOSLover earned 500 total points
ID: 39256528
Here is simple way of using sed to achieve this. First of all, please try 'which sed' command to know path to sed command. Typically it will be /usr/bin/sed. But it could be different on your system. Please try the following command (either from command line or in a script):
/usr/bin/sed -e '/#End Line: Block about dogs/i\
  This line goes between Block about dogs
' MySourceFile.txt > MyTargetFile.txt

Open in new window

0
 
LVL 5

Expert Comment

by:magento
ID: 39261799
Hi ,

Check the below post . Same as your request.

http://stackoverflow.com/questions/11694980/using-sed-insert-a-line-below-or-above-the-pattern

BR,

Magento
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem Imaging Computers With Clonezilla 2 83
Backing Up Ipset 9 98
incorrect file reading in bash 7 81
add some character at the end of line in vi 7 63
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now