1

I'm using awk to edit a text file that looks like this :

Device_ID Local Intrfce
CS_Quimicas_Alim Gig 3/13
computacion_seg_23
Gig 3/14 158 T S
MAUSALUD_B Gig 1/40
MULTIAULAS_PRINCIPAL
Gig 3/22 173 S I
S24-Computacion.cs.buap.mx
Gig 3/20 179 S I
N7K-LAN(JAF1651ANDL)
Gig 1/40 179 R S
N7K-LAN(JAF1651ANDL)
Gig 1/46 179 R S
Electronica_76 Gig 3/24
AGROECOLOGIA Gig 3/10
N7K-NK7-SERVERS(JAF1651ANDL)
Gig 1/35 159 R S
SIU_108 Gig 1/17
Bib-Salud Gig 1/40

It supposed to be a chart but when Device_ID length is to large the rest of the fields goes to the next line, i would like to edit the file so it looks like this:

Device_ID Local Intrfce
CS_Quimicas_Alim Gig 3/13
computacion_seg_23 Gig 3/14 158 T S
MAUSALUD_B Gig 1/40
MULTIAULAS_PRINCIPAL Gig 3/22 173 S I
S24-Computacion.cs.buap.mx Gig 3/20 179 S I
N7K-LAN(JAF1651ANDL) Gig 1/40 179 R S
N7K-LAN(JAF1651ANDL) Gig 1/46 179 R S
Electronica_76 Gig 3/24
AGROECOLOGIA Gig 3/10
N7K-NK7-SERVERS(JAF1651ANDL) Gig 1/35 159 R S
SIU_108 Gig 1/17
Bib-Salud Gig 1/40

I'm using an if conditional to check first field length so if it has more than 15 characters it will be printed in terminal.

BEGIN {FS=" "; Dispositivos_largos=0}
    {if (length($1) > 16 )
    {print $1}
    }
END {print "Dispositivos con mas de 16 caracteres en el nombre: "Dispositivos_largos}

But I haven't been able to merge the next line at the end of the line.

Any help?

Thanks by advance.

2 Answers 2

3

Using the length of the first word:

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

or

awk '{
    line = $0
    if (length($1) > 16) {
        getline
        line = line OFS $0
    } 
    print line
}' file
3
  • You would be better off fixing the process that writes the file incorrectly. Do you have control over that? Jan 25, 2017 at 18:07
  • Thanks it worked perfectly, yes I'm catching the information from the terminal with the instruction "log_file -noappend CDP_$DATE.txt" but it doesn't keep the structure of the chart, actually the text file that i'm getting has extra information that im deleting using "tail -n+5 CDP.txt > CDP2.txt\r" and "head -n -4 CDP2.txt > CDP.txt\r" It would be better to catch just the cdp chart in the first place. all this is in a Expect script that saves CDP neighbors information. Do you know how can i get just the chart when i type "show cdp neighbors"? Jan 26, 2017 at 17:39
  • That is a completely separate question Jan 26, 2017 at 17:44
1

Use printf() within if statement to conditionally print the line with or without \n character (line-break)

$ awk '{if(length($1)>15 && $0 !~/Gig/){printf "%s ",$0}else{print $0}}' input.txt                                       
Device_ID Local Intrfce
CS_Quimicas_Alim Gig 3/13
computacion_seg_23 Gig 3/14 158 T S
MAUSALUD_B Gig 1/40
MULTIAULAS_PRINCIPAL Gig 3/22 173 S I
S24-Computacion.cs.buap.mx Gig 3/20 179 S I
N7K-LAN(JAF1651ANDL) Gig 1/40 179 R S
N7K-LAN(JAF1651ANDL) Gig 1/46 179 R S
Electronica_76 Gig 3/24
AGROECOLOGIA Gig 3/10
N7K-NK7-SERVERS(JAF1651ANDL) Gig 1/35 159 R S
SIU_108 Gig 1/17
Bib-Salud Gig 1/40
2
  • thanks, it worked perfectly, unfortunately not always the word Gig will appear, it could be different, thats why i need to check the first word of each line Jan 26, 2017 at 17:32
  • @CesarAlejandroVillegasYepez OK. Would be great if you edited the question and explain what is the general case. If the only difference is the first word, you can edit the code to suit each case. Again, it would be much better if you clarified your question Jan 26, 2017 at 17:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .