LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Shell script - how to show a specific line of a text file (http://www.linuxquestions.org/questions/linux-software-2/shell-script-how-to-show-a-specific-line-of-a-text-file-236206/)

davi_cabral 09-28-2004 09:48 AM

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 :newbie: :Pengy: :Pengy:

jmings 09-28-2004 10:59 AM

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

davi_cabral 09-28-2004 01:31 PM

Ok! It appears good!
Only a doubt: what does it mean the "^" in the grep command?

chrism01 09-28-2004 01:39 PM

In that context, '^' anchors the pattern match to the start of the input string


All times are GMT -5. The time now is 12:40 AM.