Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete perticular characters

Status
Not open for further replies.

santoshdj123

Technical User
Jun 15, 2003
98
0
0
IN
hi

I want to write one script which should delete perticular character (or i will pass it as an argument with the pl script) and make the changes in the existing file and saves the file.
if anyone can assist me then i will be very thankful
 
Like this?

Usage: <scriptname> <char to delete> <filename>
Code:
#!<your perl binary location>

$char=$ARGV[0]; # get char to delete from command line
$file=$ARGV[1]; # get file to modify from command line

undef $/;

# get file contents
open(IN,"<$file");
$file=<IN>;
close IN;

# delete char
$file=~s/$char//gs;

# print file back out
open(OUT,">$file");
print OUT $file;
close OUT;

--G
 
you could make the script more "neat" by using Getopt::Long
Code:
#!/usr/bin/perl -w
use strict;
use Getopt::Long;

my @optionlist = qw(-character=s -filename=s);
my %options;
GetOptions(\%options, @optionlist);

print "all $options{character} will be removed from $options{filename}\n";

This will save you some error handling.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top