[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

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!
0
hankknight
Asked:
hankknight
  • 3
  • 2
  • 2
  • +2
1 Solution
 
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
 
svsCommented:
oh. stupid me, sorry.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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
 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now