How can I remove the line break in a column using Awk?

4

I have something like this:

Arena               Gig1/46       Gig1/0/49
CUCMS4              Gig7/35       eth0
SALUD-3750-P1R4v-dis    
Gig1/7        Gig1/0/12
CUCMS5              Gig7/33       eth0
EMMA_6              Gig1/36       Gig1/0/49

I need an exit like this:

Arena               Gig1/46       Gig1/0/49
CUCMS4              Gig7/35       eth0
SALUD-3750-P1R4v-dis Gig1/7        Gig1/0/12
CUCMS5              Gig7/33       eth0
EMMA_6              Gig1/36       Gig1/0/49

I tried this command but it does not delete the line break:

awk -F, '{gsub("\n","",$1); print}' archivo.txt
    
asked by user76626 07.03.2018 в 00:37
source

2 answers

3

It simply validates if a line contains 3 fields. If not, save the value and print it on the following line:

$ awk 'NF<3{if (linea) {print linea, $0; linea=""; next} linea=$0; next}1' fichero
Arena               Gig1/46       Gig1/0/49
CUCMS4              Gig7/35       eth0
SALUD-3750-P1R4v-dis     Gig1/7        Gig1/0/12
CUCMS5              Gig7/33       eth0
EMMA_6              Gig1/36       Gig1/0/49

This is:

NF<3{                        # si la línea tiene menos de 3 campos
     if (linea) {            # si "linea" está definido
           print linea, $0;  # imprime lo guardado seguido de la línea actual
           linea="";         # resetea la variable "linea"
           next              # salta a la siguiente línea
    }
    linea=$0;                # en caso contrario, guarda la línea actual
    next                     # y salta a la siguiente línea
 }
1                            # si no ha pasado nada de lo anterior,
                             # imprime la línea actual
import this
print(42)
    
answered by 07.03.2018 в 08:55
2

Because awk reads data based on line, you should look for the next line when there are less than three fields. For example:

awk 'NF != 3 { printf "%s", $0; getline; gsub("\n", "") } 1' archivo.txt

Exit:

Arena               Gig1/46       Gig1/0/49
CUCMS4              Gig7/35       eth0
SALUD-3750-P1R4v-dis    Gig1/7        Gig1/0/12
CUCMS5              Gig7/33       eth0
EMMA_6              Gig1/36       Gig1/0/49
    
answered by 07.03.2018 в 02:00