using perl or awk to print output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using perl or awk to print output
# 1  
Old 05-18-2009
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
N=12
P=13
Q=14
R=15
S=16
T=17
V=18
W=19
Y=20

From File A and File B
output shud be such that

first one (A)shud have -1 and second one (G) shud have 1
so that
A -> G 1:-1 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0

C->D 1:0 2:0 3:-1 4:1 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0

so on......

How can we do that using perl or awk??
# 2  
Old 05-18-2009
you are already exposed to perl/awk previously so your "newbie" license is already expired. what have you got so far?
# 3  
Old 05-18-2009
tried using perl

Tried using perl

Code:
!/usr/bin/perl
open IN,"FILE A";
while($line=<IN>)
{

  @free=split(/->/, $line);
  $free1=$free[0];
  $free2=$free[1];
  chomp($free1);
  chomp($free2);
  #print $free1." ".$free2."\n";
}

open IN,"FILEB";
$line=<IN>;
$j=0;

while($line=<IN>)
 {
  @number=split('=', $line);

print $number[0]."  ".$number[25]."\n";

  for($i=0;$i<25;$i++)
    {
      $darray[$j][$i]=$number[$i];
     #print $darray[$j][$i]." "; 
    }
  $j++;
}
print "\n";
close (IN);

What can be the next unable to do that ???
# 4  
Old 05-19-2009
perl:

Code:
my %hash;
while(<DATA>){
	chomp;
	my @tmp=split("=",$_);
	$hash{$tmp[0]}=$tmp[1];
}
open $fh,"<","a.spl";
while(<$fh>){
	chomp;
	/([A-Z])\s*->\s*([A-Z])/;
	print $_;
	for(my $i=1;$i<=20;$i++){
		if($i == $hash{$1}){
			print " ",$i,":",-1;
		}
		elsif($i == $hash{$2}){
			print " ",$i,":",1;
		}
		else{
			print " ",$i,":",0;
		}
	}
	print "\n";
}
__DATA__
A=1
C=2
D=3
E=4
F=5
G=6
H=7
I=8
K=9
L=10
M=11
N=12
P=13
Q=14
R=15
S=16
T=17
V=18
W=19
Y=20

# 5  
Old 05-19-2009
some interpretation in results

Quote:
Originally Posted by summer_cherry
perl:

Code:
my %hash;
while(<DATA>){
	chomp;
	my @tmp=split("=",$_);

	$hash{$tmp[0]}=$tmp[1];
}
open $fh,"<","a.spl";
while(<$fh>){
	chomp;
	/([A-Z])\s*->\s*([A-Z])/;
	print $_;
	for(my $i=1;$i<=20;$i++){
		if($i == $hash{$1}){
			print " ",$i,":",-1;
		}
		elsif($i == $hash{$2}){
			print " ",$i,":",1;
		}
		else{
			print " ",$i,":",0;
		}
	}
	print "\n";
}
__DATA__
A=1
C=2
D=3
E=4
F=5
G=6
H=7
I=8
K=9
L=10
M=11
N=12
P=13
Q=14
R=15
S=16
T=17
V=18
W=19
Y=20


Hello Summer cherry ,
After running this perl command

The result dispalyed is
A->G 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
C->D 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
A->R 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
P->A 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0

But the output should be
in case of A->G first one (A)shud have -1 and second one (G) shud have 1 and .then in case of P->A P have -1 nad A have 1.....and so on....

A->G 1:-1 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
C->D 1:0 2:0 3:-1 4:1 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
A->R 1:-1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:1 16:0 17:0 18:0 19:0 20:0
P->A 1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:-1 14:0 15:0 16:0 17:0 18:0 19:0 20:0

thanks..

Last edited by cdfd123; 05-19-2009 at 01:51 AM.. Reason: some more added
# 6  
Old 05-19-2009
Quote:
Originally Posted by cdfd123
...
After running this perl command

The result dispalyed is
A->G 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
C->D 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
A->R 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
P->A 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
...
Nope, that's wrong. The output is as expected:

Code:
$ 
$ cat a.spl
A->G       
C->D       
A->R       
P->A       
$          
$ cat test_scr.pl
#!/usr/bin/perl -w

my %hash;
while(<DATA>){
        chomp;
        my @tmp=split("=",$_);
        $hash{$tmp[0]}=$tmp[1];
}                              
open $fh,"<","a.spl";          
while(<$fh>){                  
        chomp;                 
        /([A-Z])\s*->\s*([A-Z])/;
        print $_;                
        for(my $i=1;$i<=20;$i++){
                if($i == $hash{$1}){
                        print " ",$i,":",-1;
                }                           
                elsif($i == $hash{$2}){     
                        print " ",$i,":",1; 
                }                           
                else{
                        print " ",$i,":",0;
                }
        }
        print "\n";
}
__DATA__
A=1
C=2
D=3
E=4
F=5
G=6
H=7
I=8
K=9
L=10
M=11
N=12
P=13
Q=14
R=15
S=16
T=17
V=18
W=19
Y=20
$
$ perl test_scr.pl
A->G 1:-1 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
C->D 1:0 2:-1 3:1 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
A->R 1:-1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:1 16:0 17:0 18:0 19:0 20:0
P->A 1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:-1 14:0 15:0 16:0 17:0 18:0 19:0 20:0
$
$

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

Print commas between awk output

When I output fields 1 2 4 5 & 6, I would like to have a comma between them but I am beating my head against the wall to get it to work. Any help is appreciated sed 's/]*,]*/,/g' file1 > file1.$$ && awk -F, 'FNR==NR{f2=$1 $2 $4 $5 $6;next} FNR==1{print $0, "CDP NE Hostname,CDP NE IP,Remote... (6 Replies)
Discussion started by: dis0wned
6 Replies

2. Shell Programming and Scripting

Need help in solving to obtain desired print output using awk or perl or any commands, Please help!!

I have an file which have data in lines as follows ad, findline=24,an=54,ab=34,av=64,ab=7989,ab65=34,aj=323,ay=34,au=545,ad=5545 ab,abc,an10=23,an2=24,an31=32,findline=00,an33=23,an32=26,an40=45,ac23=5,ac=87,al=76,ad=26... (3 Replies)
Discussion started by: deepKrish
3 Replies

3. Shell Programming and Scripting

awk print output problem

Hello friends, I have written a script and i need to add some part into it so that i could print out more results depending on more conditions, This is the core part of the script which does the actual work: echo "$j" && nawk -v stat=$2 'NR==FNR &&... (1 Reply)
Discussion started by: EAGL€
1 Replies

4. UNIX for Dummies Questions & Answers

Any awk one liner to print df output?

Hi, OS = Solaris Can anyone advise if there is a one liner to print specific output from a df -k output? Running df from a command line, it sometimes gives me 2 lines for some volume. By re-directing the output to a file, it always gives 1 line for each. Below is an example output,... (4 Replies)
Discussion started by: newbie_01
4 Replies

5. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

6. UNIX for Dummies Questions & Answers

Strange perl print output behaviour

Hi, Today I have found the following case in perl: print "length:$lengths\tsum:". $count{$lengths}+$count_pair{$lengths}."\tindi:$count{$lengths}\t$count_pair{$lengths}\n";This give output as That means the first part of print is not printing. Only the values after the additions are printed.... (5 Replies)
Discussion started by: gvj
5 Replies

7. Shell Programming and Scripting

Perl script to parse output and print it comma separated

I need to arrange output of SQL query into a comma separated format and I'm struggling with processing the output... The output is something like this: <Attribute1 name><x amount of white spaces><Atribute value> <Attribute2 name><x amount of white spaces><Atribute value> <Attribute3... (2 Replies)
Discussion started by: Juha
2 Replies

8. UNIX for Dummies Questions & Answers

awk {print $NF} output??

Hi, I am trying to debug an old script and have found the problem lies within this function: isIdoc() { # validate the file type fileType=`file $1 | awk '{print $NF}'` && echo 0 || echo 1 } My question is, how can I determine what is in the variable $fileType ? The program is... (1 Reply)
Discussion started by: vervette
1 Replies

9. Shell Programming and Scripting

Net::SSH::Perl ...... how to print the output in a proper format

Hi Guys, my $cmd = "ls -l"; #........ {or let it be as # my $cmd= "ls"; } my $ssh = Net::SSH::Perl->new($host); $ssh->login($user, $pass); my($stdout, $stderr, $exit) = $ssh->cmd("$cmd"); print $stdout; the script works fine, but i am unable to see the output getting displayed in a... (7 Replies)
Discussion started by: gsprasanna
7 Replies

10. Shell Programming and Scripting

using awk to search and print output

suppose i have one file file A 18 24 30 35 38 45 55 Another file file B 08_46 A 16 V -0.36 0.23 E : 1.41 08_46 A 17 D -1.04 0.22 E : 0.84 08_46 A 18 Q -0.49 0.12 E : 0.06 08_46 A 19 G 0.50 0.14 E : 0.05 08_46 A 20 V ... (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question