need perl equivalent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need perl equivalent
# 1  
Old 12-16-2009
need perl equivalent

Dear All,

Good day, can any of you help me in the following problem:

I need to find the perl equivalent for the following commandline

Code:
grep characters |awk '{print \$2}'

Expecting your reply and thanks in advance.

Warm regards
Fredrick.
# 2  
Old 12-16-2009
Code:
perl -F'\s+' -ane  'print  $F[1]."\n" if /characters/;' file

# 3  
Old 12-16-2009
Thank you very much for your reply. I wrote the following code using perl mixed with awk and system command in between, i need to change the following just only with perl. Can anyone help me in this regard.

Code:
#!/usr/bin/perl -w

chdir 'PATH';

opendir(DIR, 'test') or die "can't open the directory";

while (defined($fname = readdir(DIR)))
{
  $dir = substr($fname,0,1);

if($dir ne '.')
{
 push @files,$fname;
}
}

@files_use = sort (@files);

$length=@files_use;
$i=0;
$j=0;
chdir 'PATH/test';
open (OUTFILE, ">>PATH/outfilematrix")|| die "can't open file";
while ($i<$length)
{
 while ($j<$length)
 {
  print OUTFILE `./executable -i $files_use[$i] -i $files_use[$j] |grep character |/usr/bin/awk '{print \$2}'`;
  print OUTFILE ("\t");
  $j++;
 }
$i++;
$j=0;
print OUTFILE ("\n");
}

Since the output is not in the form of matrix, i need to convert this program using only perl scripts (with out awk and linux command).

The output should be in a matrix form.

Expecting your reply and thanks in advance.

Warm regards
Fredrick.
# 4  
Old 12-16-2009
Quote:
Originally Posted by Fredrick
...
Code:
#!/usr/bin/perl -w

chdir 'PATH';

opendir(DIR, 'test') or die "can't open the directory";

while (defined($fname = readdir(DIR)))
{
  $dir = substr($fname,0,1);

if($dir ne '.')
{
 push @files,$fname;
}
}

@files_use = sort (@files);

$length=@files_use;
$i=0;
$j=0;
chdir 'PATH/test';
open (OUTFILE, ">>PATH/outfilematrix")|| die "can't open file";
while ($i<$length)
{
 while ($j<$length)
 {
  print OUTFILE `./executable -i $files_use[$i] -i $files_use[$j] |grep character |/usr/bin/awk '{print \$2}'`;
  print OUTFILE ("\t");
  $j++;
 }
$i++;
$j=0;
print OUTFILE ("\n");
}

Since the output is not in the form of matrix, i need to convert this program using only perl scripts (with out awk and linux command).

...
You can tweak this script a bit so that the output is in the form of a matrix.

Replace this line:

Code:
print OUTFILE `./executable -i $files_use[$i] -i $files_use[$j] |grep character |/usr/bin/awk '{print \$2}'`;

by these two lines:

Code:
chomp($x = `./executable -i $files_use[$i] -i $files_use[$j] |grep character |/usr/bin/awk '{print \$2}'`);
printf OUTFILE $x;


HTH,
tyler_durden
# 5  
Old 12-17-2009
hi tyler_durden,

i have replaced that line with the following code and its working fine.

Code:
chomp($x = `./executable -i $files_use[$i] -i $files_use[$j] |grep character |/usr/bin/awk '{print \$2}'`);
printf OUTFILE $x;

Thanks & Regards
Fredrick.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : Perl equivalent to the ksh | and ;

Ive been trying to move to Perl. It has been a struggle. My question is, is there a good resource that explains nesting statements. As an example. To change primary Factory CTS 1.9.0(46) P1 *Slot 1 CTS 1.10.2(42) P1 To primary *Slot 1 CTS 1.10.2(42) P1 ... (5 Replies)
Discussion started by: popeye
5 Replies

2. Shell Programming and Scripting

Perl equivalent substitution

hi Geeks, my input file contains data like => 53 - Deewana Kar Raha Hai.mp3 54 - Hale Dil.mp3 55 - Ishq Sufiyana.mp3 56 - Abhi Kuch Dino Se.mp3 57 - Pee Loon Hoto Ki Sargam.mp3 I had used sed command to remove the prefix from the file name like sed 's/^\ it gives me the perfect... (4 Replies)
Discussion started by: lohith.dutta
4 Replies

3. Shell Programming and Scripting

What is the equivalent of NR (awk) in perl?

Hello, I searched online; it seems that perl use $NR as NR in awk; however it does not work for me. For example, how to re-write the following awk using perl: awk '{ print NR}' inputfile---------- Post updated at 01:55 PM ---------- Previous update was at 12:49 PM ---------- I found... (2 Replies)
Discussion started by: littlewenwen
2 Replies

4. Shell Programming and Scripting

grep -v equivalent in perl

I have to do grep -v in a perl script. I want to exclude blank lines and lines having visitor. #grep -v visitor abc.txt |grep '.' file:abc.txt 1340 not booked 16D:D9 tourist 8 1341 not booked 16C:D4 tourist 25 1342 not booked 16D:C4 visitor 7 1343 not booked 01C:D9 visitor 6 1344... (4 Replies)
Discussion started by: dynamax
4 Replies

5. Shell Programming and Scripting

Regular expression matching in BASH (equivalent of =~ in Perl)

In Perl I can write a condition that evaluates a match expression like this: if ($foo =~ /^bar/) { do blah blah blah } How do I write this in shell? What I need to know is what operator do I use? The '=~' doesn't seem to fit. I've tried different operators, I browsed the man page for... (3 Replies)
Discussion started by: indiana_tas
3 Replies

6. Shell Programming and Scripting

Bash equivalent of perl's pack function ?

Is there an equivalent of perl's pack function in bash ? Or in other words, how can I achieve the same thing in bash ? Much appreciated. (1 Reply)
Discussion started by: NewDeb
1 Replies

7. Shell Programming and Scripting

strtok equivalent in perl

Hi All, Is their any equivalent for strtok (in c) to use in perl script. Thanks in advance. JS (1 Reply)
Discussion started by: jisha
1 Replies

8. Shell Programming and Scripting

perl equivalent to grep -c

Guess the subject lines says it all. What is the perl equivalent to grep -c -c, --count Suppress normal output; instead print a count of match- ing lines for each input file. With the -v, --invert- match option (see below), count non-matching lines. ... (6 Replies)
Discussion started by: popeye
6 Replies

9. Shell Programming and Scripting

export equivalent command in PERL

Hi I need an equivalent command in PERL for the following. export LC_ALL=C; I hope this is the command. Please confirm this and correct me if i am wrong $ENV{LC_ALL}="C"; Thanks and Regards Ammu (1 Reply)
Discussion started by: ammu
1 Replies

10. Shell Programming and Scripting

Perl equivalent of ksh if / echo statement

Is there an equivalent perl statement for the following ksh statement ? example if then ... else ... fi (2 Replies)
Discussion started by: gefa
2 Replies
Login or Register to Ask a Question