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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert few lines above a match using sed, and within a perl file.
# 1  
Old 01-31-2012
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 insert are (above Version 1.0.0):
Code:
Version 2.0.0 
-------------
2012-01-11 - user_name
prod 2.0.0

This needs to be done in all files named VERSIONINFO recursively from the top level folder.
I have tried to do this in a perl file:
Code:
#-------------
@readme_files = `find . -name "VERSIONINO"`;
foreach $readmes (@readme_files)        {
chomp ($readmes);
        
$ver_match = `sed -n '/^Version [0-9].[0-9].[0-9]/{p;q;}' "$readmes"`;
        if ($ver_match)
                {push (@vermatch,  $ver_match);
`sed  -i '/"$ver_match"/ i \Version 2.0.0\\n-------------\\n2012-01-15 - user_name\\nprod 2.0.0\\n' "$readmes"`;
}
        else {
                push (@no_vermatch, $readmes);
                }
}
print "Versions found @vermatch\n";
print "Files with no version numbers @no_vermatch\n";

But the problem is with SED that does the actual insert. the error is:
Code:
sed: -e expression #1, char 15: unterminated address regex

sed, that picks out the first match works fine, the insert part does not. I tried escaping characters, but have not been successful. I believe I need to escape some characters in the sed insert command, but have been lost now.

Would appreciate any input to solve this.
Thanks in advance,

Last edited by Franklin52; 01-31-2012 at 03:22 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-31-2012
Output in green.
Code:
$ cat inputfile
Version 1.0.0

Version 1.0.0

Version 1.0.0
$
$ sed '0,/Version 1.0.0/i Version 2.0.0\n-------------\n2012-01-11 - user_name\nprod 2.0.0' inputfile
Version 2.0.0
-------------
2012-01-11 - user_name
prod 2.0.0
Version 1.0.0

Version 1.0.0 

Version 1.0.0 
$
$


Last edited by balajesuri; 01-31-2012 at 02:56 AM..
# 3  
Old 01-31-2012
Please use code tags when posting sample input/output or code.

Your problem is the quotes:
Code:
sed  -i '/"$ver_match"/ i YourString'

The variable does not get expanded, because it's inside the single quotes, taken as literal. Turn the single quotes off and on:
Code:
sed  -i '/'"$ver_match"'/ i YourString'

and you should be good to go.

However this can be simplified quite a bit. All this perl stuff is not needed, since it only calls external tools (find, sed). You can do without:
Code:
v="Version 1.0"; 
str="Version 2.0.0\x0a-------------\x0a2012-01-15 - user_name\x0aprod 2.0.0"
find .  -name "VERSIONINFO" | xargs sed -i '/^'"$v"'/ i '"$str"

The \x0a is a hex code of the newline char.
xargs will feed all the files to sed as arguments, so sed is actually called like:
Code:
sed -i <command> ./path1/VERSIONINFO ./path2/VERSIONINFO ./path3/VERSIONINFO


Last edited by mirni; 01-31-2012 at 03:33 AM..
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

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. UNIX for Dummies Questions & Answers

sed command to Insert a line before the last four lines of the file

By using sed command, How to insert a new line before the last four lines of the file. Old Line Old Line NEW LINE! Old Line Old Line Old Line Old Line (8 Replies)
Discussion started by: wridler
8 Replies

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

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

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

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

8. Shell Programming and Scripting

swapping lines that match a condition using sed, perl or the like

I'm a bit new to regex and sed/perl stuff, so I would like to ask for some advice. I have tried several variations of scripts I've found on the net, but can't seem to get them to work out just right. I have a file with the following information... # Host 1 host 45583 { filename... (4 Replies)
Discussion started by: TheBigAmbulance
4 Replies

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

10. Shell Programming and Scripting

how to insert text between lines of an existing file using perl

Hi , I need some inputs on how to open a file (file.txt) and parse the text example aaa of the file and bbb of the file and add the text zzzz once i parse (aaa and bbb) and followed by the remaining of the text as it is in the file using perl programming. Thanks in advance (3 Replies)
Discussion started by: madhul2002
3 Replies
Login or Register to Ask a Question