Inserting new line after match of a fixed string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting new line after match of a fixed string
# 1  
Old 04-07-2004
Inserting new line after match of a fixed string

Hi,

I have a file which contains many occurances of a string say "hellosunil".

I want to insert a newline charcater after all the "hellosunil" strings in the file.

trying to use sed,

sed -e 's/hellosunil/\\nhellosunil/g' file1

sed help says u cannot substitute a regular expression using new line char. so the above thing does not work?

how can i achieve this using sed or any other command.
# 2  
Old 04-07-2004
echo abababab | sed 's/b/\
/g'

will replace all of the b's with newline characters. I really typed a newline after the backslash. This makes the sed command a two line command.
This User Gave Thanks to Perderabo For This Post:
# 3  
Old 04-07-2004
thanks a lot it has worked
# 4  
Old 04-07-2004
Re: Inserting new line after match of a fixed string

Quote:
Originally posted by sunil_neha
Hi,

I have a file which contains many occurances of a string say "hellosunil".

I want to insert a newline charcater after all the "hellosunil" strings in the file.

trying to use sed,

sed -e 's/hellosunil/\\nhellosunil/g' file1

sed help says u cannot substitute a regular expression using new line char. so the above thing does not work?

how can i achieve this using sed or any other command.
using awk:
Code:
awk '{gsub(/hellosunil/,"\nhellosunil");print}'

# 5  
Old 04-13-2004
I tried the foll. command:

awk '{gsub(/<eCRMver1:Consumer>/, "\n<eCRMver1:Consumer>"); gsub(/<\/eCRMver1:message>/, "\n<\/eCRMver1:message>");print}' $input_file > $tem
p_output_file1

ERROR:

awk: 0602-581 The result <?xml version="1.0" of the gsub function
cannot be longer than 10,239 bytes.
The input line number is 1. The file is /appl/ecrmhome/gb53/VSC.xml.
The source line number is 1.


my VSC.xml is 35000 (and it can be much more ???)

Please suggest.
# 6  
Old 04-13-2004
taking the entire file in a variable and applying sed WORKS...

total_xml_msg=`cat $input_file`
echo $total_xml_msg | sed 's/<eCRMver1:Consumer>/\
<eCRMver1:Consumer>/g' > $temp_output_file1

good for the time being...
# 7  
Old 04-13-2004
Try:
sed 's/<eCRMver1:Consumer>/\
<eCRMver1:Consumer>/g' < $input_file > $output_file1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inserting n characters to beginning of line if match

I would like to insert n number of characters at the beginning of each line that starts with a given character. If possible, I would be most appreciative for a sed or awk solution. Given the data below, I would like to be able to insert either 125 spaces or 125 "-" at the beginning of every line... (6 Replies)
Discussion started by: jvoot
6 Replies

2. Shell Programming and Scripting

Get remaining line after string match

Want to get the remaining line after pattern match Here it starts - executed commands : - pattern to identify 100:27:500:1:34:END Required output:100:27:500:1:34:END awk '{if(/pattern to identify/) print $2}' < file I have used above code and it not giving... (3 Replies)
Discussion started by: rozee
3 Replies

3. Shell Programming and Scripting

Print entire line only if certain fixed character matches the string

Hi All, I have a file testarun.txt contains the below lines and i want to print the lines if the character positions 7-8 matches 01. 201401011111 201401022222 201402013333 201402024444 201403015555 201403026666 201404017777 201404028888 201405019999 201405020000 I am trying the... (4 Replies)
Discussion started by: Arunprasad
4 Replies

4. Shell Programming and Scripting

Inserting new line if two sequential lines begin with the same string

Hi I have a file like this: Peter North Mary Peter North Peter Borough Mary I need there to put 'X' (or anything) on a new line between the two lines where 'Peter' begins the line. There will be many matches to this string, but they will always begin with 'Peter'. Ie, the resulting... (2 Replies)
Discussion started by: majormajormajor
2 Replies

5. Shell Programming and Scripting

Delete the last line if match the string

Hi, How to delete the last line if is match the below string else no action... String checking "END OF FILE. ROW COUNT: " 9f680174-cb87-4f71-887a-93b6f62fa5aa|20077337254|2 9f680174-cb87-4f71-887a-93b6f62fa5aa|20077337254|0 229f680174-cb87-4f71-887a-93b6f62fa5aa|20077337254|3 END OF... (2 Replies)
Discussion started by: bmk
2 Replies

6. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

7. Shell Programming and Scripting

search for a string without fixed delimiter in the line

Hi Need your help to assign the string to a variable from a line which has no fixed delimter in unix. for example , my file contains Name="report"" File Name one="test1" File Name two="test2" now how do I read report , test1 and test2 ? var1=report var2=test1 var3=test2 ... (1 Reply)
Discussion started by: rashmisb
1 Replies

8. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

9. Shell Programming and Scripting

Fetch the rows with match string on a fixed lenth text file - NO delimiters

Hi I am trying to fetch the rows with match string "0000001234" Input file looks like below: 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1... (6 Replies)
Discussion started by: nareshk
6 Replies

10. Shell Programming and Scripting

Match String and get line number and filename

Hi All, I'm new to unix shell scripting.. Could someone guide me. I have to search a string in the entire directory, once the search string is matched, it should print the line number of the string that matches and also the line and along with it, it should print the file name. Thanks,... (5 Replies)
Discussion started by: thenz
5 Replies
Login or Register to Ask a Question