Solved

newline using sed.

Posted on 2009-05-11
50
691 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

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…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

729 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