?
Solved

newline using sed.

Posted on 2009-05-11
50
Medium Priority
?
693 Views
Last Modified: 2012-05-06
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


0
Comment
Question by:vishali_vishu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 20
  • 13
  • 9
  • +4
50 Comments
 

Expert Comment

by:sambasiva
ID: 24357936
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
0
 
LVL 5

Expert Comment

by:0ren
ID: 24357944
try this
sed 's/$/\n/g' /yourfile
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24357954
echo 'abc~~~def~~~ghi~~~jkl~~~mno~~pqr~~~' | sed "s/~~~/\n/g"
works just fine
0
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
LVL 5

Expert Comment

by:0ren
ID: 24357968
oops
sed 's/~~~/\n/g'
0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24358018
$ echo abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~ | sed s/"~~~"/\\n/g
abcndefnghinjklnmnonpqrn
$

i am not getting the correct result...
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24358032
Well, You not running right command. Try mine.
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358035
echo 'abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~' | sed 's/~~~/\n/g'
0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24358046

ravenpl: Not working...

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

0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24358056
What shell? It works here, try
echo 'abc~~~def~~~ghi~~~jkl~~~mno~~pqr~~~' | sed 's/~~~/\n/g'
0
 
LVL 14

Expert Comment

by:Deepak Kosaraju
ID: 24358076
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'
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358085
hey guys
isnt it what i sent ?
echo 'abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~' | sed 's/~~~/\n/g'
0
 
LVL 14

Expert Comment

by:Deepak Kosaraju
ID: 24358086
Sorry Experts said to use
echo "abc~~~def~~~ghi~~~jkl~~~mno~~pqr~~~" | sed 's/~~~/\n/g'

Open in new window

0
 
LVL 5

Expert Comment

by:0ren
ID: 24358098
hi kosarajudeepak
we are all experts
the name is on the right
0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24358111
kosarajudeepak:  Not working...
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358122
sed 's/~~~/\n/g' yourfile > newfile
0
 
LVL 14

Expert Comment

by:Deepak Kosaraju
ID: 24358129
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.
0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24358139
0ren: This will no way work.
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358144
yes it will
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358161
its exactly what you asked for
did you try it ?
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24358169
vishali_vishu: what shell You are using? It works for few people here, but You?
0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24358171
yes i tried that..... as per my search ...sed doesn't recognize \n \t characters.
0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24358178
ravenpl: korn shell....

0
 
LVL 5

Expert Comment

by:0ren
ID: 24358181
i dont think the shell matter
sed is not a shell command and the syntax here is basic.
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358206
works on ksh as well
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358213
vishali_vishu

did you try sed 's/~~~/\n/g' yourfile > newfile
which part didnt work ?
0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24358230
0ren: It is replacing ~~~ with n
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358238
then try
sed 's/~~~/\\n/g' yourfile > newfile
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24358242
Are You sure You surrounding the s/~~~/\n/g with quotes?
Anyway, try 's/~~~/\\n/g' then
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358259
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
0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24358260
0ren: no luck

~~~ is replaced with literal \n (not new line)
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358271
vishali_vishu

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

thats the line
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358274
please send a line from your input file
and the exact sed line you are running
0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24358282
#!/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
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358296
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
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358306
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'
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24358309
Strangely it haven't worked on my side, thus
echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | sed "s/~~~/\\n/g"
have
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358311
use one backslash
0
 

Expert Comment

by:sambasiva
ID: 24358332
Hello Vishali,

Can you use this

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

Author Comment

by:vishali_vishu
ID: 24358351
sambasiva: nope...

~~~ is getting replace with nothing.
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358367
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
0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24358377
Oren: i did the same...no luck.
0
 
LVL 5

Expert Comment

by:0ren
ID: 24358402
try it on bash
its look like something is really wrong with your system
i gave up

good night
0
 

Expert Comment

by:sambasiva
ID: 24358409

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
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24358458
I even verified the
#!/usr/bin/ksh
echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | sed 's/~~~/\
/g'
is working fine
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24358500
OK, so what's Your sed & OS versions?
You said it's linux, and linux uses gnu sed since always.
0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24358641
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.
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24360251
try awk


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

Open in new window

0
 
LVL 43

Accepted Solution

by:
ravenpl earned 2000 total points
ID: 24361516
Or perl - same syntax ;)
echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | perl -pe 's/~~~/\n/g'
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24365365
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
0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question