Remove all system function with sed command
I have cpp file I want to remove all system function inside this cpp file with sed command for example
int a;
cin>>a;
system(a);
system("dadad");
system('dad');I want to remove all system function in above code is there any way that I can solve this problem with sed command
1 Answer
This could get complicated but all your examples show one command per line with the system command starting at the beginning of the line. In that case, it is relatively simple. Here are three methods:
Using grep:
$ grep -v '^system(' file.cpp
int a;
cin>>a;The regex ^system( matches lines that begin with system(. The -v option tells grep to remove the lines that match.
Using sed:
$ sed '/^system(/d' file.cpp
int a;
cin>>a;The d command tells sed to delete any line that matches the regex ^system(.
Using awk:
$ awk '!/^system\(/' file.cpp
int a;
cin>>a;In awk, ! is negation and !/^system\(/ prints any line that does not match the regex ^system\(. (Unlike for the default forms of grep and sed, ( is a regex-active in awk and we must escape it with a backslash in order to match an actual (.)