0

I have nearly 300 files in 60 folders .

As per the C++ coding guidelines, I need to replace below lines from *.cpp and *.cl files (wants to remove extra space between if and for statement) -

for (* .....)

with

for(* .....)

and also

if (* .....)

with

if(* .....)

Can any one suggest me the grep command to do search and replace for all files.

Edited:

I tried with below commands:

sed -i 's/for (/for(/g' *.cpp

But got error like below:

sed: can't read *.cpp: No such file or directory
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • 1
    If you need/want to do it with grep specifically, then the [Unix&Linux StackExchange](http://unix.stackexchange.com) might be better suited for this question. That being said, there are a lot of code beautification tools which can do that kind of stuff automatically, without having to write a bunch of regular expressions – Felix G Aug 24 '20 at 08:30
  • 2
    You may consider to use the `sed` utility. – M. Nejat Aydin Aug 24 '20 at 08:31
  • 1
    The `grep` utility only *finds* patterns in files. It doesn't replace with other patterns. As mentioned, see the `sed` program. – Some programmer dude Aug 24 '20 at 08:35
  • I have updated my question .Instead of grep I need any linux command to do so . – mahesh narlekar Aug 24 '20 at 08:37
  • 1
    You should use a formatter instead of replacing manually like this. Currently probably [clang-format](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) is the most common one and is supported by almost all IDEs. Visual Studio even includes clang format by default – phuclv Aug 24 '20 at 09:08

2 Answers2

1

I think you need sed command (stream editor, see man sed on your mashine). It is more suitable for file editing.

sed -i -E 's/(for|if)[ ]+(\(.*\))/\1\2/g'

Let me explain:

  1. -i stands for inline, that means that all changes will be done and saved in the file
  1. -E is needed to use extended regular expression inside with sed

  2. s/(for|if)[ ]+(\(.*\))/\1\2/g

    • s stands for substitute
    • / is a separator, which separates different parts of command. Between first / and second / there is pattern that you need to find (and then replace). After second / and third / there that we want to have after substitution.
    • g in the end stands for global, that means to make changes in the whole file.

How to apply to every file that you need?

This question is already exist, so in the end you need to run in directory where are your files stored following command

find ./ -type f -exec sed -i -E 's/(for|if)[ ]+(\(.*\))/\1\2/g' {} \;

I hope, this will help:)

Plov Ec
  • 36
  • 2
0

I have created the file "brol.txt", with following content:

for (correct
for(wrong
if (correct
if(wrong

I have launched following grep command:

grep -E "for \(|if \(" brol.txt

With following result:

for (correct
if (correct

Explanation:

grep -E means extended grep (allows to search for expression1 OR expression2,
                             separated by a pipe character)
\( means the search for a round bracket. The backslash is an escape character.
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • My files names are different in every folders . So got error ..I have updated my question . – mahesh narlekar Aug 24 '20 at 08:35
  • I advise you to use the `find` utility: `find -name "*.cpp" -exec sed ... {} ... \;`, plenty of examples on this site. – Dominique Aug 24 '20 at 08:37
  • This quick and dirty attempt will also match `"replacing for () with for()"` and `find_for (x)`, and more problematic: `#define inside_for (x)` that becomes `#define inside_for(x)` which has a very different meaning. – chqrlie Aug 24 '20 at 16:30