LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 09-28-2004, 09:48 AM   #1
davi_cabral
LQ Newbie
 
Registered: Jul 2003
Location: Rio
Distribution: RedHat 7.2
Posts: 21

Rep: Reputation: 15
Shell script - how to show a specific line of a text file


Hi people.. I have a config file containing MRTG interfaces configuration. There are many of blocks of text like the block below:


### Interface 13 >> Descr: 'Ethernet2/3' | Name: 'Et2/3' | Ip: '10.16.5.250' | Eth: '00-60-83-7f-53-43' ###

Target[loopgersp_13]: 13:xptoandr2d2@loopgersp
SetEnv[loopgersp_13]: MRTG_INT_IP="10.16.5.250" MRTG_INT_DESCR="Ethernet2/3"
MaxBytes[loopgersp_13]: 1250000
Title[loopgersp_13]: Traffic Analysis for 13 -- DRSPMV01
PageTop[loopgersp_13]: <H1>Traffic Analysis for 13 -- DRSPMV01</H1>
<TABLE>
<TR><TD>System:</TD> <TD>DRSPMV01 in Jupiter, ESSP.P</TD></TR>
<TR><TD>Maintainer:</TD> <TD>ciscoworks@jupiter.com.br 55-21-555-6969</TD></TR>
<TR><TD>Description:</TD><TD>LINK TO DOWNTOWN OFFICE</TD></TR>
<TR><TD>ifType:</TD> <TD>ethernetCsmacd (6)</TD></TR>
<TR><TD>ifName:</TD> <TD>Et2/3</TD></TR>
<TR><TD>Max Speed:</TD> <TD>10.0 Mbits/s</TD></TR>
<TR><TD>Ip:</TD> <TD>10.16.5.250 ()</TD></TR>
</TABLE>

I would like to make a script capable to capture de number of Interface on the first lines of the blocks, and the descrition of the interfaces on the "Descrition" line, put it in columns and save on another text file, that I could read with Excel or Open Office, something like below:

13 ; Ethernet2/3 ; LINK TO DOWNTOWN OFFICE
14 ; Ethernet2/4 ; LINK TO COPACABANA OFFICE
15 ; Serial 2/0 ; LINK TO SAO PAULO

and so on...
Help welcome...
Thanks
Davi
 
Old 09-28-2004, 10:59 AM   #2
jmings
Member
 
Registered: Sep 2004
Location: Hemet,California, USA
Distribution: SimplyMEPIS
Posts: 31

Rep: Reputation: 16
Post Shell script for Davi as requested

================== getMRTG.sh =====================
#!/bin/bash

YOURDATA=MRTG.cfg ## Replace with your MRTG configuration file name
OUT=MRTG.csv
>$OUT ## Clear output file

## Gather data lines
grep "^### Interface " "$YOURDATA" >1.tmp
grep "^<TR><TD>Description:</TD><TD>" "$YOURDATA" >2.tmp

## Make sure there are the same number of lines.
LINE1=`cat 1.tmp | wc -l `
LINE2=`cat 2.tmp | wc -l `
if [ $LINE1 -ne $LINE2 ]
then
echo Mis-match number of lines.
exit 1
fi

LINE=0
while [ $LINE -lt $LINE1 ]
do
LINE=`expr $LINE + 1`
INTERFACE=`head -$LINE 1.tmp | tail -1 | cut -d' ' -f3`
PORT=`head -$LINE 1.tmp | tail -1 | cut -d\' -f2`
DESC=`head -$LINE 2.tmp | tail -1 | cut -d'<' -f5 | cut -c4-`
echo "$INTERFACE;$PORT;$DESC" | tee -a $OUT
done
rm [12].tmp
echo "Data is in $OUT"
==============================================================

I tested it on the data segment you supplied and got the results you wanted.

O Bruxo
 
Old 09-28-2004, 01:31 PM   #3
davi_cabral
LQ Newbie
 
Registered: Jul 2003
Location: Rio
Distribution: RedHat 7.2
Posts: 21

Original Poster
Rep: Reputation: 15
Ok! It appears good!
Only a doubt: what does it mean the "^" in the grep command?
 
Old 09-28-2004, 01:39 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
In that context, '^' anchors the pattern match to the start of the input string
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM
Display/Read line 'N' in a text file using script ganninu Linux - Newbie 2 10-13-2003 05:28 AM
how to convert a text file into shell script meng_en Linux - General 9 10-15-2002 10:46 PM


All times are GMT -5. The time now is 05:33 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration