aspose file tools*
The moose likes Linux / UNIX and the fly likes Inserting newline character Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Inserting newline character" Watch "Inserting newline character" New topic
Author

Inserting newline character

Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
I need to insert newline character in my file (which is basically single long line caontaining many @ along with other data) I want to insert newline character after every "@" character that I see.

Can I sue sed to achive this?
How id newline character specified in it?

Thanks!
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923



http://home.arcor.de/hirnstrom/bewerbung
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Would be a nice solution, except that it doesn't work. Sed doesn't do regular expressions in the replacement part. Newish GNU versions of sed do actually support backslash-newline in the replacement part, like this:



Note how the first line ends in a backslash. The single quotes are important here.


[Jess in Action][AskingGoodQuestions]
Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
Great Ernest!
It worked perfect!! Thanks!

Thanks Stefan for you response. Actually even I was tryng on those lines but it does not work.

This forum is great! I had not tried it for anything other than Java. and looks like it is the only forum I will need for my doubts in everything!!

Gemini
Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
Hey Guys,

I have a new problem now!

Is there any restriction on size of yourfile?
I passed yourfile of size 142154
and got yourNewFile of size 111562 running the script.

I see that few of the rows in yourNewFile are trimmed at the bottom.

Actually, I tried file with much smaller size but same thing still kept happening (few lines from bttom are gone in yourNewFile.)

Please note that the adding of newline was done perfectly the way we asked for in yourNewFile for the lines which sre seen here.

So only problem is missing few lines from bottom.

Why this might be happening?

yourFile (my initial file) has only one line (but the size of the file is as huge as written above.

Please help

Thanks!!
[ September 30, 2004: Message edited by: Gemini Moses ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I use GNU sed 4.0.7 and don't have problems with truncated output.
And it works perfectly with , which I used for testing, since I don't have a file laying around, containing '@'.

Since sed operates on input, line by line, it might get confused by additional lines.
But I don't want to read the detailed specification.
[ September 30, 2004: Message edited by: Stefan Wagner ]
Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
Thanks Stefan!

Can you provide me some link where I should be reading the specification to find out details about this problem.

Thanks!
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

what sed do you use?
Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
I am using this unix --
HP-UX Release 11i

I dont know how to look for sed version?

sed --version
sed: illegal option -- -
Usage: sed [-n] [-e script] [-f source_file] [file...]

Thanks!
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I don't know whether GNU sed works for HP-UX, but I guess so.
But you got your working solution. Why bother...
Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
No, Actually solution you provided did not work for me.
So I am getting into further details....

Thanks!
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Yes - I see, the filesize problem.

I don't know whether newline-confusion is really the reason, or what else.
I would try following:
Search for an character, which will not occur normally in input.
Assuming '#'.
Translate with sed @->@#:


now I would compare sizes.

should be the difference between them.
Then I would 'translate'.

and compare sizes again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting newline character