Solved

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

Posted on 2008-10-16
3
683 Views
Last Modified: 2013-12-26
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
Comment
Question by:EmadGirgis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
RSLE earned 200 total points
ID: 22733320

#!/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
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 150 total points
ID: 22733482
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
 
LVL 2

Assisted Solution

by:tdiops
tdiops earned 150 total points
ID: 22733641
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question