[comp.unix.questions] Changing back slashes to forward slashes

ramon@skye.mit.edu (Ramon F Herrera) (08/17/90)

I have a question for those {sed, grep, awk, tr} wizards out there.
I'd like to change all the occurrences in a file of a line like this:

#include "dira\dirb\incl.h"

to 

#include "dira/dirb/incl.h"

but only for the lines that begin with "#include".

As you may have guessed, I am porting a PC-DOS program to Unix.

thanks,

Ramon


--
Ramon F. Herrera
Research Laboratory of Electronics
Massachusetts Institute of Technology
ramon@iona.mit.edu

rouben@math13.math.umbc.edu (Rouben Rostamian) (08/17/90)

In article <1990Aug16.194644.14376@athena.mit.edu> ramon@skye.mit.edu (Ramon F Herrera) writes:
|
|I have a question for those {sed, grep, awk, tr} wizards out there.
|I'd like to change all the occurrences in a file of a line like this:
|
|#include "dira\dirb\incl.h"
|
|to 
|
|#include "dira/dirb/incl.h"
|
|but only for the lines that begin with "#include".
|

sed <infile -e '/\#include/s/\\/\//g' >outfile

--

Rouben Rostamian                               Telephone: (301) 455-2458
Department of Mathematics and Statistics       e-mail:
University of Maryland Baltimore County        rostamian@umbc.bitnet
Baltimore, MD 21228,  U.S.A.                   rostamian@umbc3.umbc.edu

merlyn@iwarp.intel.com (Randal Schwartz) (08/17/90)

In article <1990Aug16.194644.14376@athena.mit.edu>, ramon@skye (Ramon F Herrera) writes:
| 
| I have a question for those {sed, grep, awk, tr} wizards out there.
| I'd like to change all the occurrences in a file of a line like this:
| 
| #include "dira\dirb\incl.h"
| 
| to 
| 
| #include "dira/dirb/incl.h"
| 
| but only for the lines that begin with "#include".

perl -pi~ -e 's#\\#/#g if /^#include/' file1.c file2.c file3.c ...

Pretty darn simple with Perl.  Even leaves you a backup.

Just another Perl hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

lfk@athena.mit.edu (Lee F Kolakowski) (08/17/90)

On Thu, 16 Aug 90 19:46:44 GMT,
ramon@skye.mit.edu (Ramon F Herrera) said:


> I have a question for those {sed, grep, awk, tr} wizards out there.
> I'd like to change all the occurrences in a file of a line like this:

> #include "dira\dirb\incl.h"

> to 

> #include "dira/dirb/incl.h"

> but only for the lines that begin with "#include".


Try:
	awk '
	{
		if ($0 ~ /^#include/)
			 gsub(/\\/, "/")
		print
	}' filename




Frank Kolakowski 

======================================================================
|lfk@athena.mit.edu                     ||      Lee F. Kolakowski    |
|lfk@eastman2.mit.edu                   ||	M.I.T.		     |
|kolakowski@wccf.mit.edu                ||	Dept of Chemistry    |
|lfk@mbio.med.upenn.edu		        ||	Room 18-506	     |
|lfk@hx.lcs.mit.edu                     ||	77 Massachusetts Ave.|
|AT&T:  1-617-253-1866                  ||	Cambridge, MA 02139  |
|--------------------------------------------------------------------|
|                         #include <woes.h>         		     |
|		           One-Liner Here!                           |
======================================================================


--

Frank Kolakowski 

======================================================================
|lfk@athena.mit.edu                     ||      Lee F. Kolakowski    |
|lfk@eastman2.mit.edu                   ||	M.I.T.		     |
|kolakowski@wccf.mit.edu                ||	Dept of Chemistry    |
|lfk@mbio.med.upenn.edu		        ||	Room 18-506	     |
|lfk@hx.lcs.mit.edu                     ||	77 Massachusetts Ave.|
|AT&T:  1-617-253-1866                  ||	Cambridge, MA 02139  |
|--------------------------------------------------------------------|
|                         #include <woes.h>         		     |
|		           One-Liner Here!                           |
======================================================================

ronald@atcmp.nl (Ronald Pikkert) (08/17/90)

From article <1990Aug16.194644.14376@athena.mit.edu>, by ramon@skye.mit.edu (Ramon F Herrera):
<> 
<> I have a question for those {sed, grep, awk, tr} wizards out there.
<> #include "dira\dirb\incl.h"
<> to 
<> #include "dira/dirb/incl.h"
<> 

For a real edit-job use a real editor :-)

script 
------
ed - $1 <<@
g/^#include/s/\\\/\\//g
w
@

Invoke this script as: "script prog.c" and it will do the job.
You can run it without having to worry about creating 
temporary files, wich you would need with any of the filters
you mentioned.

Have fun,

-
Ronald Pikkert                 E-mail: ronald@atcmp.nl
@ AT Computing b.v.            Tel:    080 - 566880
Toernooiveld
6525 ED  Nijmegen

art@pilikia.pegasus.com (Art Neilson) (08/17/90)

In article <1990Aug16.194644.14376@athena.mit.edu> ramon@skye.mit.edu (Ramon F Herrera) writes:
>
>I have a question for those {sed, grep, awk, tr} wizards out there.
>I'd like to change all the occurrences in a file of a line like this:
>
>#include "dira\dirb\incl.h"
>
>to 
>
>#include "dira/dirb/incl.h"
Gosh, how about this ...
	sed '/^#include/s/\\/\//g' infile > outfile
-- 
Arthur W. Neilson III		| ARPA: art@pilikia.pegasus.com
Bank of Hawaii Tech Support	| UUCP: uunet!ucsd!nosc!pegasus!pilikia!art