0

I have a text file like this:

sn,device_id,ip_address
 P7K08UQ         , BIOTERIO, 148.111.83.177
 H006K022        , N7K-LAN(JAF1651ANDL), 148.111.4.111
 FNS174002FT     , LAB_PESADO, 148.111.131.188
 FNS14420533     , Arquitectura_Salones, 148.111.135.44
 H006K021        , CIVIL_253, 148.111.132.666
 H006K083        , Arquitectura, 148.111.134.111
 H006K032        , ING_CIVIL, 148.111.133.777
 FNS16361SG0     , ING_CIVIL_DIR, 148.111.4.188
 H006K040        , Ingenieria_Posgrado, 148.111.137.253
 00000MTC1444080Z, Biblio_Barragan, 148.111.136.61
 FNS11190FLE     , Electronica_Edif_3, 148.111.130.253

And another one like this:

SN: FDO1129Z9ZJ
Barragan_3750
IP address: 148.111.4.122

I would like to add the second file's info into the first one using awk like this:

sn,device_id,ip_address
     P7K08UQ         , BIOTERIO, 148.111.83.177
     H006K022        , N7K-LAN(JAF1651ANDL), 148.111.4.111
     FNS174002FT     , LAB_PESADO, 148.111.131.188
     FNS14420533     , Arquitectura_Salones, 148.111.135.44
     H006K021        , CIVIL_253, 148.111.132.666
     H006K083        , Arquitectura, 148.111.134.111
     H006K032        , ING_CIVIL, 148.111.133.777
     FNS16361SG0     , ING_CIVIL_DIR, 148.111.4.188
     H006K040        , Ingenieria_Posgrado, 148.111.137.253
     00000MTC1444080Z, Biblio_Barragan, 148.111.136.61
     FNS11190FLE     , Electronica_Edif_3, 148.111.130.253
     FDO1129Z9ZJ     , Barragan_3750, 148.111.4.122
2

The main job is to combine the three lines in the "second" file into a single line. Assuming that the file format will be the one that you have given in the example, the following awk would do that trick

awk '{ printf "%s", $NF; if (NR % 3 == 0) print ""; else printf "," }' second_file

Now the second part is adding to file1, that could be accomplished by a simple redirection >>

The complete command then would be

 awk '{ printf "%s", $NF; if (NR % 3 == 0) print ""; else printf "," }' second_file >> first_file

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.