Operating System - HP-UX
1826214 Members
2639 Online
109691 Solutions
New Discussion

Re: Breaking up is hard to do

 
SOLVED
Go to solution
Maureen Gunkel
Trusted Contributor

Breaking up is hard to do

I'm trying to take a file that I ftp'd from an NT server, and insert a newline (or carriage return) character after every instance of '>'. My problem is sed is interpreting \012 literally, and my file ends up looking like the following:
\012\012
instead of:


Can anyone help? Thanks in advance
No matter where you go, there you are.
6 REPLIES 6
Rick Garland
Honored Contributor

Re: Breaking up is hard to do

I don't believe that \012 is what you want to use. I think it is \015

You may also want to look at the tr command.
Stefan Farrelly
Honored Contributor

Re: Breaking up is hard to do


Use tr. Eg, input file (called tt) looks like;
aaa>bbb>ccc>

tr "[>]" "[\012]" < tt

gives output;

aaa
bbb
ccc
Im from Palmerston North, New Zealand, but somehow ended up in London...
Maureen Gunkel
Trusted Contributor

Re: Breaking up is hard to do

Stefan:
That gets me a lot closer, but I need to retain the '>' character. For example, aaa>bbbb>ccc> needs to be turned into:
aaa>
bbbb>
ccc>
Any ideas?
No matter where you go, there you are.
Tony Vilardi
Advisor
Solution

Re: Breaking up is hard to do

Here is how I was able to get the desired output:

sed 's/>/>Z/g' test | tr ">Z" ">\012]"

I chose a character to put after the '>' symbol and then did a grep on the file to make sure that that sequence didn't occur in the file.
Bruce Regittko_1
Esteemed Contributor

Re: Breaking up is hard to do

Hi,

...Yet another way to do it. Enter this exactly as shown below, that is, span the command over two lines.

sed 's/>/
/g' file

Bruce
www.stratech.com/training
curt larson
Frequent Advisor

Re: Breaking up is hard to do

maybe a little easier is:

sed 's/>/>\\^J/g' file

don't how the backslashes,etc will come out,
but that is a backslash cnt-J

this will probably leave you with a blank line at the end
of your output. if you want that removed:

| sed '/^$/d' to delete empty lines.

another way is:

cat file | awk -F\> '{ for ( i=1;i<=NF;i++) print $i ">";}'

you'll end up with a blank line with that method also.

both sed and awk are line based utilities, meaning if your lines are longer then a few thousand characters
they will give you a "line too long" error.

in which case you probably have to use "tr" to break up you line into small segments then add the > back to the end of the lines

cat file | tr ">" "\012" | sed -e 's/\(.*\)/\1>/' -e '/^$/d'
nobody else has this problem