Insert text file only after the first match with SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert text file only after the first match with SED
# 1  
Old 02-05-2010
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 version="1.0" ... ?) but the xml Header is not only one line all the time, it could be about 2 or 3 lines.
first i tried with this.

Code:
sed '/?>/ r License_Header.txt' < xml-Datei.xml > tempFile.txt
mv tempFile.txt xml-Datei.xml

This is searching for the expression "?>" an insert the license Header after this expression and it also runs.
But if this Expression is in the file one more time, f.e. in another xml-header whis is in a comment, the shouldn't be insert again.

so i try to find a way, that only the first match was taken to consideration and hope that somebody can help me

thanks and sry for my verry bad english
# 2  
Old 02-06-2010
Something like this?
Code:
awk '/?>/ && !f{system("cat License_Header.txt");f=1;next}1' xml-Datei.xml > tempFile.txt
mv tempFile.txt xml-Datei.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

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

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

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

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

7. Shell Programming and Scripting

Using sed to insert text file at first line

sed '1r file.txt' <source.txt >desti.txt This example will insert 'file.txt' between line 1 and 2 of source.txt. sed '0r file.txt' <source.txt >desti.txt gives an error message. Does anyone know how 'sed' can insert 'file.txt' before the first line of source.txt? (18 Replies)
Discussion started by: psve
18 Replies

8. Shell Programming and Scripting

Sed insert text at first line of empty file

I can't seem to get sed to allow me to insert text in the first line of an empty file. I have a file.txt that is a 0 byte file. I want sed to insert " fooBar" onto the first line. I've tried a few options and nothing seems to work. They work just fine if there's text in the file tho. Help? (4 Replies)
Discussion started by: DC Slick
4 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. Shell Programming and Scripting

SED- Insert text at top of file

Does anyone know how to insert text at the top and bottom of a file using sed? (12 Replies)
Discussion started by: MBGPS
12 Replies
Login or Register to Ask a Question