How to swap certain columns in .csv file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-2
down vote

favorite












I have been searching for an answer, but I have found just messy solutions, I have a .csv file like this:



device_id,ip_address,serial_number
Arq_Laboratorios_EdifB, 148.228.134.000,FOC1518Z1G8
Arquitectura_Dir, 148.228.134.000,FOC1216U13V
Arq.245, 148.228.134.000,FOC1352V3FE
Barragan_3750, 148.228.134.000,FDO1129Z9ZJ


I only need to swap column number 3 and 1 so the file turns into this:



serial_number,device_id,ip_address
FOC1518Z1G8,Arq_Laboratorios_EdifB, 148.228.134.000
FOC1216U13V,Arquitectura_Dir, 148.228.134.000
FOC1352V3FE,Arq.245, 148.228.134.000
FDO1129Z9ZJ,Barragan_3750, 148.228.134.000


I'm pretty sure there should be an easy solution using sed or awk.



UPDATE:
using this awk 'BEGINFS=OFS=","a=$1; $1=$3; $3=a1' dispositivos.csv this is what I've got:



serial_number,ip_address,device_id
, 148.228.134.000, Arq_Laboratorios_EdifB
, 148.228.134.000, Arquitectura_Dir
, 148.228.134.000, Arq.245
, 148.228.134.000, Barragan_3750


I don't know what's happening with serial numbers.










share|improve this question



















  • 2




    your profile shows 16 questions tagged awk.. by now you should be able to at least show your attempt at solving, an easy one as you put it
    – Sundeep
    Mar 30 '17 at 15:46










  • What you want is NOT swapping col3 <-> col1. You need to be really clear what you want.
    – user218374
    Mar 30 '17 at 15:48














up vote
-2
down vote

favorite












I have been searching for an answer, but I have found just messy solutions, I have a .csv file like this:



device_id,ip_address,serial_number
Arq_Laboratorios_EdifB, 148.228.134.000,FOC1518Z1G8
Arquitectura_Dir, 148.228.134.000,FOC1216U13V
Arq.245, 148.228.134.000,FOC1352V3FE
Barragan_3750, 148.228.134.000,FDO1129Z9ZJ


I only need to swap column number 3 and 1 so the file turns into this:



serial_number,device_id,ip_address
FOC1518Z1G8,Arq_Laboratorios_EdifB, 148.228.134.000
FOC1216U13V,Arquitectura_Dir, 148.228.134.000
FOC1352V3FE,Arq.245, 148.228.134.000
FDO1129Z9ZJ,Barragan_3750, 148.228.134.000


I'm pretty sure there should be an easy solution using sed or awk.



UPDATE:
using this awk 'BEGINFS=OFS=","a=$1; $1=$3; $3=a1' dispositivos.csv this is what I've got:



serial_number,ip_address,device_id
, 148.228.134.000, Arq_Laboratorios_EdifB
, 148.228.134.000, Arquitectura_Dir
, 148.228.134.000, Arq.245
, 148.228.134.000, Barragan_3750


I don't know what's happening with serial numbers.










share|improve this question



















  • 2




    your profile shows 16 questions tagged awk.. by now you should be able to at least show your attempt at solving, an easy one as you put it
    – Sundeep
    Mar 30 '17 at 15:46










  • What you want is NOT swapping col3 <-> col1. You need to be really clear what you want.
    – user218374
    Mar 30 '17 at 15:48












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have been searching for an answer, but I have found just messy solutions, I have a .csv file like this:



device_id,ip_address,serial_number
Arq_Laboratorios_EdifB, 148.228.134.000,FOC1518Z1G8
Arquitectura_Dir, 148.228.134.000,FOC1216U13V
Arq.245, 148.228.134.000,FOC1352V3FE
Barragan_3750, 148.228.134.000,FDO1129Z9ZJ


I only need to swap column number 3 and 1 so the file turns into this:



serial_number,device_id,ip_address
FOC1518Z1G8,Arq_Laboratorios_EdifB, 148.228.134.000
FOC1216U13V,Arquitectura_Dir, 148.228.134.000
FOC1352V3FE,Arq.245, 148.228.134.000
FDO1129Z9ZJ,Barragan_3750, 148.228.134.000


I'm pretty sure there should be an easy solution using sed or awk.



UPDATE:
using this awk 'BEGINFS=OFS=","a=$1; $1=$3; $3=a1' dispositivos.csv this is what I've got:



serial_number,ip_address,device_id
, 148.228.134.000, Arq_Laboratorios_EdifB
, 148.228.134.000, Arquitectura_Dir
, 148.228.134.000, Arq.245
, 148.228.134.000, Barragan_3750


I don't know what's happening with serial numbers.










share|improve this question















I have been searching for an answer, but I have found just messy solutions, I have a .csv file like this:



device_id,ip_address,serial_number
Arq_Laboratorios_EdifB, 148.228.134.000,FOC1518Z1G8
Arquitectura_Dir, 148.228.134.000,FOC1216U13V
Arq.245, 148.228.134.000,FOC1352V3FE
Barragan_3750, 148.228.134.000,FDO1129Z9ZJ


I only need to swap column number 3 and 1 so the file turns into this:



serial_number,device_id,ip_address
FOC1518Z1G8,Arq_Laboratorios_EdifB, 148.228.134.000
FOC1216U13V,Arquitectura_Dir, 148.228.134.000
FOC1352V3FE,Arq.245, 148.228.134.000
FDO1129Z9ZJ,Barragan_3750, 148.228.134.000


I'm pretty sure there should be an easy solution using sed or awk.



UPDATE:
using this awk 'BEGINFS=OFS=","a=$1; $1=$3; $3=a1' dispositivos.csv this is what I've got:



serial_number,ip_address,device_id
, 148.228.134.000, Arq_Laboratorios_EdifB
, 148.228.134.000, Arquitectura_Dir
, 148.228.134.000, Arq.245
, 148.228.134.000, Barragan_3750


I don't know what's happening with serial numbers.







text-processing awk sed grep csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 21 at 3:13









Rui F Ribeiro

36.7k1271116




36.7k1271116










asked Mar 30 '17 at 15:36









Cesar Alejandro Villegas Yepez

123212




123212







  • 2




    your profile shows 16 questions tagged awk.. by now you should be able to at least show your attempt at solving, an easy one as you put it
    – Sundeep
    Mar 30 '17 at 15:46










  • What you want is NOT swapping col3 <-> col1. You need to be really clear what you want.
    – user218374
    Mar 30 '17 at 15:48












  • 2




    your profile shows 16 questions tagged awk.. by now you should be able to at least show your attempt at solving, an easy one as you put it
    – Sundeep
    Mar 30 '17 at 15:46










  • What you want is NOT swapping col3 <-> col1. You need to be really clear what you want.
    – user218374
    Mar 30 '17 at 15:48







2




2




your profile shows 16 questions tagged awk.. by now you should be able to at least show your attempt at solving, an easy one as you put it
– Sundeep
Mar 30 '17 at 15:46




your profile shows 16 questions tagged awk.. by now you should be able to at least show your attempt at solving, an easy one as you put it
– Sundeep
Mar 30 '17 at 15:46












What you want is NOT swapping col3 <-> col1. You need to be really clear what you want.
– user218374
Mar 30 '17 at 15:48




What you want is NOT swapping col3 <-> col1. You need to be really clear what you want.
– user218374
Mar 30 '17 at 15:48










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










This should work ok:



awk 'BEGINFS=OFS=","$0=$3 FS $1 FS $21'





share|improve this answer






















  • Updated.Try this one
    – George Vasiliou
    Mar 30 '17 at 16:00










  • It does the same, I think the way I added the serial numbers is affecting. I'm using a for loop to find the serial number from "show version" and add it in a given line of the .csv file. I think so, cause the first line does change.
    – Cesar Alejandro Villegas Yepez
    Mar 30 '17 at 16:05






  • 1




    I've used "dos2unix dispositivos.csv" and then it worked. Thanks again!
    – Cesar Alejandro Villegas Yepez
    Mar 30 '17 at 16:55

















up vote
2
down vote













perl -F, -ple '$_ = join ",", @F[2,0,1]'





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f354864%2fhow-to-swap-certain-columns-in-csv-file%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    This should work ok:



    awk 'BEGINFS=OFS=","$0=$3 FS $1 FS $21'





    share|improve this answer






















    • Updated.Try this one
      – George Vasiliou
      Mar 30 '17 at 16:00










    • It does the same, I think the way I added the serial numbers is affecting. I'm using a for loop to find the serial number from "show version" and add it in a given line of the .csv file. I think so, cause the first line does change.
      – Cesar Alejandro Villegas Yepez
      Mar 30 '17 at 16:05






    • 1




      I've used "dos2unix dispositivos.csv" and then it worked. Thanks again!
      – Cesar Alejandro Villegas Yepez
      Mar 30 '17 at 16:55














    up vote
    1
    down vote



    accepted










    This should work ok:



    awk 'BEGINFS=OFS=","$0=$3 FS $1 FS $21'





    share|improve this answer






















    • Updated.Try this one
      – George Vasiliou
      Mar 30 '17 at 16:00










    • It does the same, I think the way I added the serial numbers is affecting. I'm using a for loop to find the serial number from "show version" and add it in a given line of the .csv file. I think so, cause the first line does change.
      – Cesar Alejandro Villegas Yepez
      Mar 30 '17 at 16:05






    • 1




      I've used "dos2unix dispositivos.csv" and then it worked. Thanks again!
      – Cesar Alejandro Villegas Yepez
      Mar 30 '17 at 16:55












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    This should work ok:



    awk 'BEGINFS=OFS=","$0=$3 FS $1 FS $21'





    share|improve this answer














    This should work ok:



    awk 'BEGINFS=OFS=","$0=$3 FS $1 FS $21'






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 30 '17 at 15:56

























    answered Mar 30 '17 at 15:45









    George Vasiliou

    5,32031027




    5,32031027











    • Updated.Try this one
      – George Vasiliou
      Mar 30 '17 at 16:00










    • It does the same, I think the way I added the serial numbers is affecting. I'm using a for loop to find the serial number from "show version" and add it in a given line of the .csv file. I think so, cause the first line does change.
      – Cesar Alejandro Villegas Yepez
      Mar 30 '17 at 16:05






    • 1




      I've used "dos2unix dispositivos.csv" and then it worked. Thanks again!
      – Cesar Alejandro Villegas Yepez
      Mar 30 '17 at 16:55
















    • Updated.Try this one
      – George Vasiliou
      Mar 30 '17 at 16:00










    • It does the same, I think the way I added the serial numbers is affecting. I'm using a for loop to find the serial number from "show version" and add it in a given line of the .csv file. I think so, cause the first line does change.
      – Cesar Alejandro Villegas Yepez
      Mar 30 '17 at 16:05






    • 1




      I've used "dos2unix dispositivos.csv" and then it worked. Thanks again!
      – Cesar Alejandro Villegas Yepez
      Mar 30 '17 at 16:55















    Updated.Try this one
    – George Vasiliou
    Mar 30 '17 at 16:00




    Updated.Try this one
    – George Vasiliou
    Mar 30 '17 at 16:00












    It does the same, I think the way I added the serial numbers is affecting. I'm using a for loop to find the serial number from "show version" and add it in a given line of the .csv file. I think so, cause the first line does change.
    – Cesar Alejandro Villegas Yepez
    Mar 30 '17 at 16:05




    It does the same, I think the way I added the serial numbers is affecting. I'm using a for loop to find the serial number from "show version" and add it in a given line of the .csv file. I think so, cause the first line does change.
    – Cesar Alejandro Villegas Yepez
    Mar 30 '17 at 16:05




    1




    1




    I've used "dos2unix dispositivos.csv" and then it worked. Thanks again!
    – Cesar Alejandro Villegas Yepez
    Mar 30 '17 at 16:55




    I've used "dos2unix dispositivos.csv" and then it worked. Thanks again!
    – Cesar Alejandro Villegas Yepez
    Mar 30 '17 at 16:55












    up vote
    2
    down vote













    perl -F, -ple '$_ = join ",", @F[2,0,1]'





    share|improve this answer
























      up vote
      2
      down vote













      perl -F, -ple '$_ = join ",", @F[2,0,1]'





      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        perl -F, -ple '$_ = join ",", @F[2,0,1]'





        share|improve this answer












        perl -F, -ple '$_ = join ",", @F[2,0,1]'






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 30 '17 at 15:52







        user218374


































             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f354864%2fhow-to-swap-certain-columns-in-csv-file%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)