• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inserting newline character

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what sed do you use?
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Actually solution you provided did not work for me.
So I am getting into further details....

Thanks!
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 can't take it! You are too smart for me! Here is the tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic