Replace String With Newline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace String With Newline
# 1  
Old 10-09-2012
Replace String With Newline

Hi,

I'm struggling with a string replacement.

I have an XML file which is in the following layout
Code:
<FUNCTION>
  <PRODUCTS>
    <PRODUCT CODE="PRODUCE" ACTION="amend" VALIDATE="no">
      <SUPPLIER PRODUCT="PRODUCT" ACTION="amend" CODE="SUPPLIER">
        <STOCK_QUANTITY DATA="21"/>
        <STOCK_DATE DATA="1349284740"/>
      </SUPPLIER>
    </PRODUCT>
    <PRODUCT CODE="PRODUCE" ACTION="amend" VALIDATE="no">
      <SUPPLIER PRODUCT="PRODUCT" ACTION="amend" CODE="SUPPLIER">
        <STOCK_QUANTITY DATA="21"/>
        <STOCK_DATE DATA="1349284740"/>
      </SUPPLIER>
    </PRODUCT>
    <PRODUCT CODE="PRODUCE" ACTION="amend" VALIDATE="no">
      <SUPPLIER PRODUCT="PRODUCT" ACTION="amend" CODE="SUPPLIER">
        <STOCK_QUANTITY DATA="21"/>
        <STOCK_DATE DATA="1349284740"/>
      </SUPPLIER>
    </PRODUCT>
  </PRODUCTS>
</FUNCTION>

I am attempting to amend the file so that it is in the following layout
Code:
<FUNCTION>
<PRODUCTS>
<PRODUCT CODE="PRODUCE" ACTION="amend" VALIDATE="no"><SUPPLIER PRODUCT="PRODUCT" ACTION="amend" CODE="SUPPLIER"><STOCK_QUANTITY DATA="21"/><STOCK_DATE DATA="1349284740"/></SUPPLIER></PRODUCT>
<PRODUCT CODE="PRODUCE" ACTION="amend" VALIDATE="no"><SUPPLIER PRODUCT="PRODUCT" ACTION="amend" CODE="SUPPLIER"><STOCK_QUANTITY DATA="21"/><STOCK_DATE DATA="1349284740"/></SUPPLIER></PRODUCT>
<PRODUCT CODE="PRODUCE" ACTION="amend" VALIDATE="no"><SUPPLIER PRODUCT="PRODUCT" ACTION="amend" CODE="SUPPLIER"><STOCK_QUANTITY DATA="21"/><STOCK_DATE DATA="1349284740"/></SUPPLIER></PRODUCT>
</PRODUCTS>
</FUNCTION>

I have managed to get it so that the leading spaces on each row have been removed and all the records are on one row, but I am struggling to replace
><PRODUCT CODE
with
>
<PRODUCT CODE

The code I currently have is
Code:
cat file.xml | sed 's/^ *//g' | sed 's/ *$//g' | tr -d '\n'

Can anyone help?
# 2  
Old 10-09-2012
With some assumptions:
Code:
awk '{gsub(/^[[:blank:]]*|[[:blank:]]*$/,"")
if(/^<PRODUCT /) ORS=""; else if (/^<\/PRODUCT>/) ORS=RS}1' file

# 3  
Old 10-09-2012
Thanks for the quick reply

Unfortunately, this doesn't seem to work for me.

When I run it, it says
Code:
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 2
awk: illegal statement near line 2
awk: illegal statement near line 2
awk: syntax error near line 2
awk: bailing out near line 2


Last edited by Franklin52; 10-10-2012 at 04:25 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 10-09-2012
Use nawk instead of awk.
# 5  
Old 10-09-2012
I've just tried with nawk and I no longer get the errors but nothing appears to change.

Any suggestions?
# 6  
Old 10-09-2012
That command will not change your input file. It'll just write the output to standard output. You'll need to redirect standard ouput from that command to a temporary file and then rename that temporary file to your original file (after checking that everything is OK, of course).
# 7  
Old 10-09-2012
With sed, try:
Code:
sed -n 's/^ *//;/<PRODUCT CODE/,/<\/PRODUCT>/!p;/<PRODUCT CODE/,/<\/PRODUCT>/{/<PRODUCT CODE/h;/<PRODUCT CODE/!H};/<\/PRODUCT>/{x;s/\n//g;p}' <inputfile

If you want to change your input file in place (be careful! make backups before trying, even if this sed command below should itself create a backup), try:
Code:
sed -i".sedbackup" -n 's/^ *//;/<PRODUCT CODE/,/<\/PRODUCT>/!p;/<PRODUCT CODE/,/<\/PRODUCT>/{/<PRODUCT CODE/h;/<PRODUCT CODE/!H};/<\/PRODUCT>/{x;s/\n//g;p}' inputfile

--
Bye
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace string including newline

Hi, I am trying to do some transformation on a large file and I am getting some troubles trying remove newlines only when the last character of a line is a symbol (in this case is a pipe "|"). I have tried with sed like this: sed -i 's/|\n/|/g' my_file or sed -i 's/|$/|/gg' my_file... (5 Replies)
Discussion started by: ngb
5 Replies

2. Shell Programming and Scripting

Replace newline in a string

I have a string like below: {\rtf1\fbidis\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}{\f1\fnil MS Sans Serif;}} \viewkind4\uc1\pard\ltrpar\lang2057\f0\fs16 19/11/2010 SOME DESCRIPTION. \par \lang1033\f1\par } I have to replace the newline character with null in the... (8 Replies)
Discussion started by: Pratik4891
8 Replies

3. Shell Programming and Scripting

Replace newline with comma.

I have output from a file like this: 15,01,11,14:06 235 I would like to change this to: 15,01,11,14:06,235 Removing newline and change to "," I now this can be done with tr cat OUT | tr '\n' ','' My problem is that tr is not implemented in this shell. sed is, show it should be... (7 Replies)
Discussion started by: Jotne
7 Replies

4. Shell Programming and Scripting

replace >< with > newline <

Hi All, I have the command in PERL for performing this, but Can you please suggest me how can i perform this using AWK: My input xml file looks like this: <aaa>hello</aaa><bbb>hai</bbb> I want the output like this ( means need new line after end of each xml tag): <aaa>hello</aaa>... (1 Reply)
Discussion started by: HemaV
1 Replies

5. Shell Programming and Scripting

Help with sed matching <tag1> newline spaces <tag2> and replace the value in the same string format

Hi, I'm very new to shell scripting and have searched google and this forum for quite some time now. I have the following in my xml file: <recipients> <member>value1</member> </recipients> I need to find a string <recipients> that follows with a new-line and bunch of spaces and... (5 Replies)
Discussion started by: mgharios
5 Replies

6. UNIX for Dummies Questions & Answers

replace text string with a newline

I want to replace a text string with a newline. I have a long text file of random characters. I want to replace all the occurences of "pe" with a newline. How can I do that in Unix? There's a thread from 2004 saying that you can do something like this with sed by actually pressing the return... (1 Reply)
Discussion started by: aaronpoley
1 Replies

7. Shell Programming and Scripting

Replace a string with newline

Hi all I have the problem to substitute a string with newline in Perl. Can anybody help me? And also how to replace a string with opening bracket (e.g. (START ) with a whitespace/null character? Thanks in advance. (1 Reply)
Discussion started by: my_Perl
1 Replies

8. UNIX for Dummies Questions & Answers

replace string with a newline string

Hi, I wanted to replace these lines in vi editor: input-- uid=ESVPEME | eriMasterDomain=EAMCS | eriCountry=El | ou=ESV uid=EPYCAR | eriMasterDomain=EAMCS | eriCountry=Argentina | ou=CEA uid=ERCFGA | eriMasterDomain=EAMCS | eriCountry=Costa | ou=ERC uid=EDGLUCU | eriMasterDomain=EAMCS... (5 Replies)
Discussion started by: hegdeshashi
5 Replies

9. Shell Programming and Scripting

Replace comma with newline

Hi, for some reason I cant seem to figure this out. I have a file which looks something like this word word word word word,word,word word word word,word,word,word,word word word Basically I want this whole thing to be a list with 1 word on each line like this... word word word... (1 Reply)
Discussion started by: eltinator
1 Replies

10. Shell Programming and Scripting

replace a newline (\n)

dear all: maybe i have a file like : 12 34 56 78 end how do write can i replace newline into NA : make the file inte : 12 NA 34 NA 56 78 END (3 Replies)
Discussion started by: jeter
3 Replies
Login or Register to Ask a Question