0

I'm using awk to organize the information in a text file (It contains CDP neighbors information of a switch taken from terminal) , the file looks like this:

Device ID Local Intrfce
BIOTERIO Gig 1/0/6
N7K-LAN(JAF1651ANDL)
Gig 1/0/1 145
LAB_PESADO Gig 1/0/11
Arquitectura_Salones
Gig 1/0/9 147
CIVIL_253 Gig 1/0/4
Arquitectura Gig 1/0/3
ING_CIVIL_DIR Gig 1/0/10
ING_CIVIL Gig 1/0/7
Ingenieria_Posgrado
Gig 1/0/8 132
Biblio_Barragan Gig 1/0/2
Electronica_Edif_3
Gig 1/0/5 173
Barragan_3750>exit
Connection closed by foreign host.
]0;cesar@cesar-HP-Pavilion-15-Note

And i want it to looks like this:

Device ID Local Intrfce
BIOTERIO Gig 1/0/6
N7K-LAN(JAF1651ANDL) Gig 1/0/1 145
LAB_PESADO Gig 1/0/11
Arquitectura_Salones Gig 1/0/9 147
CIVIL_253 Gig 1/0/4
Arquitectura Gig 1/0/3
ING_CIVIL_DIR Gig 1/0/10
ING_CIVIL Gig 1/0/7
Ingenieria_Posgrado Gig 1/0/8 132
Biblio_Barragan Gig 1/0/2
Electronica_Edif_3 Gig 1/0/5 173
Barragan_3750>exit Connection closed by foreign host.
]0;cesar@cesar-HP-Pavilion-15-Note

The awk script that i'm using is this:

awk '{printf "%s%s", $0, (length($1) > 16) ? OFS : ORS}' CDPyPuerto.dat > TablaCDP.dat

It looks for those lines in which the first field has more than 16 characters and replace the break line with a space.

When i run the awk script the result looks like this:

Device ID Local Intrfce 
BIOTERIO Gig 1/0/6 
N7K-LAN(JAF1651ANDL)
  Gig 1/0/1 172 
LAB_PESADO Gig 1/0/11 
Arquitectura_Salones
  Gig 1/0/9 176 
CIVIL_253 Gig 1/0/4 
Arquitectura Gig 1/0/3 
ING_CIVIL_DIR Gig 1/0/10 
ING_CIVIL Gig 1/0/7 
Ingenieria_Posgrado
  Gig 1/0/8 159 
Biblio_Barragan Gig 1/0/2 
Electronica_Edif_3
  Gig 1/0/5 141 
Barragan_3750>exit
Connection closed by foreign host.

]0;cesar@cesar-HP-Pavilion-15-Note

I have noticed that if i open the orginial text file (CDPyPuertos.dat) with gedit and edit something (e.g. add an space at the end of the file or change some character) the awk script works fine, but all this is into an Expect script, so it should to be automatic, also i've tried to change the format of the file from ascci to UTF-8 but it didn't work, the same if i use different extensions (.txt .dat .dos)

Does anybody knows why is this happening?

Thanks by advance

Update:

If i type: cat -et CDPyPuerto.dat

The result is this:

Device ID Local Intrfce$
BIOTERIO Gig 1/0/6$
N7K-LAN(JAF1651ANDL)^M$
Gig 1/0/1 165$
LAB_PESADO Gig 1/0/11$
Arquitectura_Salones^M$
Gig 1/0/9 173$
CIVIL_253 Gig 1/0/4$
Arquitectura Gig 1/0/3$
ING_CIVIL_DIR Gig 1/0/10$
ING_CIVIL Gig 1/0/7$
Ingenieria_Posgrado^M$
Gig 1/0/8 152$
Biblio_Barragan Gig 1/0/2$
Electronica_Edif_3^M$
Gig 1/0/5 133$
Barragan_3750>exit^M$
Connection closed by foreign host.^M$
^[]0;cesar@cesar-HP-Pavilion-15-Note$

After i open CDPyPuerto.dat with gedit and edit something, all the ^M$ turns into $.

How can i do it automatically?

  • 2
    Does your input file have Windows-style CR-LF line endings (check for example with cat -et CDPyPuerto.dat) – steeldriver Jan 31 '17 at 16:21
2

The way I'd solve this is to remove the CR characters (represented as ^M in the output from cat -et):

tr -d '\r' < CDPyPuerto.dat |
    awk '{printf "%s%s", $0, (length($1) > 16) ? OFS : ORS}' > TablaCDP.dat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.