Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOS to UNIX path substitution within a file

I have a file that contains this kind of paths:

C:\bad\foo.c
C:\good\foo.c
C:\good\bar\foo.c
C:\good\bar\[variable subdir count]\foo.c

And I would like to get the following file:

C:\bad\foo.c
C:/good/foo.c
C:/good/bar/foo.c
C:/good/bar/[variable subdir count]/foo.c

Note that the non matching path should not be modified.

I know how to do this with sed for a fixed number of subdir, but a variable number is giving me trouble. Actually, I would have to use many s/x/y/ expressions (as many as the max depth... not very elegant).

May be with awk, but this kind of magic is beyond my skills.

FYI, I need this trick to correct some gcov binary files on a cygwin platform.


I am dealing with binary files; therefore, I might have the following kind of data:

bindata\bindata%bindataC:\good\foo.c

which should be translated as:

bindata\bindata%bindataC:/good/foo.c

The first \ must not be translated, despite that it is on the same line.

However, I have just checked my .gcno files while editing this text and it looks like all the paths are flanked with zeros, so most of the answers below should fit.

like image 426
calandoa Avatar asked Dec 22 '22 13:12

calandoa


1 Answers

sed -e '/^C:\\good/ s/\\/\//g' input_file.txt
like image 81
Sean Bright Avatar answered Jan 07 '23 20:01

Sean Bright