• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 692
  • Last Modified:

Need a script to traverse my files check for tabs replace tabs with spaces!!

Hi all

What do I do to get list of files that have tabs? How to do substitute the tabs with spaces?
Is there a way to do that in a perl script that traverse the directory structure and replace tabs with spaces?


my project has so many files it would be time consuming to do one by one!


Thanks
in advance!
0
EmadGirgis
Asked:
EmadGirgis
3 Solutions
 
RSLECommented:

#!/usr/bin/perl
 
$dir = "path";
 
use File::Find;
find(sub{push(@files, $File::Find::name) unless -d}, $dir);
 
foreach $file (@files) {
        open(FILE, $file);
        foreach $line (<FILE>) { push(@file, $line); }
        close(FILE);
 
        $data = join("", @file);
        @file = ();
 
        $data =~ s/\t/ /g;
        open(FILE, "> $file");
        print FILE $data;
        close(FILE);
}

Open in new window

0
 
Adam314Commented:
From a prompt, change to the directory you want to start, then enter this:

If you don't want a backup, remove the .bak part
#Create a backup with extension .bak
find . -type f -exec perl -i.bak -pe 's/\t/ /g' \{\} \;
 
#Don't create a backup
find . -type f -exec perl -i -pe 's/\t/ /g' \{\} \;

Open in new window

0
 
tdiopsCommented:
Here's a sed version:
# find all files from here (.)
# copy filename to hold space
# substitute tabs to spaces
# if there was no substitution go on to the next file
# otherwise append the hold space to the pattern space
# generate the mv command
# execute and display it
find . -type f \
| sed -n '
 h
 s/[\t]/ /g
 T
 G
 s|\(.*\)\n\(.*\)|mv "\2" "\1"|p
' \
| sh -sx

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now