Using bash to swap first and second columns in CSV
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm using bash. I have a CSV file with two columns of data that looks roughly like this
num_logins,day
253,2016-07-01
127,2016-07-02
I want to swap the first and second columns (making the date column the first one). So I tried this
awk ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
However, the results are outputting the same . What am I missing in my awk statement above to get things to switch properly?
shell-script text-processing awk csv
add a comment |Â
up vote
1
down vote
favorite
I'm using bash. I have a CSV file with two columns of data that looks roughly like this
num_logins,day
253,2016-07-01
127,2016-07-02
I want to swap the first and second columns (making the date column the first one). So I tried this
awk ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
However, the results are outputting the same . What am I missing in my awk statement above to get things to switch properly?
shell-script text-processing awk csv
Bash isn’t a text editor...
– Jeff Schaller
Aug 6 at 20:21
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm using bash. I have a CSV file with two columns of data that looks roughly like this
num_logins,day
253,2016-07-01
127,2016-07-02
I want to swap the first and second columns (making the date column the first one). So I tried this
awk ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
However, the results are outputting the same . What am I missing in my awk statement above to get things to switch properly?
shell-script text-processing awk csv
I'm using bash. I have a CSV file with two columns of data that looks roughly like this
num_logins,day
253,2016-07-01
127,2016-07-02
I want to swap the first and second columns (making the date column the first one). So I tried this
awk ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
However, the results are outputting the same . What am I missing in my awk statement above to get things to switch properly?
shell-script text-processing awk csv
edited Aug 6 at 20:22


Jeff Schaller
31.2k846105
31.2k846105
asked Aug 6 at 19:15
Dave
368827
368827
Bash isn’t a text editor...
– Jeff Schaller
Aug 6 at 20:21
add a comment |Â
Bash isn’t a text editor...
– Jeff Schaller
Aug 6 at 20:21
Bash isn’t a text editor...
– Jeff Schaller
Aug 6 at 20:21
Bash isn’t a text editor...
– Jeff Schaller
Aug 6 at 20:21
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
Here you go :
awk ' FS="," print $2 "," $1 ' sampleData.csv
add a comment |Â
up vote
5
down vote
Because default delimiter is space/tab. Not comma. Taking your original code, and adding a -F,
solves it.
$ awk -F, ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
day num_logins
2016-07-01 253
2016-07-02 127
$
Stripping it down to $0=$2" "$11
gets same result.
$ awk -F, '$0=$2" "$11' /tmp/2016_logins.csv
day num_logins
2016-07-01 253
2016-07-02 127
$
2
SetOFS
to get commas in the output too.
– Kusalananda
Aug 6 at 19:30
And this one would leave columns 3+ alone, rather than throwing them away.
– Monty Harder
Aug 6 at 22:47
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Here you go :
awk ' FS="," print $2 "," $1 ' sampleData.csv
add a comment |Â
up vote
3
down vote
accepted
Here you go :
awk ' FS="," print $2 "," $1 ' sampleData.csv
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Here you go :
awk ' FS="," print $2 "," $1 ' sampleData.csv
Here you go :
awk ' FS="," print $2 "," $1 ' sampleData.csv
answered Aug 6 at 19:20


Joe M
5964
5964
add a comment |Â
add a comment |Â
up vote
5
down vote
Because default delimiter is space/tab. Not comma. Taking your original code, and adding a -F,
solves it.
$ awk -F, ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
day num_logins
2016-07-01 253
2016-07-02 127
$
Stripping it down to $0=$2" "$11
gets same result.
$ awk -F, '$0=$2" "$11' /tmp/2016_logins.csv
day num_logins
2016-07-01 253
2016-07-02 127
$
2
SetOFS
to get commas in the output too.
– Kusalananda
Aug 6 at 19:30
And this one would leave columns 3+ alone, rather than throwing them away.
– Monty Harder
Aug 6 at 22:47
add a comment |Â
up vote
5
down vote
Because default delimiter is space/tab. Not comma. Taking your original code, and adding a -F,
solves it.
$ awk -F, ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
day num_logins
2016-07-01 253
2016-07-02 127
$
Stripping it down to $0=$2" "$11
gets same result.
$ awk -F, '$0=$2" "$11' /tmp/2016_logins.csv
day num_logins
2016-07-01 253
2016-07-02 127
$
2
SetOFS
to get commas in the output too.
– Kusalananda
Aug 6 at 19:30
And this one would leave columns 3+ alone, rather than throwing them away.
– Monty Harder
Aug 6 at 22:47
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Because default delimiter is space/tab. Not comma. Taking your original code, and adding a -F,
solves it.
$ awk -F, ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
day num_logins
2016-07-01 253
2016-07-02 127
$
Stripping it down to $0=$2" "$11
gets same result.
$ awk -F, '$0=$2" "$11' /tmp/2016_logins.csv
day num_logins
2016-07-01 253
2016-07-02 127
$
Because default delimiter is space/tab. Not comma. Taking your original code, and adding a -F,
solves it.
$ awk -F, ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
day num_logins
2016-07-01 253
2016-07-02 127
$
Stripping it down to $0=$2" "$11
gets same result.
$ awk -F, '$0=$2" "$11' /tmp/2016_logins.csv
day num_logins
2016-07-01 253
2016-07-02 127
$
edited Aug 6 at 21:19
answered Aug 6 at 19:20


steve
12.3k22048
12.3k22048
2
SetOFS
to get commas in the output too.
– Kusalananda
Aug 6 at 19:30
And this one would leave columns 3+ alone, rather than throwing them away.
– Monty Harder
Aug 6 at 22:47
add a comment |Â
2
SetOFS
to get commas in the output too.
– Kusalananda
Aug 6 at 19:30
And this one would leave columns 3+ alone, rather than throwing them away.
– Monty Harder
Aug 6 at 22:47
2
2
Set
OFS
to get commas in the output too.– Kusalananda
Aug 6 at 19:30
Set
OFS
to get commas in the output too.– Kusalananda
Aug 6 at 19:30
And this one would leave columns 3+ alone, rather than throwing them away.
– Monty Harder
Aug 6 at 22:47
And this one would leave columns 3+ alone, rather than throwing them away.
– Monty Harder
Aug 6 at 22:47
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%2f460890%2fusing-bash-to-swap-first-and-second-columns-in-csv%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
Bash isn’t a text editor...
– Jeff Schaller
Aug 6 at 20:21