Shell Script or command to Remove all Text Between a start character and end character in Entire Directory
Shell Script or command to Remove all Text Between a start character and end character in Entire Directory
Looking for an easy way to remove all of my TODO: Comments throughout an entire directory of scripts.
If I have:
/*
TODO: Some text here
Some more Here
Some more Here
*/
I want to delete the /* Everything in Between and */
Not a huge deal, just wondering for decreasing file sizes
Linux..didn't specify
– Micah Yarborough
Sep 11 '18 at 21:38
And yes, I have looked through the hundreds of answers that break code. Thanks for looking though :)
– Micah Yarborough
Sep 11 '18 at 21:45
Possible duplicate of Use Bash (sed?) to remove a multiline /* ... */ style comment containing specific text (regex)
– Zlemini
Sep 11 '18 at 22:01
Surely looked but apparently not enough...
– PilouPili
Sep 11 '18 at 22:03
1 Answer
1
Here is something to start with:
$file = 'somefile.php';
$f = fopen($file,'r');
$temp = dirname($file).'/temp.php';
print_r($temp);
$t = fopen($temp, 'w');
$mode = 'write';
$buffer = ;
while($line=fgets($f))
print_r($mode."n");
switch($mode)
case 'write':
if(preg_match('/^s*/\*/', $line))
$buffer = $line;
$mode = 'comment';
else
fputs($t,$line);
break;
case 'comment':
if(preg_match('/^s*\**s*@TODO/i', $line))
$buffer = ;
$mode = 'skip';
else if(preg_match('/^s*\*//', $line))
fwrite($t,implode("", $buffer));
$buffer = ;
fputs($t,$line);
$mode = 'write';
else
$buffer = $line;
break;
case 'skip':
if(preg_match('/^s*\*//', $line))
$mode = 'write';
break;
/*
fclose($t);
fclose($f);
rename($temp, $file);
*/
I tested it against this file
<?php
$var= "foo";
/*
@todo hello
some other shuff
*/
/**
*
* @author Me
*
*/
class someclass
This was the stuff in temp.php
temp.php
<?php
$var= "foo";
/**
*
* @author Me
*
*/
class someclass
hope it helps. I would probably back up your files before attempting this.
UPDATE
Had to fix a couple minor things so it would skip comments without @todo. The rest you can probably work out fairly easily.
@todo
The regex is only there to account for spaces or comments that are like this
/*
* @todo
*/
This \* becomes * for the regex engine that then becomes a literal *.
\*
*
*
So this one s*/\*
s*/\*
s*
/
/
\*
*
Or in English the /* open comment tag.
/*
I mention this because the regex is tricky, but you probably have spaces and other stuff in there, probably every @todo isn't the very first thing on the line.
@todo
Doing it like this, you get to handle each line on it's own, it simplifies things and more importantly increases the matching accuracy.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
And have looked on your favorite search engine and read PowerShell documentation ? As you say shouldn't be a big deal.
– PilouPili
Sep 11 '18 at 21:37