using perl to print 1 or 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using perl to print 1 or 0
# 1  
Old 05-22-2009
Java using perl to print 1 or 0

Suppose u have a file
FILE A
ABCA10 DIFF VALUES P203S POLY
ABCA1 DIFF VALUES A1046D
ABCA4 DIFF VALUES D846H POLY
ABCA4 DIFF VALUES I1846T POLY
ABCA4 DIFF VALUES Wa
ABCA4 DIFF VALUES WA
ABCA4 DIFF VALUES Wb

and u want to print that if FILE A have POLY so print 0 otherwise 1

means the output
0 ABCA10 DIFF VALUES P203S POLY
1 ABCA1 DIFF VALUES A1046D
0 ABCA4 DIFF VALUES D846H POLY
0 ABCA4 DIFF VALUES I1846T POLY
1 ABCA4 DIFF VALUES Wa
1 ABCA4 DIFF VALUES WA
1 ABCA4 DIFF VALUES Wb


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

open IN,"LIST_ALL_AWK";

$output_file="LABELS";

open OUT,">$output_file";

$line=<IN>;

 $identity=$line;
 @fields = split(/ /,$identity);
#print $fields[4]."\n";
if ($fields[4] =~m "POLY")
{
print   " 0 $line";
}
else
{
print "1 $line";

}

output showing is
0 ABCA10 DIFF VALUES P203S POLY

otherwise how to print this above output??
# 2  
Old 05-22-2009
Quote:
Originally Posted by cdfd123
...want to print that if FILE A have POLY so print 0 otherwise 1

means the output
0 ABCA10 DIFF VALUES P203S POLY
1 ABCA1 DIFF VALUES A1046D
0 ABCA4 DIFF VALUES D846H POLY
0 ABCA4 DIFF VALUES I1846T POLY
1 ABCA4 DIFF VALUES Wa
1 ABCA4 DIFF VALUES WA
1 ABCA4 DIFF VALUES Wb
...
One way to do it:

Code:
$
$ cat -n myfile.txt
     1  ABCA10 DIFF VALUES P203S POLY
     2  ABCA1 DIFF VALUES A1046D
     3  ABCA4 DIFF VALUES D846H POLY
     4  ABCA4 DIFF VALUES Wb
     5  ABCA4 DIFF VALUES POLY E238
     6  ABCA5 DIFF VALUES A1223 poly
     7  ABCA6 DIFF VALUES Poly Y7789
$
$ # POLY only at the end ?
$
$ perl -ne '{print /POLY$/ ? 0 : 1," $_"}' myfile.txt
0 ABCA10 DIFF VALUES P203S POLY
1 ABCA1 DIFF VALUES A1046D
0 ABCA4 DIFF VALUES D846H POLY
1 ABCA4 DIFF VALUES Wb
1 ABCA4 DIFF VALUES POLY E238
1 ABCA5 DIFF VALUES A1223 poly
1 ABCA6 DIFF VALUES Poly Y7789
$
$ # POLY anywhere in the line ?
$
$ perl -ne '{print /.*POLY.*/ ? 0 : 1," $_"}' myfile.txt
0 ABCA10 DIFF VALUES P203S POLY
1 ABCA1 DIFF VALUES A1046D
0 ABCA4 DIFF VALUES D846H POLY
1 ABCA4 DIFF VALUES Wb
0 ABCA4 DIFF VALUES POLY E238
1 ABCA5 DIFF VALUES A1223 poly
1 ABCA6 DIFF VALUES Poly Y7789
$
$ # POLY only at the end and case insensitive ?
$
$ perl -ne '{print /POLY$/i ? 0 : 1," $_"}' myfile.txt
0 ABCA10 DIFF VALUES P203S POLY
1 ABCA1 DIFF VALUES A1046D
0 ABCA4 DIFF VALUES D846H POLY
1 ABCA4 DIFF VALUES Wb
1 ABCA4 DIFF VALUES POLY E238
0 ABCA5 DIFF VALUES A1223 poly
1 ABCA6 DIFF VALUES Poly Y7789
$
$ # POLY anywhere in the line and case insensitive ?
$
$ perl -ne '{print /.*POLY.*/i ? 0 : 1," $_"}' myfile.txt
0 ABCA10 DIFF VALUES P203S POLY
1 ABCA1 DIFF VALUES A1046D
0 ABCA4 DIFF VALUES D846H POLY
1 ABCA4 DIFF VALUES Wb
0 ABCA4 DIFF VALUES POLY E238
0 ABCA5 DIFF VALUES A1223 poly
0 ABCA6 DIFF VALUES Poly Y7789
$
$

HTH,
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and Print in Unix or Perl

This is (12 Replies)
Discussion started by: parthmittal2007
12 Replies

2. Shell Programming and Scripting

color print using perl

Hi i want to print the text in different color using perl script. i am able to print on console(terminal). but if i try to print the same into a file, i am not able to do color.pl #!/bin/usr/perl use Term::ANSIColor; print "This text is normal.\n"; use Term::ANSIColor qw(:constants);... (3 Replies)
Discussion started by: roopa
3 Replies

3. Shell Programming and Scripting

Perl print between 2 patterns

I have been unable to find this anywhere; I have a multiline variable, and I want to print the text between two patterns in that variable. So the variable is My real name is not DeadmanAnd I need the output to be this, by printing between "real" and "not" name is or including the two... (10 Replies)
Discussion started by: killer54291
10 Replies

4. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

5. Shell Programming and Scripting

awk -F'|' '{ print $5 }' in perl

Hi guys i need you help its urgent.. im trying to translate this file processing command in Perl. awk -F'|' '{ print $5 }' any ideas ;) Regards (6 Replies)
Discussion started by: raid5
6 Replies

6. Shell Programming and Scripting

using perl to print columnwise

Suppose we have two files one file FAAA_HUMAN.input1 2 5 7 11 and another file FAAA_HUMAN.output M*0.0540*0.0039*0.2212*0.0082*0.0020*0.0137*0.0028*0.0029*0.2198*0.0104*0.0889*0.0282*0.0049*0.0804*0.1743*0.0215*0.0531*0.0071*0.0007*0.0021*0.7270*2.5000*... (4 Replies)
Discussion started by: cdfd123
4 Replies

7. Shell Programming and Scripting

perl with two files and print

Suppose u have two files one file >hi|23433|sp|he is RAJ<space>>hi|23333|df|He is HUMAN<space>>hi|222|gi|howru|just WOWHEISWONDERFUL >hi|25559|gs|heisANUJ<space>>hi|2232|sp|he is fool SKSIKSIKSLKSSLLS Another file HUMAN so output wil be ...if the list contain HUMAN only take it... (1 Reply)
Discussion started by: cdfd123
1 Replies

8. Shell Programming and Scripting

using perl or awk to print output

suppose u have file File A A -> G C->D A -> R P->A File B A=1 C=2 D=3 E=4 F=5 G=6 H=7 I=8 K=9 L=10 M=11 (5 Replies)
Discussion started by: cdfd123
5 Replies

9. Shell Programming and Scripting

Perl: Search and print help

Hi all, I need a bit of help with a perl script, I have a file containing lines that look like: M1 (Agnd Agnd ibias_gnP Agnd) nch l=250.0n w=10u m=1 ad=2.5e-12 \ as=2.5e-12 pd=20.5u ps=20.5u nrd=0.025 nrs=0.025 sa=2.5e-07 \ sb=2.5e-07 M21 (Agnd VSSabc Agnd Agnd) nch... (3 Replies)
Discussion started by: Crypto
3 Replies

10. UNIX for Dummies Questions & Answers

perl print command

Hi, I am running this command in perl print "this is a test message @1.00 pm\n" but the output i am getting as this is a test message .00 pm pls help, how to get the proper output (2 Replies)
Discussion started by: vasikaran
2 Replies
Login or Register to Ask a Question