insert new line at found chars

Hey gang,

I have:
XXZZXXZZXX 123 asdaffggh dfghyrgr ertyhdhh XXZZXXZZXX 234 sdg XXZZXXZZXX 456 gfg fggfd

That is all on one line. Very simply put I want to do is something like:
sed s'/XXZZXXZZXX /\n/g'
or
tr 'XXZZXXZZXX ' '/n'

I have tried various things but can never get the desired results.

I want to end up with:
123 asdaffggh dfghyrgr ertyhdhh
234 sdg
456 gfg fggfd

The sed solution is basically correct. I'm guessing your sed doesn't understand \n to mean newline, so you need to use a literal newline or something.

sed 's/XXZZXXZZXX /
/g'

Yes, that's a literal newline after the second slash. You might need to put a backslash in front.

tr is out of the question; it only replaces individual characters.

I tried that earlier I get:

# /usr/bin/sed 's/XXZZXXZZXX /
> /g' /myfile
sed: command garbled: s/XXZZXXZZXX /
#

I am unclear what you meant by the backslash in front, but I just tried some things and got this to work:

# /usr/bin/sed 's/XXZZXXZZXX /\
> /g' /myfile

It now works, Thanks!

Change that, I am still having issues. I added a newline in vi when I was testing. The source file has no newline and it seems sed will not even read it. My output from previous post is empty with correct source file. I try and add a newlne so sed will read it with

# /usr/bin/sed '$a\
> ' /myfile

My output is empty again. I guess I need a command to add a new line before I can have sed work with it? Thoughts?

You mean that the input data file has no newline at the end, and your sed will not cope with such a file?

There are some tools which will add a missing newline on the last line as a side effect, but with any luck, none of them are installed on your system. Try awk for a start. You might as well do the substitution in awk too then.

awk '{ gsub ("XXZZXXZZXX ", "\n") }1' file

Yes

Awk did:

# /usr/bin/awk '{ gsub ("XXZZXXZZXX ", "\n") }1' /myfile
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
# 

This added a new line

echo >>myfile

Then I used the sed from above. I am more familiar with sed. Thanks again!

For what it's worth, the gsub function is an addition which was not present in the original Aho Weinberger Kernighan version of awk. Perhaps you could find nawk, mawk, gawk, or XPG4 awk on your system.