Solved

newline using sed.

Posted on 2009-05-11
50
683 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 1

Author Comment

by:vishali_vishu
ID: 24358470
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 500 total points
ID: 24361516
Or perl - same syntax ;)
echo "abc~~~def~~~ghi~~~jkl~~~mno~~~pqr~~~" | perl -pe 's/~~~/\n/g'
0
 
LVL 45

Expert Comment

by:Kdo
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now