vishali_vishu
asked on
newline using sed.
i want to replace a specific string with newline character...using sed
example:
abc~~~def~~~ghi~~~jkl~~~mn o~~pqr~~~
o/p:
abc
def
ghi
jkl
mno
pqr
i tried like this...but now working...
sed 's/~~~/\
/g' text_file > text_file.dat
example:
abc~~~def~~~ghi~~~jkl~~~mn
o/p:
abc
def
ghi
jkl
mno
pqr
i tried like this...but now working...
sed 's/~~~/\
/g' text_file > text_file.dat
try this
sed 's/$/\n/g' /yourfile
sed 's/$/\n/g' /yourfile
echo 'abc~~~def~~~ghi~~~jkl~~~m no~~pqr~~~ ' | sed "s/~~~/\n/g"
works just fine
works just fine
oops
sed 's/~~~/\n/g'
sed 's/~~~/\n/g'
ASKER
$ echo abc~~~def~~~ghi~~~jkl~~~mn o~~~pqr~~~ | sed s/"~~~"/\\n/g
abcndefnghinjklnmnonpqrn
$
i am not getting the correct result...
abcndefnghinjklnmnonpqrn
$
i am not getting the correct result...
Well, You not running right command. Try mine.
echo 'abc~~~def~~~ghi~~~jkl~~~m no~~~pqr~~ ~' | sed 's/~~~/\n/g'
ASKER
ravenpl: Not working...
$ echo abc~~~def~~~ghi~~~jkl~~~mn
abcndefnghinjklnmno~~pqrn
$
What shell? It works here, try
echo 'abc~~~def~~~ghi~~~jkl~~~m no~~pqr~~~ ' | sed 's/~~~/\n/g'
echo 'abc~~~def~~~ghi~~~jkl~~~m
well don't use "" in sed
U are using:
$ echo abc~~~def~~~ghi~~~jkl~~~mn o~~pqr~~~ | sed "s/~~~/\n/g"
But Experts said to u
$ echo abc~~~def~~~ghi~~~jkl~~~mn o~~pqr~~~ | sed 's/~~~/\n/g'
U are using:
$ echo abc~~~def~~~ghi~~~jkl~~~mn
But Experts said to u
$ echo abc~~~def~~~ghi~~~jkl~~~mn
hey guys
isnt it what i sent ?
echo 'abc~~~def~~~ghi~~~jkl~~~m no~~~pqr~~ ~' | sed 's/~~~/\n/g'
isnt it what i sent ?
echo 'abc~~~def~~~ghi~~~jkl~~~m
Sorry Experts said to use
echo "abc~~~def~~~ghi~~~jkl~~~mno~~pqr~~~" | sed 's/~~~/\n/g'
hi kosarajudeepak
we are all experts
the name is on the right
we are all experts
the name is on the right
ASKER
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.
ASKER
0ren: This will no way work.
yes it will
its exactly what you asked for
did you try it ?
did you try it ?
vishali_vishu: what shell You are using? It works for few people here, but You?
ASKER
yes i tried that..... as per my search ...sed doesn't recognize \n \t characters.
ASKER
ravenpl: korn shell....
i dont think the shell matter
sed is not a shell command and the syntax here is basic.
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 ?
did you try sed 's/~~~/\n/g' yourfile > newfile
which part didnt work ?
ASKER
0ren: It is replacing ~~~ with n
then try
sed 's/~~~/\\n/g' yourfile > newfile
sed 's/~~~/\\n/g' yourfile > newfile
Are You sure You surrounding the s/~~~/\n/g with quotes?
Anyway, try 's/~~~/\\n/g' then
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
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
ASKER
0ren: no luck
~~~ is replaced with literal \n (not new line)
~~~ is replaced with literal \n (not new line)
vishali_vishu
sed 's/~~~/\n/g' yourfile > newfile
thats the line
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
and the exact sed line you are running
ASKER
#!/usr/bin/ksh
echo "abc~~~def~~~ghi~~~jkl~~~m no~~~pqr~~ ~" | sed 's/~~~/\\n/g'
------ i tried this and the o/p is :abc\ndef\nghi\njkl\nmno\n pqr\n
echo "abc~~~def~~~ghi~~~jkl~~~m
------ i tried this and the o/p is :abc\ndef\nghi\njkl\nmno\n
oren@orendesktop ~ > cat test
#!/usr/bin/ksh
echo "abc~~~def~~~ghi~~~jkl~~~m no~~~pqr~~ ~" | sed 's/~~~/\n/g'
oren@orendesktop ~ > ./test
abc
def
ghi
jkl
mno
pqr
#!/usr/bin/ksh
echo "abc~~~def~~~ghi~~~jkl~~~m
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~~~m no~~~pqr~~ ~" | sed 's/~~~/\n/g'
unless you have a different ksh it should work for you
echo "abc~~~def~~~ghi~~~jkl~~~m
Strangely it haven't worked on my side, thus
echo "abc~~~def~~~ghi~~~jkl~~~m no~~~pqr~~ ~" | sed "s/~~~/\\n/g"
have
echo "abc~~~def~~~ghi~~~jkl~~~m
have
use one backslash
Hello Vishali,
Can you use this
echo "abc~~~def~~~ghi~~~jkl~~~m no~~~pqr~~ ~" | sed s/"~~~"/\n/g
Can you use this
echo "abc~~~def~~~ghi~~~jkl~~~m
ASKER
sambasiva: nope...
~~~ is getting replace with nothing.
~~~ is getting replace with nothing.
Vishali
create a small script
#!/usr/bin/ksh
echo "abc~~~def~~~ghi~~~jkl~~~m no~~~pqr~~ ~" | sed 's/~~~/\n/g'
chmod it to 755
and run it
tell me if it didnt work
create a small script
#!/usr/bin/ksh
echo "abc~~~def~~~ghi~~~jkl~~~m
chmod it to 755
and run it
tell me if it didnt work
ASKER
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
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~~~m
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~~~m no~~~pqr~~ ~" | sed 's/~~~/\
/g'
is working fine
#!/usr/bin/ksh
echo "abc~~~def~~~ghi~~~jkl~~~m
/g'
is working fine
OK, so what's Your sed & OS versions?
You said it's linux, and linux uses gnu sed since always.
You said it's linux, and linux uses gnu sed since always.
ASKER
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.
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'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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~~~m no~~~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
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~~~m
The down-side is that if the text contains the ~ character, the *y* sub-command will change it, too.
Good Luck,
Kent
[samba@samba-lx1 ~]$ echo abc~~~def~~~ghi~~~jkl~~~mn
abc
def
ghi
jkl
mno
pqr
[samba@samba-lx1 ~]$
Also, please note that in your original you missed one ~ before p
Regards
Samba