Removing all lines that begin with // and contain keyword

Hello,

I want to remove all lines that start with "//" and contain "deleteme" from all files in a directory and all its subdirectories.

For example, this line should be removed:

     // $Id: my.class.php 29 2006-08-18 07:35:21Z deleteme_ $

There is a simular solution here:
http://www.experts-exchange.com/OS/Linux/Q_22904616.html

Thanks!
LVL 16
hankknightAsked:
Who is Participating?
 
ghostdog74Commented:
you can go through a for loop or while loop

Using for loop:

for files in $(find /path -type f -name "*txt"  )
do
   awk '
 /^ +\/\/.+deleteme/ || /^\/\/.+deleteme/{next}
 {
   print $0 > "temp"      
 }
 END {
  {
        cmd = "mv temp \047" FILENAME "\047"
        system(cmd)      
   }
 }' "$files"
  #remove temp file if desired.
done

or while loop

find /path -type f -name "*txt" | while read files; do
echo "files:$files"
#your awk command....
done
0
 
svsCommented:
grep -r -l  '^//.*deleteme' | xargs echo rm

(remove 'echo' after dry run.)

But this will work only if there are no names will embedded spaces.
0
 
omarfaridCommented:
Hi,

svs:

He is not trying to remove the file, he wants to remove the lines from the file
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
svsCommented:
oh. stupid me, sorry.
0
 
ghostdog74Commented:
for files in *txt;
do
 awk '
 /^ +\/\/.+deleteme/ || /^\/\/.+deleteme/{next}
 {
   print $0 > "temp"      
 }
 END {
  {
        cmd = "mv temp \047" FILENAME "\047"
        system(cmd)      
   }
 }' "$files"
done



or


for files in *txt;
do
 awk '
 /^ +\/\/.+deleteme/ || /^\/\/.+deleteme/{next}
 {
   print $0 > "temp"      
 }'  "$files"
 mv "temp" "$files"
done

0
 
hankknightAuthor Commented:
Thanks, ghostdog74.

I tried your first example and it worked PERFECTLY except I want it to work recursively for ALL sub-directories (there are over 100)
0
 
TintinCommented:
I think it's easier to do:

find /some/dir -type f|xargs -i sed -i '/^\/\/.*deleteme/D' {}
0
 
ghostdog74Commented:
have you tried  xargs with file names that contains spaces ?
0
 
TintinCommented:
The OP didn't mention anything about filenames with spaces in them.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.