Perl One Liners:
perl -i.bak -p -e 's#\n##ig' ser : To remove 'new line char' from a file
perl -i.bak -p -e 's# #\n#ig' ser : To introduce a 'new line char' in a
file, i.e where where is a space char, replace it with a newline char
V. useful and imp:
perl -i.bak -p -e 's#set .*TOP_NAME.*\n#$_\set_svf
"\$TOP_NAME.svf"\n#ig' arm.tcl
to include white space chars between set and TOP_NAME:
perl -i.bak -p -e 's#set\sTOP_NAME.*\n#$_\set_svf
"\$TOP_NAME.svf"\n#ig' *tcl
File Reading in Perl:
Reading a single line:
open (CHECKBOOK, "checkbook.txt"); ## Open a file called
checkbook.txt, using a file handle CHECKBOOK.
$record = <CHECKBOOK>; //Read one line in variable 'record'
Reading recursively every line
one by one till end of file
open (CHECKBOOK, "checkbook.txt");
while ($record = <CHECKBOOK>) {
print $record;
}
close(CHECKBOOK);
Reading a Whole File at Once
open(MYINPUTFILE, "<filename.out"); # open for input
my(@lines) = <MYINPUTFILE>; # read file into list
@lines = sort(@lines); # sort the list
my($line);
foreach $line (@lines) # loop thru list
{
print "$line"; # print in sort order
}
close(MYINPUTFILE);
Perl split
@mystr = split(/ /, $InArgs{'ExtnList'});
Perl foreach
foreach $ii(@mystr)
Perl file exists check
if(-e "./InpVars")
Perl Pattern Match
if($line =~ /^(DirSearchOrder)\s+(.*)/)