INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Unix sed carriage return

Unix sed carriage return

(OP)
Hello.
I am new to unix.
Using ksh.
I have this file in which I like to replace "CarRet" by a real carriage return ( or end of line or what ever it is).
ex :
Message1CarRetMessage2
should be
Message1
Message2
I tried a few things like :
sed 's\CarRet\/n\g' result : Message1/nMessage2
or 's/CarRet/'"$(printf '\015')"'/g' (not sure 15 is the correct number, but it didn't work anyways) result : Message1"$(printf '\015')"Message2
I tried sed 's/CarRet/\015/g'
error message : I know the reason is the way I wrote \015 but i do not know how to write it : I "use" 1 charcter for each digit, instead of having 1 character for the whole number - not sure how to explain this ...

Well, any idea is welcome !

RE: Unix sed carriage return

Hi

CODE

master # echo 'Message1CarRetMessage2' | sed 's/CarRet/\n/g'
Message1
Message2

Feherke.
http://rootshell.be/~feherke/

RE: Unix sed carriage return

(OP)
Actually I am using a sed file like so :
sed -n -f MySedFile.sed MyInputFile > MyOutputFile

and MySedFile is something like :

s/abc/def/g
s/CarRet/\n/g
s/X/x/g

where should I put the master # - and what is this ?

Thank you

RE: Unix sed carriage return

Hi

Is my prompt. I include that in the codes in my posts to show that the following command was executed from the command prompt and its output follows.

I would not use the -n option. That means to not produce output automatically, only when a p command is executed.

Feherke.
http://rootshell.be/~feherke/

RE: Unix sed carriage return

Hi

Maybe this examples explains better what I want to say about the outputing :

CODE

master # echo hello | sed 's/l/|_/g'
he|_|_o

master # echo hello | sed -n 's/l/|_/g'

master # echo hello | sed -n 's/l/|_/g;p'
he|_|_o

master # echo hello | sed 's/l/|_/g;p'
he|_|_o
he|_|_o

Feherke.
http://rootshell.be/~feherke/

RE: Unix sed carriage return

(OP)
oh - I feel really stupid now ....

I tried and the result :
Message1nMessage2 ...
(I took everytthing out of my sed file but s/CarRet/\n/g and did not use the -n option)

RE: Unix sed carriage return

(OP)

Well .. not sure how to see all this ...

SunOS5.8 (I am logging to the Unix server from my Windows-PC)
as for the sed version ???

RE: Unix sed carriage return

And what about this ?
nawk '{gsub(/CarRet/,"\n");print}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886

RE: Unix sed carriage return

Hi

Quote (cbms):

as for the sed version ???
Ask it.

CODE

master # sed --version
GNU sed version 4.1.2
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
As Unix programs applies only POSIX standards, maybe your sed needs -v parameter instead of --version.

Feherke.
http://rootshell.be/~feherke/

RE: Unix sed carriage return

(OP)

sed -v
sed -version
sed --v
sed --version
are all 'illegal option'

I will try PHV's solution also I am not to sure how ...
I let you know.
If it's not working, I will also try to work on the way my file is generated in the first place maybe there is something to do there ...

Many thanks for the help !
And I come back to you if I found a way ... or need more help !


RE: Unix sed carriage return

Sounds like you don't have the GNU version of sed.

I don't mind people who aren't what they seem. I just wish they'd make their mind up.

Alan Bennett.

RE: Unix sed carriage return

(OP)

PHV's solution did it !
I will try to found out more about nawk.
(I like to understand why something is working, or why not.)

RE: Unix sed carriage return

on non-GNU, plain vanilla unix sed, you can't use \n, but you can go like this:

CODE

# echo 'Message1CarRetMessage2' | sed 's/CarRet/\
> /g'
Message1
Message2
#

the # and > in red are the PS1 and PS2 prompts, you don't type them.

in a script it would be

CODE

echo 'Message1CarRetMessage2' | sed 's/CarRet/\
/g'

Just make sure the bakslash '\' is the last character on that first line - this makes sed continue reading its commands on the next line. Other UNIX commands and the shells behave the same.

HTH,

p5wizard

RE: Unix sed carriage return

Here's another for old-fashioned sed:

CODE

sed -e :a -e 'G;s/\(.*\)\(Car\)\(.*\)\(\n\)/\1\4\3/;t a' -e 's/.$//'

This is probably better

CODE

sed -e '/Car/{' -e 'G' -e :a -e 's/\(.*\)\(Car\)\(.*\)\(\n\)/\1\4\3\4/;t a' -e 's/.$//' -e '}'
or

CODE

sed '/Car/{
  G
  :a
  s/\(.*\)\(Car\)\(.*\)\(\n\)/\1\4\3\4/
  t a
  /.$//
}'

Cheers,
ND smile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close