Grep in line use perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep in line use perl
# 1  
Old 01-23-2013
Grep in line use perl

Hi,

i want to create perl script to telnet and print the output. :

This is the script that i have so far :

Code:
#!/usr/bin/perl

use lib '/usr/lib/perl5/5.14';
use Telnet ();

$target = "192.168.5.1";
$user = "root";
$passwd = "admin123";
print "####################################################################################\n";  

$t = new Net::Telnet (Timeout => 20);
eval { $t->open($target) };
print "Logging in to $target!\n";
$t->login($user, $passwd);
  
print "Waiting for prompt!\n";
$t->waitfor('/$/');
my $cmd_string = "/interface/wifi-1-2/show Clients";
@cell_data_sector1 = $t->cmd($cmd_string);

eval { $t->close($target) };

if ($debug =~ /-d/) { 
    print @cell_data_sector1; 
}

print @cell_data_sector1;

And this is the output :
Code:
SS-ID VLAN MAC               TIME IP                 RSSI MODE  UAPSD BW GI WMOS DHCP       IDENTITY
----- ---- ---               ---- --                 ---- ----  ----- -- -- ---- ----       --------
1-1   0    C4:46:19:75:C1:55 23m  192.168.5.253      -24  bgn   no    20 S  4.9  ack*
1-2   0    5C:57:C8:69:8C:1E 3s   192.168.5.254      -38  bg    no    20 L  4.8  ack*

But actually after this i want to continue to another command and print, but in another command i need to grep the output based on ssid, for example i need to grep "C4:46:19:75:C1:55" and the output will be "1-1"

I have try with this code but still doesn't work.

Code:
    my @macall = qw(print @cell_data_sector1);
    foreach $line (@macall) {
        if ($line =~ /C4:46:19:75:C1:55:\s+(\S+)/) {
                $id = $1;
            }
        }
      print "ID = $id";
      return (0);

Anybody can help me?
# 2  
Old 01-23-2013
change line $id = $1; to:
Code:
$id=$line;
$id=~s/\s.*//;

# 3  
Old 01-23-2013
do u mean like this ?

Code:
    my @macall = qw(print @cell_data_sector1);
    foreach $line (@macall) {
        if ($line =~ /C4:46:19:75:C1:55:\s+(\S+)/) {
                $id=$line;
                $id=~s/\s.*//;
            }
        }
      print "ID = $id";
      return (0);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep -q not works with multiples grep in same line

Hi, I'm trying to make a grep to see if exists occurrences with a sentence like these: grep -qi "message" file0 | grep -i $date | grep -vi "exception" echo $? 1 If I execute without -q modifier I can find occurrences. Someone could help me please? Thanks and sorry for my English! (1 Reply)
Discussion started by: mierdatuti
1 Replies

2. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

3. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

4. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

5. Shell Programming and Scripting

How to Grep than scan line below grep pattern

Hello Colleagues, I have a file that looks like below. 6-12731913-12731913 9230760143480 410018547148230 20131002193434+0500 20131002193434+0500 ;20131002T161031000-10.50.241.21-21912131-1419034760, ver: 0 20131009 92220056296730 CC0P abc Core_Context_R1A SMS 6-12726796-12726796... (14 Replies)
Discussion started by: umarsatti
14 Replies

6. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

7. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

8. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

9. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies

10. UNIX for Dummies Questions & Answers

Grep or other ways to output line above and/or below searched line

Hi all, Would like to know how I could search for a string 'xyz' but have the output show the line plus the line above and/or below all lines found. eg. search for xyz from file containing: abc 12345 asdf xyz asdfds wwwww kjkjkj ppppp kkkxyz eeee zzzzz and the output to... (2 Replies)
Discussion started by: sammac
2 Replies
Login or Register to Ask a Question