LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-21-2008, 09:52 AM   #1
seefor
Member
 
Registered: Mar 2006
Posts: 34

Rep: Reputation: 15
Trying to edit a file via Command line or script


Hello all, I'm new to shell scripting.
I wrote a Shell/Expect script to telnet into my devices and run a command debug dev -m. Below is the output of that command that shows all the cards and their serial number:
Code:
spawn telnet 192.168.1.118
Trying 192.168.1.118...
Connected to 192.168.1.118.
Escape character is '^]'.

login: admin
password: 
NewYork.MON1.1M> debug dev -m
      Configuration    Status
Slot  Model            Model            Version    Serial Number
------------------------------------------------------------------------------
M* 1  sys-a12-std      sys-a12-std      v7-0-5     0B0000000F
   2  atm-m11-1a1      atm-m11-1a1      v7-0-5     0B0000000F
   3  tmx-m12-tnl2     tmx-m12-tnl2     v7-0-5     0B0000000F
   4  enc-s11-std-d    enc-s11-std-d    v7-0-5     0B0000000F

NewYork.MON1.1M> spawn telnet 192.168.1.119
Trying 192.168.1.119...
Connected to 192.168.1.119.
Escape character is '^]'.

login: admin
password: 
NewYork.EDGE1a.1M> debug dev -m
      Configuration    Status
Slot  Model            Model            Version    Serial Number
------------------------------------------------------------------------------
M* 1  sys-a12-std      sys-a12-std      v7-0-4     0B0000000F
   3  atm-m11-1a1      atm-m11-1a1      v7-0-4     0B0000000F
   8  tmx-m12-tnl2     tmx-m12-tnl2     v7-0-4     0B0000000F
I was wondering if anyone have an idea of changing the output the file to look something like this:
Code:
NewYork.EDGE1a.1M	M* 1  sys-a12-std      sys-a12-std      v7-0-4     0B0000000F
NewYork.EDGE1a.1M	   3  atm-m11-1a1      atm-m11-1a1      v7-0-4     0B0000000F
NewYork.EDGE1a.1M	8  tmx-m12-tnl2     tmx-m12-tnl2     v7-0-4     0B0000000F
Here is the expect script:
Code:
set hostname [lrange $argv 0 0]
spawn telnet $hostname
expect "login";
send "admin\n";
expect "password:";
send "password\n";
expect ">";
send "debug dev -m";
I have a few hundred of these devices all with different amount of cards in each slot.

Thanks a million for any input.
SeeFor
 
Old 10-21-2008, 10:09 AM   #2
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
Take a look at awk/gawk for text processing.
 
Old 10-21-2008, 11:51 AM   #3
seefor
Member
 
Registered: Mar 2006
Posts: 34

Original Poster
Rep: Reputation: 15
openSauce thanks for the heads up and awk. It's a very powerful program.

So here is what I found when I run the following:
Code:
awk '/v7/ {print $1,$2,$3,$4,$5, $6} /1M/ {print $1}' test | more
Output:
Quote:
NewYork.MON1.1M>
M* 1 sys-a12-std sys-a12-std v7-0-5 0B0000000F
atm-m11-1a1 atm-m11-1a1 v7-0-5 0B0000000F
tmx-m12-tnl2 tmx-m12-tnl2 v7-0-5 0B0000000F
enc-s11-std-d enc-s11-std-d v7-0-5 0B0000000F
NewYork.MON1.1M>
NewYork.EDGE1a.1M>
M* 1 sys-a12-std sys-a12-std v7-0-4 0B0000000F
atm-m11-1a1 atm-m11-1a1 v7-0-4 0B0000000F
tmx-m12-tnl2 tmx-m12-tnl2 v7-0-4 0B0000000F
NewYork.EDGE1a.1M>
Is their a way to process it by removing the Trailing NewYork.MON1.1M> and make it look something like:
Quote:
NewYork.EDGE1a.1M M* 1 sys-a12-std sys-a12-std v7-0-4 0B0000000F
NewYork.EDGE1a.1M 3 atm-m11-1a1 atm-m11-1a1 v7-0-4 0B0000000F
NewYork.EDGE1a.1M 8 tmx-m12-tnl2 tmx-m12-tnl2 v7-0-4 0B0000000F
Again thanks for any help that you can prvode.
SeeFor
 
Old 10-21-2008, 12:10 PM   #4
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
You can have variables in awk programs; what you want is something like

Code:
awk '/v7/ {print prefix, $1,$2,$3,$4,$5, $6} /1M/ {prefix=$1}' test
You can also get rid of the trailing '>' character using the awk substr function if you want.

cheers,

OS
 
Old 10-21-2008, 12:39 PM   #5
seefor
Member
 
Registered: Mar 2006
Posts: 34

Original Poster
Rep: Reputation: 15
Again openSauce thanks a million

When I run the command you provided
Code:
awk '/v7/ {print prefix, $1,$2,$3,$4,$5,$6} /1M/ {prefix=$1}' test
Results:
Quote:
NewYork.EDGE1a.1M M* 1 sys-a12-std sys-a12-std v7-0-4 0B0000000F
ewYork.EDGE1a.1M 3 atm-m11-1a1 atm-m11-1a1 v7-0-4 0B0000000F
ewYork.EDGE1a.1M 8 tmx-m12-tnl2 tmx-m12-tnl2 v7-0-4 0B0000000F

I also tried the substr:
Quote:
awk '/v7/ {print substr(prefix,1,17) $1,$2,$3,$4,$5,$6} /1M/ {prefix=$1}' test
That didn't work too well, the problem is the length of the names are all different lengths
Example:
NewYork.EDGE1a.1M
NewYork.EDGE101a.1M
SanFrancisco.EDGE101a.1M
So it will remove the .1M on the second one and 01a.1M on the third.

Again thanks for all the help, I loving Linux scripting.
SeeFor
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script read line from file, use it in command DiGiGoth Programming 5 08-31-2008 11:08 AM
Random file lines directed to a new file. In script an error. In command line no err leventis Programming 1 09-28-2006 07:16 AM
command line edit -- global find/replace on text file w/o going into vi car182 Linux - Newbie 4 05-25-2006 05:42 PM
edit wav files via command line in a script legolin Linux - Software 4 12-21-2005 10:09 AM
how to edit a file when not in X (ie from the command line) ludwig W Linux - Newbie 12 04-22-2003 04:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration