How to swap certain columns in .csv file

Clash 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.
text-processing awk sed grep csv
add a comment |Â
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.
text-processing awk sed grep csv
2
your profile shows 16 questions taggedawk.. 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
add a comment |Â
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.
text-processing awk sed grep csv
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
text-processing awk sed grep csv
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 taggedawk.. 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
add a comment |Â
2
your profile shows 16 questions taggedawk.. 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
add a comment |Â
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'
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
add a comment |Â
up vote
2
down vote
perl -F, -ple '$_ = join ",", @F[2,0,1]'
add a comment |Â
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'
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
add a comment |Â
up vote
1
down vote
accepted
This should work ok:
awk 'BEGINFS=OFS=","$0=$3 FS $1 FS $21'
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
add a comment |Â
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'
This should work ok:
awk 'BEGINFS=OFS=","$0=$3 FS $1 FS $21'
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
add a comment |Â
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
add a comment |Â
up vote
2
down vote
perl -F, -ple '$_ = join ",", @F[2,0,1]'
add a comment |Â
up vote
2
down vote
perl -F, -ple '$_ = join ",", @F[2,0,1]'
add a comment |Â
up vote
2
down vote
up vote
2
down vote
perl -F, -ple '$_ = join ",", @F[2,0,1]'
perl -F, -ple '$_ = join ",", @F[2,0,1]'
answered Mar 30 '17 at 15:52
user218374
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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