Need to insert a newline using sed on all records in a file
Solaris / OpenSolarisThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
@ 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.
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).
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.