Solved

newline using sed.

Posted on 2009-05-11
50
686 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

770 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