954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Replacing a field separator during a read.

Replacing a field separator during a read.

I want to replace a pipe symbol (|) field seperator with a tab when I print the contents of records in a file. This is my text file:Jeremiah Stein|LW89W|1|U
Patty Smith|JJ12R|2|W
The cgi script I wrote will read it and print it out with the | between field. How can I replace the | with a tab? This is my script. It can be called up from a link or in the browser's window: http://localhost/cgi-bin/cvtc/c05ex4b.cgi

#!/usr/bin/perl
#c05ex4b.cgi - reads data from a text file and creates a dynamic Web page
# that prints registered information
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use strict;
 
#declare variables
my ($name, $serial, $modnum, $sysletter, @records, $rec);
 
#Read Records
open(INFILE, "<", "c05ex4.txt")
or die "Error opening c05ex4.txt. $!, stopped";
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
chomp($rec);
($name, $serial, $modnum, $sysletter) = split(/\|/, $rec);
}
 
#create Web page
print "<HTML><HEAD><TITLE>Juniper Printer Registrations</TITLE></HEAD>\n";
print "<BODY><H2>\n";
print "<B>View Registration File</B></h2>\n";
print "The Following Printers have been registratered. \n";
 
foreach my $rec(@records) {
print "$rec\n";
}
print "</BODY></HTML>\n";



I am learning from a text book and it is not covered.

NevadaSam
Newbie Poster
5 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

Look up the substitution operator. s//.

You can replace the data like so:

$myvar = "hello|world|yes!";
$myvar =~ s/|/\t/gi;
print "$myvar";
Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Thanks for the reply. I have it working now. That is some good information to know as I go along.

NevadaSam
Newbie Poster
5 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

Cool, do me a favor and up at the top, mark the thread as solved.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Look up the substitution operator. s//.

You can replace the data like so:

$myvar = "hello|world|yes!";
$myvar =~ s/|/\t/gi;
print "$myvar";

I'm wondering if you or the OP ran the code you suggested, I think you will find the results not what is expected if the code is left as-is.

Usually you don't want to do a substitution on field delimiters but I guess you could if you wanted to. You split the string on the delimiters then munge the data, I had already suggested using join for this (on another forum):

print join("\t",split(/\|/,$var));


Nit-pik: There is also no need to use the "i" option to substitute non alpha characters as there is no case involved.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

Your absolutely right... I didn't test the code, I went off memory. So, You would need to escape the piping symbol as such:

$myvar = "hello|world|yes!";
$myvar =~ s/\|/\t/gi;
print "$myvar";

And as for the regex optional modifier's Whoopie Do. You can add it or leave it, I don't think it really matters. I think you'll find regex to be faster in operation (not that it would be noticable on small amounts of data) than nested splits and joins, but I guess it boils down to style.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

perl -pi.int -e 's/\|/,/g;' fileName.ext

But I am getting

'/' is not recognized as an internal or external command, operable program or batch file.

Any idea?

symfar
Newbie Poster
1 post since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Instead of perl -pi.int -e 's/\|/,/g;' fileName.ext try using double quotes instead of the single quotes. perl -pi.int -e "s/\|/,/g;" fileName.ext http://stackoverflow.com/questions/660624/why-doesnt-my-perl-one-liner-work-on-windows

d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You