How to get the right text with bash

I am new in bash scripts. I try to make script who check devices ( tp-link, cisco, lynksys... ) to connect via telnet and get some info.

So the first script work with no problem:

#!/bin/sh
NOW=$(date +"%m-%d-%Y")
HOST='IPADDRES'
USER='USER'
PASSWD='PASSWORD'
CMD='enable'
CMD2='show mac address-table'
CMD3='terminal length 0'

(echo "$HOST - " ; (
    echo open "$HOST"
    sleep 1
    echo "$USER"
    sleep 1
    echo "$PASSWD"
    sleep 1
    echo "$CMD"
    sleep 1
    echo "$CMD3"
    sleep 1
    echo "$CMD2\n"
    sleep 30
    echo "exit"
    ) | telnet   ) > name.$NOW.txt

When I make: cat name.date.txt I get this resulst


hostname1 -
telnet> Trying hostname1...
Connected to hostname1.
Escape character is '^]'.
User Access Verification

Username: 
Password:








                 Welcome to BDCOM P3310C EPON OLT




hostname1>enable
hostname1#terminal length 0
hostname1#show mac address-table
        Mac Address Table (Total 311)
------------------------------------------

Vlan    Mac Address       Type       Ports
----    -----------       ----       -----
All     8479.735b.9132    STATIC     CPU
300     44d9.e776.d8b7    DYNAMIC    g0/3
300     4c5e.0cff.6dea    DYNAMIC    g0/3
300     d4ca.6d9e.3280    DYNAMIC    g0/3

So I need export just this line 1 2 and 4 its like this:

300     44d9.e776.d8b7    DYNAMIC    g0/3
300     4c5e.0cff.6dea    DYNAMIC    g0/3
300     d4ca.6d9e.3280    DYNAMIC    g0/3

I don`t need anything before "Vlan Mac Address Type Ports"

So I make this script:

cat name.date.txt | awk '$1<"ALL"{print $1"    ",$2"    ",$3"  ",$4}'

And I get this result:

hostname1    -














             -----------------------------

----     -----------     ----   -----
300     44d9.e776.d8b7     DYNAMIC   g0/3
300     0002.9b80.7f28     DYNAMIC   g0/3
300     0002.9b65.7b66     DYNAMIC   g0/3
300     4c5e.0cff.6dea     DYNAMIC   g0/3
300     d4ca.6d9e.3280     DYNAMIC   g0/3

Who to fix ?

I just export file only this info:

300     44d9.e776.d8b7     DYNAMIC   g0/3
300     0002.9b80.7f28     DYNAMIC   g0/3
300     0002.9b65.7b66     DYNAMIC   g0/3
300     4c5e.0cff.6dea     DYNAMIC   g0/3
300     d4ca.6d9e.3280     DYNAMIC   g0/3

If will be better if I can export to table or csv

Thanks for answers


Assuming you want to print lines from where $1 is equal to All until the end of the file using awk:

awk '$1=="All" {p=1; next} p' name.date.txt

(omit the next if you want to include the matching line).

To make the output comma separated, set the output field separator to "," and force reconstruction of the record by evaluating $1=$1 for example:

awk '$1=="All" {p=1; OFS=","; next} p {$1=$1; print}' name.date.txt

There are several options, but you haven't defined your clear criteria - so I'm mostly guessing here.

If the output you need is consistently in the bottom of your file, you can use tail to get the last X lines, e.g.:

tail -n 6 name.date.txt 

... will give you the last 6 lines (the table).

Vlan    Mac Address       Type       Ports
----    -----------       ----       -----
All     8479.735b.9132    STATIC     CPU
300     44d9.e776.d8b7    DYNAMIC    g0/3
300     4c5e.0cff.6dea    DYNAMIC    g0/3
300     d4ca.6d9e.3280    DYNAMIC    g0/3

Another option is to use grep to search for a pattern, e.g.:

grep 'STATIC\|DYNAMIC' name.date.txt

... will match those lines that include either the word STATIC or DYNAMIC.

All     8479.735b.9132    STATIC     CPU
300     44d9.e776.d8b7    DYNAMIC    g0/3
300     4c5e.0cff.6dea    DYNAMIC    g0/3
300     d4ca.6d9e.3280    DYNAMIC    g0/3