Using sed to insert text between lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to insert text between lines
# 1  
Old 06-29-2016
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:
Code:
abcd[0].efgh[N].zzzz=blah
abcd[0].efgh[N].xxxx=blah

Where N = 0 to 2

Original File:
Code:
abcd[0].efgh[0].wwxx=aaaaa
abcd[0].efgh[0].yyzz=bbbbb
abcd[0].efgh[1].wwxx=aaaaa
abcd[0].efgh[1].yyzz=bbbbb
abcd[0].efgh[2].wwxx=aaaaa
abcd[0].efgh[2].yyzz=bbbbb

Change to:
Code:
abcd[0].efgh[0].wwxx=aaaaa
abcd[0].efgh[0].yyzz=bbbbb
abcd[0].efgh[0].zzzz=blah
abcd[0].efgh[0].xxxx=blah
abcd[0].efgh[1].wwxx=aaaaa
abcd[0].efgh[1].yyzz=bbbbb
abcd[0].efgh[1].zzzz=blah
abcd[0].efgh[1].xxxx=blah
abcd[0].efgh[2].wwxx=aaaaa
abcd[0].efgh[2].yyzz=bbbbb
abcd[0].efgh[2].zzzz=blah
abcd[0].efgh[2].xxxx=blah


This is what I have done so far (and it works with Linux GNU sed), but I am looking for a more elegant way to do it. Perhaps using backreferencing?

Code:
sed "{
/efgh\[0\].yyzz=bbbbb/a abcd[0].efgh[0].wwxx=aaaaa\nabcd[0].efgh[0].xxxx=blah
/efgh\[1\].yyzz=bbbbb/a abcd[0].efgh[1].wwxx=aaaaa\nabcd[0].efgh[1].xxxx=blah
/efgh\[2\].yyzz=bbbbb/a abcd[0].efgh[2].wwxx=aaaaa\nabcd[0].efgh[2].xxxx=blah
}" dataset.txt

Any ideas?

Thanks in advance.
# 2  
Old 06-29-2016
Does it need to be sed?
# 3  
Old 06-29-2016
I'm happy to use awk, perl or python. Just wondered if it was possible using sed?

Thanks.
# 4  
Old 06-29-2016
Actually, thinking twice, it is. Try
Code:
sed 's/\(efgh\[\)\([[:digit:]]\)\(\].yyzz=bbbbb\)/\1\2\3\nabcd[0].efgh[\2].wwxx=aaaaa\nabcd[0].efgh[\2].xxxx=blah/' file
abcd[0].efgh[0].wwxx=aaaaa
abcd[0].efgh[0].yyzz=bbbbb
abcd[0].efgh[0].wwxx=aaaaa
abcd[0].efgh[0].xxxx=blah
abcd[0].efgh[1].wwxx=aaaaa
abcd[0].efgh[1].yyzz=bbbbb
abcd[0].efgh[1].wwxx=aaaaa
abcd[0].efgh[1].xxxx=blah
abcd[0].efgh[2].wwxx=aaaaa
abcd[0].efgh[2].yyzz=bbbbb
abcd[0].efgh[2].wwxx=aaaaa
abcd[0].efgh[2].xxxx=blah

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert Text after one, two, three lines & so on..

I want to insert "Text" in each file as a place where I mentioned below "Insert Text Here". These files are something like news of newspaper. Generally, newspaper headlines contain one or two lines. I don't know how it can be identified whether Text is inserted after first line or second line. ... (10 Replies)
Discussion started by: imranrasheedamu
10 Replies

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

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

4. Shell Programming and Scripting

How do I insert text with sed ?

Hi I was wondering if anyone new of a solution to this problem? I need to copy a time stamp that is on a line of .text in a text file into multiple positions on the same line. I need to insert the time stamp on the same line between every occurance of the text ".pdf_.html" right after the... (9 Replies)
Discussion started by: Paul Walker
9 Replies

5. Shell Programming and Scripting

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 #Start of file entry { } #End of file entry new bit of text has to be put in just above the } eg #Start of file entry { New bit of text } #End of file entry (7 Replies)
Discussion started by: eeisken
7 Replies

6. Shell Programming and Scripting

Insert text using sed

sed 's/$/TEST/g' will insert TEST at the end of each line. i want to insert TEST at column 64 (7 Replies)
Discussion started by: lawsongeek
7 Replies

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

8. UNIX for Dummies Questions & Answers

Insert Text on lines having the string word

I need help on how I can accomplish my task. I hope someone can help me since I've researching and trying to accomplish this for hours now. Basically, I need to comment-out (or insert a # sign in the beginning of the line) a line when the line has the specific word I am searching. Example I have... (3 Replies)
Discussion started by: Orbix
3 Replies

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

10. UNIX for Dummies Questions & Answers

Insert Text With Sed

Hello. Trying to insert text at line 1 and after last line of file. I have searched posts but nothing seems to work. I keep getting extra characters error or nothing gets inserted into the file. #!/bin/sh touch textfile.txt sed 'i\ Add this line before every line with WORD' textfile.txt ... (5 Replies)
Discussion started by: steveramsey
5 Replies
Login or Register to Ask a Question