Replace String With Newline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace String With Newline
# 8  
Old 10-09-2012
I realised that, but even when I do so it doesn't appear to be any different to the original file.
# 9  
Old 10-09-2012
To edit the file itself (have a backup copy handy just in case):
Code:
printf '%s\n' '1,$s/^  *//' '1,$s/  *$//' 'g/^<PRODUCT / .,/^<\/PRODUCT>/j' w | ed -s file

In heredoc format:
Code:
ed -s file <<'EOED'
1,$s/^  *//
1,$s/  *$//
g/^<PRODUCT / .,/^<\/PRODUCT>/j
w
EOED

If you rather not modify the original file, ed's write command, w, accepts an optional filename argument. Alternatively, you can create a copy with cp and edit that.

Regards,
Alister

Last edited by alister; 10-09-2012 at 12:29 PM.. Reason: Added command to delete trailing spaces
This User Gave Thanks to alister For This Post:
# 10  
Old 10-09-2012
Sorry Lem, I must have been replying at the same time as you.

I've just tried yours and it says
sed: command garbled

---------- Post updated at 04:30 PM ---------- Previous update was at 04:24 PM ----------

Alister, your code worked perfectly.

Thank you all for your help.
# 11  
Old 10-09-2012
Quote:
Originally Posted by elixir_sinari
With some assumptions:
Code:
awk '{gsub(/^[[:blank:]]*|[[:blank:]]*$/,"")
if(/^<PRODUCT /) ORS=""; else if (/^<\/PRODUCT>/) ORS=RS}1' file

Solaris nawk doesn't support character classes e.g. ':blank:'
I'm not sure of the /usr/xpg4/bin/awk - you can try...
This User Gave Thanks to vgersh99 For This Post:
# 12  
Old 12-05-2012
Sorry to bump this old thread, but I'm having issues with this again.

I'm using the following
Code:
printf '%s\n' '1,$s/^  *//' '1,$s/  *$//' 'g/^<PRODUCT / .,/^<\/PRODUCT>/j' w | ed -s file

But for large files it isn't working for the whole file.

It's getting part way through and then looks to be only performing part of it.

This is how the file looks when I run this.
Code:
<FUNCTION>
<PRODUCTS>
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no"><SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX"><STOCK_QUANTITY DATA="0"/><STOCK_DATE DATA="1354298820"/></SUPPLIER></PRODUCT>
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no"><SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX"><STOCK_QUANTITY DATA="0"/><STOCK_DATE DATA="1354298820"/></SUPPLIER></PRODUCT>
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no"><SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX"><STOCK_QUANTITY DATA="0"/><STOCK_DATE DATA="1354298820"/></SUPPLIER></PRODUCT>
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no"><SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX"><STOCK_QUANTITY DATA="0"/><STOCK_DATE DATA="1354298820"/></SUPPLIER></PRODUCT>
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no"><SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX"><STOCK_QUANTITY DATA="0"/><STOCK_DATE DATA="1354298820"/></SUPPLIER></PRODUCT>
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no"><SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX"><STOCK_QUANTITY DATA="0"/><STOCK_DATE DATA="1354298820"/></SUPPLIER></PRODUCT>
..............
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no"><SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX"><STOCK_QUANTITY DATA="0"/><STOCK_DATE DATA="1354298820"/></SUPPLIER></PRODUCT>
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no">
<SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX">
<STOCK_QUANTITY DATA="0"/>
<STOCK_DATE DATA="1354298820"/>
</SUPPLIER>
</PRODUCT>
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no">
<SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX">
<STOCK_QUANTITY DATA="0"/>
<STOCK_DATE DATA="1354298820"/>
</SUPPLIER>
</PRODUCT>
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no">
<SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX">
<STOCK_QUANTITY DATA="0"/>
<STOCK_DATE DATA="1354298820"/>
</SUPPLIER>
</PRODUCT>
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no">
<SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX">
<STOCK_QUANTITY DATA="0"/>
<STOCK_DATE DATA="1354298820"/>
</SUPPLIER>
</PRODUCT>
<PRODUCT CODE="PROD1" ACTION="amend" VALIDATE="no">
<SUPPLIER PRODUCT="SUPPPROD1" ACTION="amend" CODE="WESTCOAXX">
<STOCK_QUANTITY DATA="0"/>
<STOCK_DATE DATA="1354298820"/>
</SUPPLIER>
</PRODUCT>
</PRODUCTS>
</FUNCTION>

Does anyone know how I can correct this?
# 13  
Old 12-05-2012
Wrench

Try the below as is..
Code:
$ uname -rs
SunOS 5.10
$ sed 's/^ *//
> /PRODUCT CODE/{
> :l
> N
> /<.PRODUCT>/{
> s/ *\n *//gp
> d
> }
> bl
> }' inputfile


Last edited by michaelrozar17; 12-06-2012 at 03:15 AM.. Reason: modified the script's logic..
This User Gave Thanks to michaelrozar17 For This Post:
# 14  
Old 12-06-2012
That doesn't work correctly.

It removes sections of the file

Code:
<FUNCTION>
<PRODUCTS>
</PRODUCT>> DATA="1354298820"/> ACTION="amend" CODE="WESTCOAXX">
</PRODUCT>> DATA="1354298820"/>CTION="amend" CODE="WESTCOAXX">
</PRODUCT>> DATA="1354298820"/>TION="amend" CODE="WESTCOAXX">
</PRODUCT>> DATA="1354298820"/>TION="amend" CODE="WESTCOAXX">
</PRODUCT>> DATA="1354298820"/>TION="amend" CODE="WESTCOAXX">
</PRODUCT>> DATA="1354298820"/>TION="amend" CODE="WESTCOAXX">
</PRODUCT>> DATA="1354298820"/>TION="amend" CODE="WESTCOAXX">
</PRODUCT>> DATA="1354298820"/>TION="amend" CODE="WESTCOAXX">
</PRODUCT>> DATA="1354298820"/>TION="amend" CODE="WESTCOAXX">
</PRODUCTS>
</FUNCTION>

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