views:

117

answers:

3

I'm using this unix command(in a php file) to remove a certain string and then remove the whitespace left by that string. Unfortunately in many cases, the files get completely erased.

Is there a workaround?

<?php

$dir = "./";

$rmcode = `find $dir -name "*.php"
-type f |xargs sed -i 'somestring' 2>&1`; echo "String removed.<br />\n";

$emptyline = `find $dir -name "*.php"
-type f | xargs sed -i '/./,$!d' 2>&1`; echo "Empty lines removed.<br
/>\n";

?>

Edit

Would this work?

$emptyline = `find $dir -name "*.php"
-type f | xargs sed -i '/./,$!d' 2>&1`; echo "Empty lines removed.<br
/>\n";

becomes

$emptyline = `find $dir -name "*.php"
-type f | xargs sed -i '/\s/,$!d' 2>&1`; echo "Empty lines removed.<br
/>\n";
A: 

so

$emptyline = find $dir -name "*.php" -type f | xargs sed -i '/./,$!d' 2>&1; echo "Empty lines removed.\n";

becomes

$emptyline = find $dir -name "*.php" -type f | xargs sed -i '/\s/,$!d' 2>&1; echo "Empty lines removed.\n";

??

Naqi
Is this an answer or a question?
Mark Byers
a question...i just need to remove the whitespace left by the previous string..not all whitespaces in the entire file...is that possible? thanks.
Naqi
Edit your question, do not create answers to follow-up. Answers are, aptly, reserved for actual answers to your question.
Mark Trapp
A: 

try removing the string AND the whitespace all at once:

<?php

$dir = "./";

$rmcode = `find $dir -name "*.php" -type f | xargs sed -i 's/somestring\s*//g' 2>&1`; echo "String removed.<br />\n";

?>

The \s matches whitespace.
The * means zero or more times.


Updated with working example.

fseto@bass:~/src$ echo "test somestring is a test" > testfile
fseto@bass:~/src$ cat testfile
test somestring is a test
fseto@bass:~/src$ sed -i 's/somestring\s*//g' testfile 
fseto@bass:~/src$ cat testfile
test is a test
fseto
thanks for the answer...just a quick question..would this remove whitespace from just the line that was removed or also whitespaces after that and within the entire file? thanks!
Naqi
i just tried this and it did not seem to delete the string. I tried with the word 'somestring' just as i have it above. In my actual version im using a regex expression instead of somestring. The expression is 's#<?php /\*\*/ eval(base64_decode("aWY.*?>##g' which removes any line of that sort. All the help is very much appreciated.
Naqi
You probably want to update the question. or post a new question. In any case, you want to match and remove the whitespace with the string. Otherwise, you'll have a hard time removing whitespace after the fact.
fseto
A: 

I dont seem to be able to comment on the question above, but my answer for "Jonathan" . Im trying to remove the string from the line that contains it and once done, remove the whitespace left by that line. So if i have

line1 somestring line2 somethingelse line3 else..

ill end up with

line1 somethingelse line2 else...

Naqi