Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


  Search this Thread
Old 09-28-2004, 10:48 AM   #1
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: '' | Eth: '00-60-83-7f-53-43' ###

Target[loopgersp_13]: 13:xptoandr2d2@loopgersp
SetEnv[loopgersp_13]: MRTG_INT_IP="" 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>
<TR><TD>System:</TD> <TD>DRSPMV01 in Jupiter, ESSP.P</TD></TR>
<TR><TD>Maintainer:</TD> <TD> 55-21-555-6969</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> ()</TD></TR>

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
15 ; Serial 2/0 ; LINK TO SAO PAULO

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

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

================== =====================

YOURDATA=MRTG.cfg ## Replace with your MRTG configuration file name
>$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 ]
echo Mis-match number of lines.
exit 1

while [ $LINE -lt $LINE1 ]
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
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, 02:31 PM   #3
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, 02:39 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,329

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


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 57 04-17-2016 07:07 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 08:15 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 09:51 PM
Display/Read line 'N' in a text file using script ganninu Linux - Newbie 2 10-13-2003 06:28 AM
how to convert a text file into shell script meng_en Linux - General 9 10-15-2002 11:46 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:31 PM.

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