Visit Our UNIX and Linux User Community


How to get lines having a variable in perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get lines having a variable in perl?
# 1  
Old 08-14-2013
IBM How to get lines having a variable in perl?

Hi, This might be simple question. But i couldn't find the answer.
#$ra is having a value which i have got from some other file
I want the first occurrence of the line having the value that is in variable "$ra"
This is what i have tried.
Code:
while ( $iop = <TST>)
{
  if($iop =~ /$ra/)
{
  print "$iop \n";
  last;
  }
}

plz help me.
Thanks in advance

Last edited by Scott; 08-14-2013 at 01:44 PM.. Reason: Plz use code tags!
# 2  
Old 08-14-2013
Must the two lines be identical (in which place you should probably chomp and perform an eq comparison rather than use a regex...)

putting $ra_r=qr($ra); outside the loop and then checking if $iop=~$ra_r would speed up the code slightly (only one regex compilation.

What issue are you seeing?
# 3  
Old 08-14-2013
the vaule of $ra=000000000000096C

Now i want the first line of a file that is having the value 000000000000096C
for example:
Code:
I 000000000000096C 48000000  * 000000000000096C         b         *

Here is the new code i wrote
Code:
33: # printing the iop corresponding to real address
34: open (TST, '<psi.tst');
35: while ( $iop = <TST>)
36: {
37:   if($iop =~ /$ra/){
38:     last;
39:  }
40:  }
41:  print $iop;

ERROR iam getting is :
Code:
Use of uninitialized value in print at psi.pl line 41, <TST> line 51574.


Last edited by twistedpair; 08-15-2013 at 12:19 AM.. Reason: Removed IMG tag; added code tags
# 4  
Old 08-14-2013
Hi,

try this:

INPUT
Code:
hi
line2 has 000000000000096C
line3 is here.

line 5 also has 000000000000096C
line3 twice.

Code:
#!/usr/bin/perl -w
use 5.010;
my $ra="000000000000096C";
say "INPUT IS:$ra";

while (<>){
chomp;
if ( /$ra/ ) {
say;
last;
}
}

# 5  
Old 08-15-2013
HI greet_sed

iam getting some different errors when i tried your code.

Unquoted string "say" may clash with future reserved word at temp2.pl line 9.
No such class say at temp2.pl line 4, near "my say"
syntax error at temp2.pl line 4, near "my say "INPUT IS:$ra""
Execution of temp2.pl aborted due to compilation errors.
# 6  
Old 08-15-2013
Did you add use 5.010 line in your code?

Use print instead of say to avoid errors.

for ex:
Code:
print "$ra\n";
print;

# 7  
Old 08-15-2013
Thanks, i got it where i missed. I just did chomp of the variable. Now its working. I think when i extracted $ra new line character also came. So i removed it. It took almost 4 hours for to do experiments & figure out.
Code:
$ra = substr ($lastword,$n);
print "$lastword";
print "$ra";
chomp($ra);
# printing the iop corresponding to real address
open (TST, '<psi.txt');

while ( $iop = <TST>)
{ # chomp;
  if( $iop =~ /$ra/ ){
      print "$iop";
      last;
  }
}


Last edited by Franklin52; 08-15-2013 at 05:44 AM.. Reason: Please use code tags

Previous Thread | Next Thread
Test Your Knowledge in Computers #1006
Difficulty: Medium
In calculus and most other branches of mathematics beyond practical geometry, angles are universally measured in degrees.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl help - how to assign output of perl to variable

Hi, guys, i have a script i inherited from a coworker but i'm not perl savy. The script works but i would like it to work better. I want to run this command ./ciscomgrtest.pl -r "show version" -h hosts.router and have the script goto each router in the hosts.router file and run the command... (2 Replies)
Discussion started by: whipuras
2 Replies

2. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

3. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

4. Shell Programming and Scripting

Trying to break up a lines in a variable

I was trying to figure out a way to split along a CR in a variable. In the code below, the result for CPU returns CPU=`cat /proc/cpuinfo | grep -i 'model name' |awk -F ':' '{ print $2}'` for echo $CPU AMD Phenom(tm) 9850 Quad-Core Processor AMD Phenom(tm) 9850 Quad-Core Processor... (7 Replies)
Discussion started by: baggins2000
7 Replies

5. Shell Programming and Scripting

perl - how can we name a variable base on value of another variable

Hey all, perl - how can we name a variable base on the value of another variable? for example in ksh/bash we do : export c="100" export x`echo $c`=2000 echo $x100 x100=2000 is it possible to do something similar for perl? I already tried many ways but nothing is working. I am... (3 Replies)
Discussion started by: cacm1975
3 Replies

6. Shell Programming and Scripting

Append variable to only certain lines

Hi There! I'm trying to write a shell script which generates a random hexadecimal number and then appends it to the end of lines XX onwards of a certain file. XX is determined by a certain string on the previous line. Thus for example, if the input file is I search for the string... (3 Replies)
Discussion started by: orno
3 Replies

7. Shell Programming and Scripting

combine two perl lines into a single perl command

Hi Everyone, i have a string 00:44:40 so: $tmp=~ s/://gi; $tmp=~s/({2})({2})({2})/$1*3600+$2*60+$3/e; the output is 2680. Any way to combine this two lines into a single line? Thanks (4 Replies)
Discussion started by: jimmy_y
4 Replies

8. Shell Programming and Scripting

[Perl] Insert lines before lines.

Hi, New problem, or challenge as they prefer in the US. I need to insert some lines in a file before certain other lines. To make it more clear: Original file: aaaa bbbbb ccccc ddddd bbbbb fffff ggggg Now I want to insert the line "NEW_NEW_NEW" when I match "fffff", but I want... (7 Replies)
Discussion started by: ejdv
7 Replies

9. Shell Programming and Scripting

Multiple variable in a variable in Perl

Hi All, I am trying to convert the below Csh while loop into Perl while loop but the problem is that in this csh script, i have 2 variables inside a variable -> $count is a variable {SB$count} as a whole is another variable. Csh is able to assign values to such variable like the below but i do... (3 Replies)
Discussion started by: Raynon
3 Replies

10. Shell Programming and Scripting

lines from file to variable

I have a text file that contains a word on each line. I want to be able to use each word as a variable to be put into a URL. I have been trying to put together a for loop but I am not having much luck. So basically all I am trying to do is for line in ~/desktop/test.txt do curl -s... (4 Replies)
Discussion started by: iCONAN
4 Replies

Featured Tech Videos