Solved

traverse through a file to replae tab with pipe

Posted on 2010-09-20
4
569 Views
Last Modified: 2012-05-10
Hi can someone fill in the below function, not very handy with perl. Thanks

sub replaceTab{
 
my $file = $_;

//replace all tab in $file with pipe |  

}
0
Comment
Question by:gagaliya
4 Comments
 
LVL 2

Expert Comment

by:skwok
ID: 33721222
Give this a try (from: : http://www.linuxquestions.org/questions/programming-9/perl-replace-text-in-file-476382/)

All you need is this:

/usr/bin/perl -p -i -e "s/$1/$2/g" $3

For example, let's say you want to change all occurrences of "rabbit" to "blueberry" in all ".html" files in the current directory. You would:

Code:
/usr/bin/perl -p -i -e "s/rabbit/blueberry/g" *.html


The above script executes perl as a one-liner, using shell to parse the substitution parameters. The perl part is:

/usr/bin/perl -p -i -e "s/$1/$2/g" $3

Where:

Quote:
-p
causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed:

LINE:
while (<>) {
... # your program goes here
} continue {
print or die "-p destination: $!\n";
}

If a file named by an argument cannot be opened for some reason, Perl warns you about it, and moves on to the next file. Note that the lines are printed automatically. An error occurring during printing is treated as fatal. To suppress printing use the -n switch. A -p overrides a -n switch.

BEGIN and END blocks may be used to capture control before or after the implicit loop, just as in awk.

-i[extension]
specifies that files processed by the <> construct are to be edited in-place. It does this by renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements. The extension, if supplied, is used to modify the name of the old file to make a backup copy, following these rules:

If no extension is supplied, no backup is made and the current file is overwritten.

If the extension doesn't contain a *, then it is appended to the end of the current filename as a suffix. If the extension does contain one or more * characters, then each * is replaced with the current filename. In Perl terms, you could think of this as:

($backup = $extension) =~ s/\*/$file_name/g;

-e commandline
may be used to enter one line of program. If -e is given, Perl will not look for a filename in the argument list. Multiple -e commands may be given to build up a multi-line script. Make sure to use semicolons where you would in a normal program.

"s/$1/$2/g"
performs the string substitution

$3
is the filename glob
0
 
LVL 12

Accepted Solution

by:
Fidelius earned 500 total points
ID: 33721300
Hi,

You need something like this:

sub replaceTab {      
      open INPUTFILE, "<", $ARGV[0] || die "Couldn't open the file!";
      open OUTPUTFILE, ">", $ARGV[1] || die "Couldn't open the file!";
      while (<INPUTFILE>) {
        $_ =~ s/\t/\|/g;
        print OUTPUTFILE $_;
      }
      close INPUTFILE;
      close OUTPUTFILE;
}

Regards!
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 33721413
More efficient for replacing single characters is tr///.

perl -p -i -e 'tr/\t/\|/' *.html

or replace s/// with tr/// in Fidelius answer.
0
 
LVL 1

Author Closing Comment

by:gagaliya
ID: 33725804
perfect, thank you guys.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Script to append xml file 32 192
perl to mysql 5 130
ppm conversion to curl on a module install 8 80
Perl regex to replace any capital letters not preceded by ">" 6 109
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.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now