Perl: How to Replace Semicolons
The Perl programming language was originally designed to create reports. It contains powerful functions for manipulating text files. For example, you can replace a character (such as a semicolon) or string of characters with another. The "s" function is used for substitution or replacement. The edited text is placed into a second text file and the original file remains unchanged. The "open" function opens the files. The "close" function closes the files when you are finished using them.
Instructions
-
-
1
Open a blank plain text file in any text editor.
-
2
Type the following line to start the Perl script:
#!/usr/bin/perl
-
-
3
Type the following lines to open the file that contains the semicolons ("orig_file") and the file where you want to save the changes ("output"):
open (FILE, "orig_file") or die $!;
open (OUTFILE, "output") or die $!;
Replace "orig_file" and "output" with the appropriate names for your script.
-
4
Type the following lines to create the while loop that will read the contents of the original file, replace the semicolons with colons and write the changes to the output file:
while (<FILE>)
{
$_ =~ s/;/:/g;
print OUTFILE $_;
}
Replace the ":" in "$_ =~ s/;/:/g;" with whatever character you want to replace the semicolon with.
-
5
Type the following lines to close the open files:
close (OUTFILE);
close (FILE);
-
6
Save the file as "sub_semicolon.pl."
-
7
Type the command "perl sub_semicolon.pl" at a command prompt to execute the script.
-
1