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):
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:
But the problem is with SED that does the actual insert. the error is:
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
Please use code tags when posting sample input/output or code.
Your problem is the quotes:
The variable does not get expanded, because it's inside the single quotes, taken as literal. Turn the single quotes off and on:
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:
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)