Operating System - HP-UX
1834101 Members
2869 Online
110063 Solutions
New Discussion

need script to replace "/" with "\"

 
Stephen Kennedy_2
Occasional Contributor

need script to replace "/" with "\"

All

I need to do the following I need a script that will change /aaaa/bbbb/cccc to \aaaa\bbbb\ccccc I have tried sed 's///\/g' and awk FS = / OFS = \ any suggestions, I need to keep away from perl so is there an easy way to perform this.....


21 REPLIES 21
Pete Randall
Outstanding Contributor

Re: need script to replace "/" with "\"

Your sed solution would most likely be the easiest, but I think you were missing one "/". Try sed 's////\/g'.


Pete

Pete
Roberto Polli
Trusted Contributor

Re: need script to replace "/" with "\"

Well your sed script miss the \ protection!!

sed 's/\//\\/g'

will work for you. If you use gnu-sed (the linux one) you can do it clearly using any character instead of \

eg sed 's|\/|\\|g'


Peace, R.
H.Merijn Brand (procura
Honored Contributor

Re: need script to replace "/" with "\"

# man tr


# tr '\\' /
Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
G. Vrijhoeven
Honored Contributor

Re: need script to replace "/" with "\"

Hi,

echo /aaaa/bbbb/cccc | sed 's%\/%\\%g'

HTH,

Gideon
john korterman
Honored Contributor

Re: need script to replace "/" with "\"

Hi,

# STRING="/aaaa/bbbb/cccc"
# echo "$STRING" | tr "[\057]" "[\134]"
\aaaa\bbbb\cccc

regards,
John K.
it would be nice if you always got a second chance
H.Merijn Brand (procura
Honored Contributor

Re: need script to replace "/" with "\"

Sorry backwards is evenly easy:

# tr / '\\'
See that perl is not always the best tool?

Enjoy, Have FUN! H.Merijn [ using the right tool for the right problem ]
Enjoy, Have FUN! H.Merijn
KapilRaj
Honored Contributor

Re: need script to replace "/" with "\"

cat filename |sed -e "s;/;\;g" works in Korh SHell

Regds,

Kaps
Nothing is impossible
H.Merijn Brand (procura
Honored Contributor

Re: need script to replace "/" with "\"

Kapil: no it doesn't:

a5:/u/usr/merijn 101 > ksh
$ echo /aaa/bbb/ccc | sed -e "s;/;\;g"
sed: -e expression #1, char 7: Unterminated `s' command
$

Enjoy, Have FUN! H.Merijn [ who thinks sed is not the right tool here ]
Enjoy, Have FUN! H.Merijn
G. Vrijhoeven
Honored Contributor

Re: need script to replace "/" with "\"

Merijn,

prc60b03:/root # timex echo aa/ss | sed 's%\/%\\%g'

real 0.00
user 0.00
sys 0.00

aa\ss
prc60b03:/root # timex echo aa/ss | tr / '\\'

real 0.01
user 0.00
sys 0.00

aa\ss

Who is just curious about why exactly tr is the better tool over sed.

Gideon ( 0 points please )

G. Vrijhoeven
Honored Contributor

Re: need script to replace "/" with "\"

Merijn,

Just did the same with the a large file on a slow server and tr was faster. So i must admit your are right.

Gideon

H.Merijn Brand (procura
Honored Contributor

Re: need script to replace "/" with "\"

Humbug! Cache is your enemy here. Watch:

lt09:/home/merijn 182 > > test.txt
lt09:/home/merijn 183 > repeat 5000 echo /what/if/there/are/more/lines >> test.txt
lt09:/home/merijn 184 > time tr / '\\' /dev/null
0.001u 0.000s 0:00.00 0.0% 0+0k 0+0io 127pf+0w
lt09:/home/merijn 185 > time tr / '\\' < test.txt > /dev/null
0.000u 0.002s 0:00.00 0.0% 0+0k 0+0io 127pf+0w
lt09:/home/merijn 186 > time sed 's:/:\\:g' /dev/null
0.059u 0.001s 0:00.05 100.0% 0+0k 0+0io 142pf+0w
lt09:/home/merijn 187 > time sed 's:/:\\:g' < test.txt > /dev/null
0.060u 0.001s 0:00.06 100.0% 0+0k 0+0io 142pf+0w
lt09:/home/merijn 188 >

Plainly shows that tr is faster

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Elmar P. Kolkman
Honored Contributor

Re: need script to replace "/" with "\"

Other solution, and quite as fast as tr on large input:
awk '{gsub("/","\\");print}'

For the performance freaks, do it like this:
timex awk '{gsub("/","\\");print}' /tmp/tmpf

This gives better info as in Gideon's post, because in those you're only timing the echo or cat, not the command doing the substitions.
Every problem has at least one solution. Only some solutions are harder to find.
Nicolas Dumeige
Esteemed Contributor

Re: need script to replace "/" with "\"

(
To keep the substitute code clear and readable, use another symbol :
s@pattern@substitute@
rather than
s/pattern/substitute/
)

My 2 cents
All different, all Unix
KapilRaj
Honored Contributor

Re: need script to replace "/" with "\"

Merijn,

It works for me in AIX , just tried the same in BASH and KSH of Linux it gave me the same error !

That means there are diffrences in every Vendor's implementation of shells ?

Regds,

Kaps
Nothing is impossible
H.Merijn Brand (procura
Honored Contributor

Re: need script to replace "/" with "\"

Kapil, you are serious with that question? :) :)

Of course there are. IBM think they are the only truth, and so do M$.

Standards = multiple versions of the truth

I'm not surprised at all. Try all machines in HP/Compaq's test park. And Solaris, Primos, FreeBSD, OS/2, ...
I et they all act different

Enjoy, Have FUN! H.Merijn [ whose job in the Perl community is to know about these differences ]
Enjoy, Have FUN! H.Merijn
KapilRaj
Honored Contributor

Re: need script to replace "/" with "\"

Seriously ? Yes. I thought Korn shell is korn shell every where and so on with C

So that means if I have to migrate a script which I have on AIX to HPUX I would need to break by head looking at each shell command !!!

Thanks alot.

Regds,

Kaps
Nothing is impossible
Elmar P. Kolkman
Honored Contributor

Re: need script to replace "/" with "\"

And to make matters worse, take a look at tools like awk, which are not even compatible across OS's (use nawk on Solaris, to make it compatible with awk on HP-UX, for instance).
Every problem has at least one solution. Only some solutions are harder to find.
Nicolas Dumeige
Esteemed Contributor

Re: need script to replace "/" with "\"

Kapil,

To get the ksh to print his revision number, try [ESC] then [CTRL]V .

Version M-12/28/93d

Cheers,

Nicolas
All different, all Unix
Nicolas Dumeige
Esteemed Contributor

Re: need script to replace "/" with "\"

On Linux or other free unix like implementation wich use pdksh (public domain ksh), there is one very odd difference.

If you type :
echo value | read var
var will be empty, no value has been assigned to it.

All different, all Unix
Keith Bevan_1
Trusted Contributor

Re: need script to replace "/" with "\"

Stephen,

Problems can arise with the '/' as sed will treat this as a seperator in your sed statement.

Change the '/' to a ',' :-

sed ,/,\,g


Keith

You are either part of the solution or part of the problem
Sathish C
Frequent Advisor

Re: need script to replace "/" with "\"

Hi
The easiest is via the most powerfull editior vi ,

vi the file you wanna change
in the Esc mode

:%s/\/..../\\..../g

....=> can be the length of the string you wanna replace .
Some cause happiness wherever they go; others, whenever they go