Replace a string with multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a string with multiple lines
# 1  
Old 07-23-2015
Replace a string with multiple lines

Hello Guys,

I need to replace a string with multiple lines.

For eg:-
Code:
ABC,DEF,GHI,JKL,MNO,PQR,STU

need to convert the above as below:-

Code:
ABC,DEF,
GHI1
GHI2
GHI3,
JKL,MNO,
PQR1
PQR2
PQR3,
STU

i have tried using code as:-

Code:
echo "ABC,DEF,GHI,JKL,MNO,PQR,STU"| sed 's/GHI/GHI1\nGHI2\n/g'

it giving me as
Code:
ABC,DEF,GHI1nGHI2n,JKL,MNO,PQR,STU

thanks to help.

Regards,
Jaskirat

Last edited by Scrutinizer; 07-23-2015 at 07:46 PM.. Reason: code tags, removed quote tags
# 2  
Old 07-23-2015
Hello Jassi,

Not sure about your logic but as per your output following may help you in same.
Code:
 awk -vone=1 -vtwo=2 -vthree=3 -F, '{for(i=1;i<=NF;i++){A=i%3==0?A ORS $i one ORS $i two ORS $i three ORS:A=A?A OFS $i:$i}} END{gsub(/^$/,X,A);gsub(/\n\,/,",\n",A);print A}'  OFS=,  Input_file

Output will be as follows.
Code:
ABC,DEF
GHI1
GHI2
GHI3,
JKL,MNO
PQR1
PQR2
PQR3,
STU

Hope this helps.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 07-23-2015
Quote:
Originally Posted by jassi10781
Hello Guys,

I need to replace a string with multiple lines.

For eg:-
Code:
ABC,DEF,GHI,JKL,MNO,PQR,STU

need to convert the above as below:-

Code:
ABC,DEF,
GHI1
GHI2
GHI3,
JKL,MNO,
PQR1
PQR2
PQR3,
STU

i have tried using code as:-

Code:
echo "ABC,DEF,GHI,JKL,MNO,PQR,STU"| sed 's/GHI/GHI1\nGHI2\n/g'

it giving me as
Code:
ABC,DEF,GHI1nGHI2n,JKL,MNO,PQR,STU

thanks to help.

Regards,
Jaskirat
Standard sed does not understand \n in the replacement part, you need to use a hard newline instead:

Code:
$ echo "ABC,DEF,GHI,JKL,MNO,PQR,STU"| sed 's/GHI/\
GHI1\
GHI2\
GHI3/g'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove multiple lines from a particular string to particular string

Hi, I have a file containing the DDLs of tables in a schema. From that I need to remove all the lines from a starting string till a specific string. Here is an example. File1.txt ------------- CREATE TABLE "SCHEMA1"."LKP11_TBL_USERS" ( "ID" NUMBER(8,0) NOT NULL ENABLE, "USER_ID"... (3 Replies)
Discussion started by: satyaatcgi
3 Replies

2. Shell Programming and Scripting

Search a string and replace the same string above two lines?.

I need to search this "<CardinalMPI>" string and replace the same string above two lines of the string. Nov 16, 2012 12:58:34 PM INFO: Centinel LookUp ResponseXML : <CardinalMPI> <ReasonCode></ReasonCode> <ErrorDesc></ErrorDesc> <MerchantReferenceNumber></MerchantReferenceNumber>... (4 Replies)
Discussion started by: laknar
4 Replies

3. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

4. Shell Programming and Scripting

Find and replace multiple lines

I have a section of text in file A, see below # falkdjf lkjadf lkjadf lkajdf lkajdf lkajdf lkjadf lkjadf 234.234.2.234 lkjlkjlk 234.234.3.234 # Only the first line with "# falkdjf lkjadf lkjadf" is unique in the file. The new section that I want to overwrite the old section above is in... (1 Reply)
Discussion started by: jyang72211
1 Replies

5. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

8. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

9. Shell Programming and Scripting

replace multiple lines in multiple files

i have to search a string and replace with multiple lines. example Input echo 'sample text' echo 'college days' output echo 'sample text' echo 'information on students' echo 'emp number' echo 'holidays' i have to search a word college and replace the multiple lines i have... (1 Reply)
Discussion started by: unihp1
1 Replies

10. Shell Programming and Scripting

How can I replace multiple lines from different files

Suppose i have two files file1.txt Name : Raju Address:rt8pouououoiu City:tyretyeuetu file2.txt Address :28a The line "address:28a" in file2 has to get replaced in the address line of file1 .. The output should be Name :Raju Address:28a city :tyretyeuetu Please help me... (2 Replies)
Discussion started by: ranga27
2 Replies
Login or Register to Ask a Question