-2

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 'BEGIN{FS=OFS=","}{a=$1; $1=$3; $3=a}1' 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.

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, 2017 at 15:46
  • What you want is NOT swapping col3 <-> col1. You need to be really clear what you want.
    – user218374
    Mar 30, 2017 at 15:48

2 Answers 2

1

This should work ok:

awk 'BEGIN{FS=OFS=","}{$0=$3 FS $1 FS $2}1'
3
  • Updated.Try this one Mar 30, 2017 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. Mar 30, 2017 at 16:05
  • 1
    I've used "dos2unix dispositivos.csv" and then it worked. Thanks again! Mar 30, 2017 at 16:55
1
perl -F, -ple '$_ = join ",", @F[2,0,1]'
1
  • @user218374 Even though this topic is 4 years old, you should always explain your answer.
    – Vituvo
    Jun 4, 2021 at 5:57

You must log in to answer this question.

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