LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 02-27-2010, 12:34 PM   #1
Linuxready
LQ Newbie
 
Registered: Feb 2010
Posts: 3

Rep: Reputation: 0
Need to insert a newline using sed on all records in a file


I have an XML file all in one stream, without any Carriage Return / Line Feeds. The file looks something like this:

<tag1>data</tag1><tag2>data2</tag2><tag3>etc</tag3>

Where there is a ><, I need to insert a new line in between the >< so that the file looks like:

<tag1>data</tag1>
<tag2>data2</tag2>
<tag3>etc</tag3>

myfile contains <tag1>data</tag1><tag2>data2</tag2><tag3>etc</tag3>

filename=$myfile
cat $filename | sed 's|><|>\<|'g > new

new was the same as $filename

Then I treid cat $filename | sed 's|><|\<|'g > new

cat new responded with
<tag1>data</tag1<tag2>data2</tag2<tag3>etc</tag3<

What am I doing wrong?

Thanks!!
 
Old 02-27-2010, 12:43 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Not thoroughly tested! But maybe try:

Code:
sed -i 's%\(</tag[0-9]>\)%\1\n%g' testfile
That will edit the file in-place, so make a backup of it before proceeding. It looks for the closing tag, and replaces it with itself PLUS a newline. It works for me in the console, with a test file containing your example text, but in the real situation, it may not work, depending on what actual DATA is in your file between the tags.


I'm not sure fully what you're doing wrong in your first attempt(s) there -- you'd need to explain what your code was supposed to do, as I don't quite understand it.
 
Old 02-27-2010, 01:18 PM   #3
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
How about:
Code:
sed 's/></>\n</g'
 
Old 02-27-2010, 01:58 PM   #4
Linuxready
LQ Newbie
 
Registered: Feb 2010
Posts: 3

Original Poster
Rep: Reputation: 0
When I do the command below on the command line

cat $filename | sed -i 's%\(</tag[0-9]>\)%\1\n%g' > testfile

I get the following error:
sed: illegal option -- i



When I do
cat $filename | sed 's/></>\n</g' > testfile

I get n between the ><
 
Old 02-27-2010, 02:13 PM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
What is your sed version
Code:
bash-3.1$ sed --v 
GNU sed version 4.2.1
or
bash-3.1$ sed --version
GNU sed version 4.2.1
 
Old 02-27-2010, 02:32 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
@ linuxready -- you are not supposed to CAT the file into the command I gave; the "-i" option specifies an in-place operation, meaning that SED itself will read the file and operate on it; no cat required.

If you insist on using cat, then remove the -i from the sed statement, cat your file into the sed statement, and use a shell redirect to dump the output into a new file.

PS - NOTE, that "testfile" in my example, refers to the file you are operating on (I should have used the name "filename" instead -- sorry.)

Last edited by GrapefruiTgirl; 02-27-2010 at 02:34 PM.
 
Old 02-27-2010, 03:04 PM   #7
Linuxready
LQ Newbie
 
Registered: Feb 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Sorry!!! I just realized that I was working on the SunOS 5.10 Unix server and not our Linux server.

So sed --v, sed -V, or sed -version is not listing the version of sed on the Unix server.

Also,
I tried cat $filename | sed 's%\(</tag[0-9]>\)%\1\n%g' > new

and it put n in between >< for instance >n<
 
Old 02-27-2010, 03:08 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
I've asked for this thread to be moved to the Solaris forum, where the help offered will hopefully be better suited to the OS you're dealing with.

Meanwhile, I suppose you need to examine whatever the differences are between GNU sed, and whatever sed you have at your disposal, and adjust the suggestion(s) given to see if you can produce the desired functionality. OR, (and this may be more trouble than it's worth) seek a method other than sed, for this task (though it is IMO definitely a sed job).
 
Old 02-27-2010, 03:10 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <*Solaris> as that system uses antiques that don't have half the functionality of the GNU variants, and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 02-27-2010, 03:34 PM   #10
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Found out that you have to place the newline literary.You have to press enter. Can't test it myself.
Code:
$filename | sed 's%\(</tag[0-9]>\)%\1\
%g' > new
hm works also in a bash script.

Last edited by whizje; 02-27-2010 at 03:42 PM. Reason: tested
 
Old 02-27-2010, 03:45 PM   #11
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
@ whizje -- nice find! How primitive is that that a \n doesn't work..

Now we just need the OP to test for us, and see if it works in their environment.

Thanks,

Sasha
 
Old 02-27-2010, 09:55 PM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Hey, two LQ mods bashing Solaris in the same thread ...

Let me answer to that:

Solaris vs Gnu sed behavior differences are mainly due to the fact Solaris and Gnu developers have quite different rules, goals and priorities. The former are very serious about upward compatibility so are very conservative regarding changes that either might break existing scripts or might introduce standard compliance conflicts in the future. Sed is one of these commands having their interfaces classified as "Standard", i.e. committed to a standard defined outside Sun^H^H^HOracle. That mostly means if the standard doesn't evolve, there is little hope to see a request for enhancement implemented.

The "-i" option is missing from Solaris sed because it isn't defined by POSIX. Implementing it is too risky as it would create a conflict in the (quite unlikely) event that the standard decide one day that "-i" would serve a different purpose.

The way to split a line is specified by the POSIX standard that way:
Quote:
A line can be split by substituting a <newline> into it. The application shall escape the <newline> in the replacement by preceding it by a backslash.
Solaris sed is then supporting that way to do it. Implementing the "\n" extension would break existing scripts that expect that sequence to simply mean "n". It isn't going to happen.

In any case, there are several workarounds should you object to use raw newlines in the command.

The most common is simply to use Gnu sed instead of the default one. Gnu tools have been easily available for SunOS/Solaris for ages, even before Linux was born. Gnu sed is actually now part of Solaris distributions based on the OpenSolaris code base, usually under /usr/gnu/bin/sed.

Alternatively, you can use a different tool that is more friendly with "\n" like awk, perl or whatever, eg:
Code:
nawk '{gsub("><",">\n<"); print}' $filename > new

Last edited by jlliagre; 02-27-2010 at 09:56 PM.
 
Old 02-28-2010, 07:35 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Two good places for sed information are the unix grymoire and the sed faq. Both cover multiple versions of the program.

http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html

The sed faq even has a section devoted specifically to embedding newlines:

http://sed.sourceforge.net/sedfaq4.html#s4.1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed: insert a newline. Why does not it work? J_Szucs Linux - Software 5 06-14-2019 08:18 AM
Sed to read the file and replace and insert the pattern saurabhchokshi Programming 2 06-12-2009 01:15 PM
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 07:14 PM
sed insert newline character jhwilliams Linux - Software 6 06-08-2007 03:13 PM
Any tool to insert a newline each X characters in a file? neoAKiRAz Programming 5 05-09-2007 07:15 PM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 07:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration