Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix sed carriage return 2

Status
Not open for further replies.

cbsm

Programmer
Oct 3, 2002
229
FR
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 !
 
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
 
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 [tt]p[/tt] command is executed.

Feherke.
 
Hi

Maybe this examples explains better what I want to say about the outputing :
Code:
[blue]master #[/blue] echo hello | sed 's/l/|_/g'
he|_|_o

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

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

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

Feherke.
 
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)
 

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 ???
 
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
 
Hi

cbms said:
as for the sed version ???
Ask it.
Code:
[blue]master #[/blue] 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 [tt]sed[/tt] needs -v parameter instead of --version.

Feherke.
 

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 !


 
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.
 

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.)
 
on non-GNU, plain vanilla unix sed, you can't use \n, but you can go like this:
Code:
[COLOR=red]# [/color]echo 'Message1CarRetMessage2' | sed 's/CarRet/\
[COLOR=red]> [/color]/g'
Message1
Message2
[COLOR=red]# [/color]

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
 
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]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top