Solved

replace tabs with spaces

Posted on 2008-10-16
3
2,188 Views
Last Modified: 2013-12-26
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 13

Assisted Solution

by:marchent
marchent earned 150 total points
ID: 22733107
Traverse the directory, open file and read each line. For each line perform tab replacement as below example
## Replace tabs with a single space
my $str = "1\t2\t3\t4 5 6";
print $str ."\n";
$str =~ s/\t/ /g;
print $str ."\n";

Open in new window

0
 
LVL 6

Accepted Solution

by:
RSLE earned 200 total points
ID: 22733404
double post?
http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q__23820640.html

#!/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: 22733494
One of the posts should be deleted...
#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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

617 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