Visit The New, Modern Unix Linux Community

Sed; insert text two lines above match

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed; insert text two lines above match
# 1  
Sed; insert text two lines above match


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

My goal is to insert another line, like so:

# v0004 - Some text
# v0005 - More text
# v0006 - New text

# 2  
Do you have to use sed? Easily done with ed. This is an edit inline example (editing the file itself).

ed file.txt <<EOF
# v0006 - New text

This User Gave Thanks to cjcox For This Post:
# 3  
not so efficient , but try this.
line_no=`awk END' { print NR-1 } ' file`
text="v0006 - New text"
sed ''"$line_no"'i'"# $text"'' file

# 4  
Try also
awk '{B[NR%3]=$0} NR>2{ print B[(NR+1)%3]} /SEPARATOR/ {print "# v0006 - New text"}  END {print B[(NR+2)%3]; print B[(NR+3)%3]}' file1
# v0004 - Some text
# v0005 - More text
# v0006 - New text

# 5  
Hi All!
Thanks for the replies! I'm afraid I wasn't clear enough in my initial example. I realized that when I received my first reply, but I wasn't able to reply anymore.

The separator is NOT the last line in the file, I want to automatically insert a line into an existing changelog, that is part of a script, which starts below the separator...
I would like to use the separator, because sometimes the comments in the changelog span multiple lines, so I can't look for the last v000x and append a new line below it.

Like so:

# v0004 - Some text
# v0005 - More text
# v0006 - New text
Script start here...

---------- Post updated at 10:39 AM ---------- Previous update was at 10:33 AM ----------

Never mind. RudiC's piece of code, even though it's completely incomprehensible, does the job!
# 6  
awk '
                {B[NR%3]=$0}                    # use a circular 3 line buffer (B array) for delayed printing
NR>2            {print B[(NR+1)%3]}             # from line three, print line No. - 2 (acquaint yourself with the "%" modulo function)
/SEPARATOR/     {print "# v0006 - New text"}    # if the search string is found, print the text after the text of 2 lines ago
END             {print B[(NR+2)%3]              # just empty the buffer when input is done
                 print B[(NR+3)%3]
' file

Last edited by RudiC; 11-16-2015 at 06:29 AM..
This User Gave Thanks to RudiC For This Post:

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

Test Your Knowledge in Computers #23
Difficulty: Easy
One Terabyte (1 TB) is equal to 1000 GB.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Insert text after match in XML file

Having a little trouble getting this to work just right. I have xml files that i want to split some data. I have 2 <name> tags within the file I would like to take only the first tag and split the data. tag example. From this. TAB<Name>smith, john</Name> to TAB<Name>smith,... (8 Replies)
Discussion started by: whegra
8 Replies

3. Shell Programming and Scripting

Search a certain char and insert new text if a match found

Have a file which has the create statement like below create table emp ( empno integer, empname char(50)) primary index(empno); i need to find a string starting with create and ends with semi-colon ;. if so insert the below statement before create statement rename table emp to emp_rename;... (2 Replies)
Discussion started by: Mohan0509
2 Replies

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

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

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

7. Shell Programming and Scripting

Insert few lines above a match using sed, and within a perl file.

Greetings all, I am trying to match a string, and after that insert a few lines above that match. The string is "Version 1.0.0". I need to insert a few lines ONLY above the first match (there are many Version numbers in the file). The rest of the matches must be ignored. The lines I need to... (2 Replies)
Discussion started by: nagaraj s
2 Replies

8. Shell Programming and Scripting

Insert text file only after the first match with SED

Hello, I'm new in Shell scripting but i should write a script, which inserts the license header out of a txt-File into the files in our Projekt. For the Java classes it runs without Problems but for XML files not. At xml-files i have to put the license Header after the xml-Header (?xml... (1 Reply)
Discussion started by: PhoenixONE
1 Replies

9. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 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