?
Solved

How to replace a forward slash with 2 backslashes ?

Posted on 2003-02-20
11
Medium Priority
?
3,243 Views
Last Modified: 2013-12-26
I have a Makefile that compiles Java files.
Because Sun JDK 1.1.8 on Windows requires backward slashes
in the file name, I need to replace each forward slash
in the filename with 2 backslashes.

Here is the rule I am using to compile:

bin/%.class: src/%.java  
     @mkdir -p $(dir $@)
     $(JAVAC) -classpath "src;$(CLASSPATH)" -d \
              $(dir $@) src/$*.java


So, what I want is in the last line to change src/$*.java
so that it contains doubled backslashes instead of forward slashes.

0
Comment
Question by:seva
[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
11 Comments
 
LVL 1

Author Comment

by:seva
ID: 7990981
Actually, I found one solution involving "sed" and
"xargs", but still would be great to learn if it is possible just with Make functions.
0
 
LVL 1

Author Comment

by:seva
ID: 7991086
Actually, I found one solution involving "sed" and
"xargs", but still would be great to learn if it is possible just with Make functions.
0
 
LVL 2

Accepted Solution

by:
ellesd earned 150 total points
ID: 7991324
If this is your only makefile, which it seems like it is with the use of your macros, just change it.  If not, then sed is probably your best way.  Are you wanting to have a forward slash sometimes and double backslashes sometimes, or double backslashes all the time.  If you always want double backslashes, again edit the file or use sed.  If you want to alternate, try using macro definitions; they can be declared on the command line.  Other than that, I'm not quite sure what you are referring to by make functions.  Please give some additional description.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 150 total points
ID: 7993357
DOS-SEP         = \\\\
UNIX-SEP        = /
SEP             = $($(P)-SEP)
PATH=           = path$(SEP)file

# keep in mind that you probably need
simple-SEP      = \\
# when $(simple-SEP) is to be substituted by Makefile only, and not (as in your example) by an additional shell command in the target
0
 
LVL 1

Author Comment

by:seva
ID: 8030453
Sorry for delay.

DOS-SEP         = \\\\
UNIX-SEP        = /
SEP             = $($(P)-SEP)
PATH=           = path$(SEP)file

didn't work for me, probably because I am running make inside Cygwin.
Just curious, are SEP and PATH make special variables?
How is PATH=     = path$(SEP)file
supposed to work?
Thanks.

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 8037380
example:
   make P=UNIX any-target
evaluates SEP to / and PATH to path/file
   make P=DOS any-target
evaluates SEP to \\ and PATH to path\\file

BTW, keep in mind that the macro-name PATH (in my example) might be a bad choice,'cause it overwrides the shell's PATH variable
0
 
LVL 4

Expert Comment

by:Vinit Kain
ID: 8041940
try this

cat filename|sed "s/\//\\\\\\\\/g"
0
 
LVL 1

Author Comment

by:seva
ID: 8060992
OK, I'll try to formulate my problem more precisely.

I have a rule in my Makefile:

bin/%.class: src/%.java
     javac -classpath \
"src;bin;d:/opt/jdk1.1.8/lib/classes.zip" \
       -d bin src/$*.java

and the following target:

mytest: bin/Test.class

If I run make with JDK1.2 or later in my PATH,
it works fine.
However, JDK 1.1 doesn't like forward slashes in
the rule's command.

I can't just replace forward slash with a backslash
in the command because the JAVA file name (which substitutes $*) contains
potentially many directories in it, like

com/company/utility/model/SomeClass.java.

At the same time, I can't replace other forward slashes
in the rule with backslashes (I'm talking about
  bin/%.class: src/%.java )
because then make won't find any files because
make doesn't like backslashes.

The problem that I am trying to solve is how to keep
forward slashes in the rule pattern, but replace
forward slashes with double back slashes
in the command.

I've managed this with the following:

bin/%.class: src/%.java
     echo src/$*.java | sed 's/\//\\\\/g' | xargs \  
       javac  -classpath "src;classes.zip" -d bin

Another way would be to use cygwin special command
"cygpath -w <filename>"

I guess at this point I am purely interested
if someone knows of any other way to do that,
preferably with just "make" facilities.

Thanks everyone for your input!




0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 8061219
STOP
where do you think is your problem now?

> If I run make with JDK1.2 or later in my PATH, it works fine.
and
> However, JDK 1.1 doesn't like forward slashes in the rule's command

what has JDK x.y to do with your make (except the arguments passed to J*)

> I can't replace other forward slashes in the rule with backslashes ..
true, you cannot on UNIX (probably with some M$ make)


> .. but replace forward slashes with double back slashes in the command
see my very first suggestion

Do you want to run this Makefilewith different JDK versions?
If so, and JDK is the culprit (not make!), then I's suggest to Makefiles
0
 
LVL 18

Expert Comment

by:liddler
ID: 9826992
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Delete and refund

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

liddler
EE Cleanup Volunteer
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9827088
ahoffmann gave an answer
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

800 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