1

In my file , i have to replace all the 'Volume' to 'Grain_'. This is a part of an automation script written in Matlab.

I tried to use the following:

 sed -i -e 's/Volume/GRAIN_/g' 3C1_N103_Gmsh2.inp

I created a string in matlab like this

str = ['sed -i -e ','''','s/Volume/GRAIN_/g','''',' ','3C1_N103_Gmsh2.inp'];

I tried with dos and system commands but none of them worked

dos(str)
system(str)

I am on Windows.

I got the following error :

'sed' is not recognized as an internal or external command, operable program or batch file. Any suggestions on how I do this ?

jarnosz
  • 243
  • 1
  • 18
Mechanician
  • 525
  • 1
  • 6
  • 20
  • Are you on Windows? – blackpen Nov 08 '16 at 00:10
  • Yes. I am on Windows – Mechanician Nov 08 '16 at 00:12
  • Windows doesn't come built with sed. Have you explicitly installed any packages/software (such as cygwin, MingGW or anything that gives you a sed command for windows)? In absence of those, you can't use sed. – blackpen Nov 08 '16 at 00:16
  • Yes. I have cygwin installed in my PC, Is there a work around ? – Mechanician Nov 08 '16 at 00:18
  • See [here](http://stackoverflow.com/questions/127318/is-there-any-sed-like-utility-for-cmd-exe). It has good discusson on alternatives/substitutes for linux/sed on windows. You could use powershell if this is a quick one time job that you want to get over with. – blackpen Nov 08 '16 at 00:19
  • If you have cygwin, you can use the sed from there (by specifying the exact PATH). – blackpen Nov 08 '16 at 00:19

1 Answers1

1

Since you say that you have cygwin, with little bit of effort, you can use sed from there.

C:> type input.txt
one two three

C:>c:\cygwin\bin\sed.exe -e 's/two/2/' input.txt
one 2 three

Try that one in matlab and let us know.

blackpen
  • 2,339
  • 13
  • 15
  • I tried this : dos('C:\cygwin64\bin\sed.exe -e ''s/Volume/GRAIN_/'' 3C1_N103_Gmsh2.inp') but it didnt work although i obtained no errors – Mechanician Nov 08 '16 at 01:04
  • Yes, it works. I even ran a c++ code using cygwin using dos (). This is running but i am unable to replace the word – Mechanician Nov 08 '16 at 01:21
  • The **dos()** command only seems to return status (0/1). The **s = evalc('system(''dir'')')** command is supposed to return output as per [here](http://blogs.mathworks.com/community/2010/05/17/calling-shell-commands-from-matlab/). Also **[status,cmdout] = system(command)** is also supposed to return output as mentioned [here](https://www.mathworks.com/help/matlab/ref/system.html). – blackpen Nov 08 '16 at 01:40
  • system([ 'C:\cygwin64\bin\sed.exe -i -e' ' ' '''' 's/Volume/GRAIN_/g' '''' ' ' '3C1_N103_Gmsh2.inp' ]); It worked ! – Mechanician Nov 09 '16 at 21:25
  • You chose "in place" editing option of "sed" to edit the 3C1_N103_Gmsh2.inp file; thus you don't need output. Out of curiosity, were you ever able to get output as "return" of "system" command? If so, you can use that code, in future, if you ever need output from the "system" command. Good luck. – blackpen Nov 09 '16 at 22:08