Link to home
Start Free TrialLog in
Avatar of vishali_vishu
vishali_vishu

asked on

newline using sed.

i want to replace a specific string with newline character...using sed

example:

abc~~~def~~~ghi~~~jkl~~~mno~~pqr~~~

o/p:
abc
def
ghi
jkl
mno
pqr


i tried like this...but now working...

sed 's/~~~/\
/g' text_file > text_file.dat


Avatar of sambasiva
sambasiva

You have to use like this

[samba@samba-lx1 ~]$ echo abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~ | sed s/"~~~"/\\n/g
abc
def
ghi
jkl
mno
pqr

[samba@samba-lx1 ~]$

Also, please note that in your original you missed one ~ before p

Regards
Samba
try this
sed 's/$/\n/g' /yourfile
echo 'abc~~~def~~~ghi~~~jkl~~~mno~~pqr~~~' | sed "s/~~~/\n/g"
works just fine
oops
sed 's/~~~/\n/g'
Avatar of vishali_vishu

ASKER

$ echo abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~ | sed s/"~~~"/\\n/g
abcndefnghinjklnmnonpqrn
$

i am not getting the correct result...
Well, You not running right command. Try mine.
echo 'abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~' | sed 's/~~~/\n/g'

ravenpl: Not working...

$ echo abc~~~def~~~ghi~~~jkl~~~mno~~pqr~~~ | sed "s/~~~/\n/g"
abcndefnghinjklnmno~~pqrn
$

What shell? It works here, try
echo 'abc~~~def~~~ghi~~~jkl~~~mno~~pqr~~~' | sed 's/~~~/\n/g'
well don't use "" in sed
U are using:
$ echo abc~~~def~~~ghi~~~jkl~~~mno~~pqr~~~ | sed "s/~~~/\n/g"

But Experts said to u
$ echo abc~~~def~~~ghi~~~jkl~~~mno~~pqr~~~ | sed 's/~~~/\n/g'
hey guys
isnt it what i sent ?
echo 'abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~' | sed 's/~~~/\n/g'
Sorry Experts said to use
echo "abc~~~def~~~ghi~~~jkl~~~mno~~pqr~~~" | sed 's/~~~/\n/g'

Open in new window

hi kosarajudeepak
we are all experts
the name is on the right
kosarajudeepak:  Not working...
sed 's/~~~/\n/g' yourfile > newfile
Oren: Experts I mean every one who replied before I started commenting to this question, all of you had suggested the correct way to add newline character as per author request, but author was bit confused with " vs ' when using sed. so I was trying to correct the typo.
0ren: This will no way work.
yes it will
its exactly what you asked for
did you try it ?
vishali_vishu: what shell You are using? It works for few people here, but You?
yes i tried that..... as per my search ...sed doesn't recognize \n \t characters.
ravenpl: korn shell....

i dont think the shell matter
sed is not a shell command and the syntax here is basic.
works on ksh as well
vishali_vishu

did you try sed 's/~~~/\n/g' yourfile > newfile
which part didnt work ?
0ren: It is replacing ~~~ with n
then try
sed 's/~~~/\\n/g' yourfile > newfile
Are You sure You surrounding the s/~~~/\n/g with quotes?
Anyway, try 's/~~~/\\n/g' then
did you copy the line exactly ?
i tried it on ksh,bash and it worked
please send a line from your input file
and the exact sed line you are running
0ren: no luck

~~~ is replaced with literal \n (not new line)
vishali_vishu

sed 's/~~~/\n/g' yourfile > newfile

thats the line
please send a line from your input file
and the exact sed line you are running
#!/usr/bin/ksh
echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | sed 's/~~~/\\n/g'

------ i tried this and the o/p is :abc\ndef\nghi\njkl\nmno\npqr\n
oren@orendesktop ~ > cat test
#!/usr/bin/ksh
echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | sed 's/~~~/\n/g'
oren@orendesktop ~ > ./test
abc
def
ghi
jkl
mno
pqr
here is the script
unless you have a different ksh it should work for you

echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | sed 's/~~~/\n/g'
Strangely it haven't worked on my side, thus
echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | sed "s/~~~/\\n/g"
have
use one backslash
Hello Vishali,

Can you use this

echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | sed s/"~~~"/\n/g
sambasiva: nope...

~~~ is getting replace with nothing.
Vishali

create a small script


#!/usr/bin/ksh
echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | sed 's/~~~/\n/g'

chmod it to 755
and run it

tell me if it didnt work
Oren: i did the same...no luck.
try it on bash
its look like something is really wrong with your system
i gave up

good night

Not sure how one of \ is missing. This one worked for me

echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | sed s/"~~~"/\\n/g

Can you give us o/p of            set |grep SH to see what is your shell
I even verified the
#!/usr/bin/ksh
echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | sed 's/~~~/\
/g'
is working fine
OK, so what's Your sed & OS versions?
You said it's linux, and linux uses gnu sed since always.
when i do at the command line i am getting the correct o/p

cat $1.temp | sed 's/~~~/\
/g'


but when i include this in the script file...not working.
try awk


# echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | awk 'BEGIN{FS="~~~";OFS="\n"}{$1=$1}1'

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kent Olsen
Hi vishali_vishu,

All sed's are not created equally.  :(

In linux, the simple \n usually works.  

In some versions you can use the hex value (\0A) in the string.

And in others, you can you the *y* sub-command, though it's not very flexible.

echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | sed 's/~~~/~/; y/~/\n'

The down-side is that if the text contains the ~ character, the *y* sub-command will change it, too.


Good Luck,
Kent