Visit The New, Modern Unix Linux Community


sed insert text 2 lines above pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed insert text 2 lines above pattern
# 1  
sed insert text 2 lines above pattern

Hi

I am trying to insert a block of text 2 lines above a pattern match using sed

eg
Code:
#Start of file entry
{

}
#End of file entry

new bit of text has to be put in just above the } eg

Code:
#Start of file entry
{

New bit of text
}
#End of file entry

I have tried the
Code:
sed /Pattern/i "text" filename

but it keeps getting garbled with the \n

Last edited by Scrutinizer; 04-04-2012 at 06:31 AM.. Reason: Code tags
# 2  
awk

Hi,

Try this one,

Code:
awk -v pat="}" '{a[NR]=$0;}END{for(i=1;i<=NR;i++){if(a[i] ~ pat ){print "Insert Block\n"a[i];}else{print a[i];}}}' file

Cheers,
RangaSmilie

Last edited by rangarasan; 04-04-2012 at 06:38 AM..
# 3  
Try:
Code:
sed '/}/i\
New bit of text\
' infile

Code:
awk '/}/{print "New bit of text"}1' infile

# 4  
Hi rangarasan

thanks for that but I'm a bit confused not that used to awk

is pattern my pattern match eg
Code:
/\}\/n#End of file entry 1/

or is this the block of text I want to insert and how will this handle multiple inserts depending in the Start and End value eg in a file like test.txt

Code:
#Start of file entry 1
{
Insert text block a
}
#End of file entry 1

Code:
#Start of file entry 2
{
Insert of text block b
}
#End of file entry 2


Last edited by Scrutinizer; 04-04-2012 at 06:39 AM.. Reason: code tags
# 5  
awk

If } found then we need add some part before "}" line right?
If Yes, check with the awk else provides us desired input and desired output.
# 6  
Hi

its more if

#End of file entry 1

is found then insert the text before the } above it

I have tried the following but it returns nothing
Code:
sed -e 's/\}\n#End of file entry 1/#Start of Range\n range 123.456.789.0\n\}#End of file entry 1/' test


Last edited by Franklin52; 04-04-2012 at 07:05 AM.. Reason: Please use code tags for code and data samples, thank you
# 7  
Sed can not be used to operate beyond newlines like that... Try:
Code:
sed '/}/{N;/\n#End of file entry 1/{s/^/text to insert\
/;};}' infile

or more modern sed:
Code:
sed '/}/{N;/\n#End of file entry 1/{s/^/text to insert\n/;};}' infile


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #521
Difficulty: Medium
0b110010000001 = 2^11 + 2^10 + 2^7 + 2^0
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

2. 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

3. Shell Programming and Scripting

Sed; insert text two lines above match

Hi! Considering below text, how would I use sed to insert text right below the v0005-line, using the SEPARATOR-line as a pattern to search for, so two lines above the separator? I can do it right above the separator, but not 2 lines... # v0004 - Some text # v0005 - More text #... (5 Replies)
Discussion started by: indo1144
5 Replies

4. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Insert rows of text into a file after pattern

SHELL=bash OS=rhel I have a file1 that contains text in sentences like so: file1 this is a sentence this is a sentence this is a sentence I also have a file2 like so: file2 command=" here is some text here is more text again we have some text " I wish to echo the text from... (4 Replies)
Discussion started by: jaysunn
4 Replies

8. Shell Programming and Scripting

sed/awk to insert multiple lines before pattern

I'm attempting to insert multiple lines before a line matching a given search pattern. These lines are generated in a separate function and can either be piped in as stdout or read from a temporary file. I've been able to insert the lines from a file after the pattern using: sed -i '/pattern/... (2 Replies)
Discussion started by: zksailor534
2 Replies

9. Shell Programming and Scripting

insert text into another file after matching pattern

i am not sure what i should be using but would like a simple command that is able to insert a certain block of text that i define or from another text file into a xml file after a certain match is done for e.g insert the text </servlet-mapping> <!-- beechac added - for epic post-->... (3 Replies)
Discussion started by: cookie23patel
3 Replies

10. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

Featured Tech Videos