0

I want to run a sed command : sed command to replace if( with if (

My command looks like this:

sed 's/if(/if ( /g' file1.c >file1.c

where file1.c contains the string that needs to be replaced.

Problem is file contents gets cleared and no changes are reflected.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
PKSA
  • 113
  • 1
  • 7
  • 2
    You cannot redirect to the same file: it gets truncated first and then evaluated. Instead, use `sed -i '...' file` to do in-place replacement, or just redirect to another file. – fedorqui May 14 '14 at 11:48

2 Answers2

0

Use this:

sed -re 's/if(/if ( /g' 
Undo
  • 25,519
  • 37
  • 106
  • 129
angel
  • 322
  • 1
  • 7
0

You can try two different approaches.

If you want to update existing file:

sed -i 's/if(/if ( /g' file1.c  

If you want to store the modified file in another file:

sed 's/if(/if ( /g' file1.c > file2.c  
fedorqui
  • 275,237
  • 103
  • 548
  • 598
angel
  • 322
  • 1
  • 7