Tag Archives: PERL

Length extension

By bigbenn

Ok I have a PERL script that reads an .xml file. One of the new services listed in the file, is alot longer than the others. I am trying to get a way for the spaces of the Service module to read the .xml file and set the length of the %s to the longest Service

Code:

my $jmsfn = "jms-connection.xml";

sub getJmsPathName($) {
my ($i) = @_;
return sprintf "/tmp/$jmsfn", $APSAPPDIR, $i, $i;
}

my $format = "%8s %15s %8s %sn";
printf $format, "Instance", "Service", "Actual", "Configured";
foreach my $i (@instances) {
prolix("Checking instance: " . $i);
my $jmsref = getJmsXml($i);
foreach my $c (@{$jmsref->{JMSConnConfig}->{JMSConnections}->{Connection}})


The format is currently set at 15, but I want it to be set to the amount of spaces of the longest Service, becuase it is messing up the column alignment. I know I need to add a subroutine, I am just not sure how I should set it to recgognize the value.

…read more

Source: FULL ARTICLE at The UNIX and Linux Forums

Formatted output in PERL

By mahi_mayu069

Hi Techies,

I’m a newbie to PERL, Please help me with following problem.

I have a text file like below

418673132,P
492538858,P
384535478,P
521522357,I
529435679,I
183617024,P
184414408,I
735510689,P
736238343,I
411642045,I
412690979,I
104232783,I
104281821,P
104526020,I
180317526,P
180480531,I

i want the output like below in a seperate text file

———- Post updated at 10:10 AM ———- Previous update was at 10:06 AM ———-

[QUOTE=mahi_mayu069;302834099]Hi Techies,

I’m a newbie to PERL, Please help me with following problem.

I have a text file like below

cat input.txt

Code:


418673132,P
492538858,P
384535478,P
521522357,I
529435679,I
183617024,P
184414408,I
735510689,P
736238343,I
411642045,I
412690979,I
104232783,I
104281821,P
104526020,I
180317526,P
180480531,I


i want the output like below in a seperate text file

cat output.txt

…read more

Source: FULL ARTICLE at The UNIX and Linux Forums

Perl :: to parse the data from a string.

By scriptscript

Hi folks,

I have a line in log from which I need to parse few data.

Code:

Jul 6 00:05:58 dg01aipagnfe01p %FWSM-3-106011: Deny inbound (No xlate)


From the above… I need to parse the %FWSM-3-106011: substring.

Another example

Code:

Jul 13 00:08:55 dq01aipaynas01p %FWSM-6-302010: 2 in use, 1661 most used


from the above Ineed to parse the %FWSM-6-302010: substring in perl.

Could anyone please let me know how to parse as mentioned above in PERL..

Thanks in advance ….

Regards,
J

…read more

Source: FULL ARTICLE at The UNIX and Linux Forums

Script to create unique look-up for headers for a Dictionary

By gimley

I have a text file in UTF-8 format which has the following data structure

Code:

HEADWORD=gloss1,gloss2,gloss3 etc


I want to convert it so that all the glosses of the HeadWord appear on separate lines

Code:

HEADWORD=gloss1
HEADWORD=gloss2
HEADWORD=gloss3


An example will illustrate the requirement
INPUT

Code:

હોશમાં આવવું=regain consciousness.
હોશિયાર=clever, intelligent; skilful; alert, vigilant; cautious; understanding, sensible.
હોશિયારી કરવી=boast,(try to) be clever.
હોશિયારી દાખવવી=boast,(try to) be clever.
હોશિયારી બતાવવી=boast,(try to) be clever.
હોશિયારી મારવી=boast,(try to) be clever.
હોશિયારી રાખવી=be cautious,be vigilant,be alert.
હોશિયારી=cleverness, vigilance
હોહા=noise, uproar, tumult, public talk or discussion, excitement, agitation, alarm, consternation.
હોહાકાર=uproar, tumult, excitement, alarm.
હોહો=noise, uproar, tumult, public talk or discussion, excitement, agitation, alarm, consternation.


The Output would be

Code:

હોશિયાર=clever
હોશિયાર=intelligent
હોશિયાર=skilful
હોશિયાર=alert
હોશિયાર=vigilant
હોશિયાર=cautious
હોશિયાર=understanding
હોશિયાર=sensible.
હોશિયારી કરવી=boast
હોશિયારી કરવી=(try to) be clever.
હોશિયારી દાખવવી=boast
હોશિયારી દાખવવી=(try to) be clever.
હોશિયારી બતાવવી=boast
હોશિયારી બતાવવી=(try to) be clever.
હોશિયારી મારવી=boast
હોશિયારી મારવી=(try to) be clever.
હોશિયારી રાખવી=be cautious
હોશિયારી રાખવી=vigilant or alert.
હોશિયારી=cleverness
હોશિયારી=vigilance
હોશિયારી=etc.
હોહા=noise
હોહા=uproar
હોહા=tumult
હોહા=public talk or discussion
હોહા=excitement
હોહા=agitation
હોહા=alarm
હોહા=consternation.
હોહાકાર=uproar
હોહાકાર=tumult
હોહાકાર=excitement
હોહાકાર=alarm
હોહો=noise
હોહો=uproar
હોહો=tumult
હોહો=public talk or discussion
હોહો=excitement
હોહો=agitation
હોહો=alarm
હોહો=consternation


At present I use macros which identify the delimiter, copy the text between two delimiters, paste it on next line, preface it with the headword and continue the operation till end of line and repeat the same for the next line. Since the file is huge a PERL or AWK script would help.
I work under Windows and UNIX type solutions do not work for me unfortunately.
Many thanks in advance.

From: http://www.unix.com/shell-programming-scripting/221547-script-create-unique-look-up-headers-dictionary.html

Perl : printing the files in the exact order as it is..

By scriptscript

Hi folks,

I am trying to open directory and then print the files in that directory in PERL.
I have used the below code.

Code:

opendir(DIR,$destination) or die ("Cannot open directory $destination");
my @bwfiles = readdir(DIR);
print @bwfiles;


I am able to retrieve all the files the files are not printed according to the timestamp.

In directory files are listed as below..

Code:

-rw-r--r-- 1 johng users 10471630 2013-03-19 12:56 file1.txt
-rw-r--r-- 1 johng users 45496 2013-03-19 13:37 file2.txt
-rw-r--r-- 1 johng users 45512 2013-03-24 13:42 file3.txt
-rw-r--r-- 1 johng users 0 2013-03-28 06:17 file4.txt
-rw-r--r-- 1 johng users 0 2013-03-28 06:18 file5.txt


But while printing the files..

Code:

file5.txt
file3.txt
file4.txt
file1.txt
file2.txt


But I expected is

Code:

file1.txt
file2.txt
file3.txt
file4.txt
file5.txt


How to print them in exact order in the directory..

Could anyone please help me on this ?

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Sorting a file with frequency on length

By gimley

Hello,
I have a file which has the following structure

Code:

word space Frequency


The file is around 30,000 headwords each along with its frequency. The words have different lengths. What I need is a PERL or AWK script which can sort the file on length of the headword and once the file is sorted on lenght: smallest to largest; sort each such set of words having the same length on their frequency.
At present I do this in Excel using the

Code:

=Len(text)


formula, but this is getting tedious.
I am giving below a sample input file

Code:

the 29962169
and 14291859
you 12345509
for 3296048
not 3091071
but 2994482
say 2345958
she 2123744
get 2081392
one 1988291
can 1915289
out 1812292
him 1571291
who 1543711
are 1487971
now 1453264
was 1399013
that 7834407
have 5930242
with 3983564
this 3814998
what 3327049
they 2684414
your 2329896
know 2221467
from 2207336
like 1845600
just 1756270
here 1558771
come 1541623
when 1465219
there 1957160
about 1903238
right 1410555
think 1398723
would 1346905


The expected output would be:

Code:

the 29962169
and 14291859
you 12345509
for 3296048
not 3091071
but 2994482
say 2345958
she 2123744
get 2081392
one 1988291
can 1915289
out 1812292
him 1571291
who 1543711
are 1487971
now 1453264
was 1399013
that 7834407
have 5930242
with 3983564
this 3814998
what 3327049
they 2684414
your 2329896
know 2221467
from 2207336
like 1845600
just 1756270
here 1558771
come 1541623
when 1465219
there 1957160
about 1903238
right 1410555
think 1398723
would 1346905


As you can see the file has been sorted on length and then on frequency.
Any help given would avoid the tedium of loading the file each time in excel. Many thanks in advance

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

PERL: retrieve the data based on regular expression

By i150371485

Hi Friends

i have a code below

sample $text contains the values

Code:


test1 PIC X
test1 PIC XX
test1 PIC XXX
test1 PIC X(8)
test1 PIC X(12)
test1 PIC X
test1 X(8)
test1 PIC X VALUE 'N'.


Code:

$text =~ /^d{6} +(d{2}) +([w|-|:]+) +PIC +([d|w]+)(((d+))(V([d|w]+)| +(COMP-3).|.)|( +(COMP-3).| +(COMP-3))|.)/)


the above retreives the

Code:

test1 PIC X
test1 PIC XX
test1 PIC XXX
test1 PIC X(8)
test1 PIC X(12)
test1 PIC X
test1 X(8)


i am not able to match the last line . i am newbie to perl and ned the regular expression which also captures the last line .

Code:


test1 PIC X VALUE 'N'.


Please help

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Elaminate BACKTRACKS in PERL

By bashily

I have the following code and I am trying to use mail command to send email to myself. settings on my server is setup, so I don’t need define the “FROM” address.
The main problem is if I use “-T” for tainted mode, this code won’t work. any idea how to fix it?


#!/usr/bin/perl -wT
$ENV{PATH}="/bin:/usr/bin"; # Securing the PATH
#@mail_to = split(/=/, $ENV{QUERY_STRING});
#$mail_to[1] = s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
#$mail_to[1] = tr/+/ /;
### Declare Variables ###
my $incorrectFlag = 0;

# Define REGEX pattern for email field
my %PATTERNS = (
"email" => '^(([a-zA-Z0-9_-.]+)@([a-zA-Z0-9_-.]+).([a-zA-Z]{2,5}){1,60})$'
);

# GET DATA FROM THE ENVIRONMENT VARIABLE
$qstring = $ENV{'QUERY_STRING'};

# Break data into ARRAY
@pairs = split (/&/, $qstring);

# START A LOOP TO PROCESS FORM DATA
foreach (@pairs) {

#split field name and value on ‘=’, store in two scalar variables
($key, $value) = split (/=/);
#translate ‘+’ signs back to spaces
$value =~ tr/+/ /;
#translate special character
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
#store data in hash
$form{$key} = $value;
}

######### HTML HEADER ##########
print "Content-Type:text/htmlnn";
# Check email format against the pattern
if ($form{address} !~ $PATTERNS{email})
{
print " This is a warning! You need to input a proper email address and nothing more!n";
$incorrectFlag = 1;
exit;
}
else
{
#print $form{address};
$incorrectFlag = 0;
}
'mail -s "message" $form{address} < message.txt';
print "n";
print "

n";
print "The sender is $form{address}
n";
#print "The Value of QSTRING is: $qstringn";
print "

n";


…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Perl – Append data to existing excel cell

By prasperl

Hello All,

I have the following code in PERL to write data to excel sheet.

Can someone please help me about how to append data to an exisitng cell?

For ex in the below given case,Cell 1,1 has Active State PERL

Now I want to add a new line like “prorgamming” without overwritting the existing content.

Is there a way to do it?


my $workbook = Spreadsheet::WriteExcel->new($FileName);
## Add a worksheet
my $worksheet1 = $workbook->add_worksheet('PERL');
## Define the format and add it to the worksheet
my $format = $workbook->add_format(
#center_across => 1,
bold => 1,
size => 10,
border => 4,
color => 'black',
bg_color => 'cyan',
border_color => 'black',
align => 'vcenter',
);
## Change width for only first column
$worksheet1->set_column(0,0,20);
my $failcolor= $workbook->add_format(
bold => 1,
size => 10,
border => 4,
color => 'red',
border_color => 'black',
align => 'vcenter',
);

# Write a formatted and unformatted string, row and column
# notation.
$worksheet1->write(0,0, "PERL FLAVOURS",$format);
$worksheet1->write(1,1,"Active State PERL",$failcolor);
$worksheet1->write(2,0,"Strawberry PERL");
$worksheet1->write(3,0,"Vennila PERL");


…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

PERL annoying scope problem

By yifangt

Hello,
I met a problem with following code:

Code:

#!/usr/bin/perl -w
# test.pl

use strict;
use diagnostics;
use DBI;

my $dbh = DBI->connect(
"DBI:mysql:BibleBook","yifangt","password")
or die("Cannot connect: $DBI::errstr");

my $sql = qq(SELECT * FROM library WHERE isbn = '1234512345');

my $sth = $dbh->prepare($sql);

$sth->execute;

while(my $record = $sth->fetch){
for my $field (@$record){
print "$fieldn";
}
print "nn";
}


which looks fine and was running successfully except an warning message:

Code:

"Use of uninitialized value $field in concatenation (.) or string at test.pl line 20. "


The diagnostics tells me the problem is with line 20 and I googled around and, found it is a very popular scope problem with $field, which actually should be related to @$record. But,
1)$record was declared, so was $field;
2)how the scope change within the while and for loops, or/and outside the while loop? which is what I am not sure about;
3) the interesting thing is not every line of the loop issued the same warning. Why?
Can anybody tell me exactly what was wrong in my case and the correct way to eliminate this warning?
Thanks a lot!

Source: FULL ARTICLE at The UNIX and Linux Forums

Sorting on length with identification of number of characters

By gimley

Hello,
I am writing an open-source stemmer in Java for Indic languages which admit a large number of suffixes.
The Java stemmer requires that each suffix string be sorted as per its length and that all strings of the same length are arranged in a single group, sorted alphabetically. Moreover as a header I need to specify the numeric value of the string, say

Quote:

5
6
7
8
etc.


Since the languages in question have over 300 and more suffixes, trying to sort on length and identifying the length of each string and counting it becomes a difficult issue.
An example will make this clear.
Input:

Quote:

आधी
इतक
इतपत
ईचना
ईचनात

ईना
ईन


Expected output

Quote:

1

2
ईन
3
आधी
इतक
ईना
4
इतपत
ईचना
5
ईचनात


Since handling such a large database is laborious, is it possible to write a script in AWK or PERL which would enable the above output.
Your help would go a long way in putting java-based stemmers in different languages in the open-source community.
Many thanks in advance for your kind help

Source: FULL ARTICLE at The UNIX and Linux Forums

Help in separating a multilingual file

By gimley

Hello,
I have a text file running into around 100 thousand+ lines which has the following rigid structure:

Quote:

Identity Number (always a Numeric Field)


Quote:

English (a whole set of names


Quote:

Data in another language in UTF8 format


Each field is separated by a comma.
Some examples are given below:

Quote:

23,Chinttaman Pagare,चिंतमण पगारे
24, Chinttaman Pateel,चिंतामण पाटल
25, Chinttaman Rout,चिंतामण राऊत
26, Chinttaman Yashawante,चिंतामण यशवंत


I would like to extract the data such that all the English words are stored in one file and the other language words in another file. The numbers would be ignored.
I work under windows OS.
A script in AWK or PERL would be of great help.
Many thanks in advance

Source: FULL ARTICLE at The UNIX and Linux Forums

Classpath of JAVA

By yifangt

Hello,
ABC question as I could not get a direct answer: How to locate the default IO class of JAVA? My question was triggered by the row in most java program

Code:

import java.io.*


as I am very curious where the io class is located in my Mint14 (Ubuntu12.10). I am aware it is similar to use DBI in PERL, or #include in C++, where I could locate the packages/libraries in certain directories:

Code:

/usr/lib/perl5/DBI.pm
/usr/include/c++/4.7/iostream


but I could not locate any file name (io.class, or io.jre) for this io class in java? Have checked my system for the libraries:

Code:

/usr/lib/jvm/java-7-openjdk-amd64/lib/
/usr/lib/jvm/java-7-openjdk-common/jre/lib/
/home/kevin/downloadsoftware/jre1.7.0_10/lib/
/usr/lib/eclipse/plugins/


Also checked Oracle website, where there is:

Code:

package java.io


but I could not find this file in my system, and the java program works fine. Can anybody give me some explanantion about the “default IO, file, string, date & time, etc classes” of java1.7.x when it was installed. My java is:

Code:

$java -version
java version "1.7.0_09"
OpenJDK Runtime Environment (IcedTea7 2.3.3) (7u9-2.3.3-0ubuntu1~12.10.1)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)


Thanks a lot!

Source: FULL ARTICLE at The UNIX and Linux Forums