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
30.9k846105
30.9k846105
asked Aug 6 at 19:15
Dave
363626
363626
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 |Â
3 Answers
3
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 |Â
up vote
-1
down vote
You can do this without awk as well:
IFS=','
while read a b
do
echo $b, $a
done <<!
num_logins,day
253,2016-07-01
127,2016-07-02
!
BTW, I used what I think is called a 'here document' (?) - that is the construction with:
....<<!
...
!
This can be replaced with:
... < /tmp/2016_logins.csv
You should put those variables in quotes:echo "$b, $a"
.âÂÂprintf "%sn" "$b,â¯$a"
orprintf "%s,â¯%sn" "$b" "$a"
would be better.
â G-Man
Aug 7 at 9:28
Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
â j4nd3r53n
Aug 7 at 13:40
Your solution works for the sample data.â ThatâÂÂs a pretty low bar to clear; we prefer answers that work for any data consistent with the question.â Try your answer with an input of*,2018-08-08
.
â G-Man
Aug 8 at 1:33
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
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.1k22047
12.1k22047
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 |Â
up vote
-1
down vote
You can do this without awk as well:
IFS=','
while read a b
do
echo $b, $a
done <<!
num_logins,day
253,2016-07-01
127,2016-07-02
!
BTW, I used what I think is called a 'here document' (?) - that is the construction with:
....<<!
...
!
This can be replaced with:
... < /tmp/2016_logins.csv
You should put those variables in quotes:echo "$b, $a"
.âÂÂprintf "%sn" "$b,â¯$a"
orprintf "%s,â¯%sn" "$b" "$a"
would be better.
â G-Man
Aug 7 at 9:28
Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
â j4nd3r53n
Aug 7 at 13:40
Your solution works for the sample data.â ThatâÂÂs a pretty low bar to clear; we prefer answers that work for any data consistent with the question.â Try your answer with an input of*,2018-08-08
.
â G-Man
Aug 8 at 1:33
add a comment |Â
up vote
-1
down vote
You can do this without awk as well:
IFS=','
while read a b
do
echo $b, $a
done <<!
num_logins,day
253,2016-07-01
127,2016-07-02
!
BTW, I used what I think is called a 'here document' (?) - that is the construction with:
....<<!
...
!
This can be replaced with:
... < /tmp/2016_logins.csv
You should put those variables in quotes:echo "$b, $a"
.âÂÂprintf "%sn" "$b,â¯$a"
orprintf "%s,â¯%sn" "$b" "$a"
would be better.
â G-Man
Aug 7 at 9:28
Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
â j4nd3r53n
Aug 7 at 13:40
Your solution works for the sample data.â ThatâÂÂs a pretty low bar to clear; we prefer answers that work for any data consistent with the question.â Try your answer with an input of*,2018-08-08
.
â G-Man
Aug 8 at 1:33
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
You can do this without awk as well:
IFS=','
while read a b
do
echo $b, $a
done <<!
num_logins,day
253,2016-07-01
127,2016-07-02
!
BTW, I used what I think is called a 'here document' (?) - that is the construction with:
....<<!
...
!
This can be replaced with:
... < /tmp/2016_logins.csv
You can do this without awk as well:
IFS=','
while read a b
do
echo $b, $a
done <<!
num_logins,day
253,2016-07-01
127,2016-07-02
!
BTW, I used what I think is called a 'here document' (?) - that is the construction with:
....<<!
...
!
This can be replaced with:
... < /tmp/2016_logins.csv
answered Aug 7 at 8:55
j4nd3r53n
99
99
You should put those variables in quotes:echo "$b, $a"
.âÂÂprintf "%sn" "$b,â¯$a"
orprintf "%s,â¯%sn" "$b" "$a"
would be better.
â G-Man
Aug 7 at 9:28
Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
â j4nd3r53n
Aug 7 at 13:40
Your solution works for the sample data.â ThatâÂÂs a pretty low bar to clear; we prefer answers that work for any data consistent with the question.â Try your answer with an input of*,2018-08-08
.
â G-Man
Aug 8 at 1:33
add a comment |Â
You should put those variables in quotes:echo "$b, $a"
.âÂÂprintf "%sn" "$b,â¯$a"
orprintf "%s,â¯%sn" "$b" "$a"
would be better.
â G-Man
Aug 7 at 9:28
Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
â j4nd3r53n
Aug 7 at 13:40
Your solution works for the sample data.â ThatâÂÂs a pretty low bar to clear; we prefer answers that work for any data consistent with the question.â Try your answer with an input of*,2018-08-08
.
â G-Man
Aug 8 at 1:33
You should put those variables in quotes:
echo "$b, $a"
.â printf "%sn" "$b,â¯$a"
or printf "%s,â¯%sn" "$b" "$a"
would be better.â G-Man
Aug 7 at 9:28
You should put those variables in quotes:
echo "$b, $a"
.â printf "%sn" "$b,â¯$a"
or printf "%s,â¯%sn" "$b" "$a"
would be better.â G-Man
Aug 7 at 9:28
Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
â j4nd3r53n
Aug 7 at 13:40
Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
â j4nd3r53n
Aug 7 at 13:40
Your solution works for the sample data.â ThatâÂÂs a pretty low bar to clear; we prefer answers that work for any data consistent with the question.â Try your answer with an input of
*,2018-08-08
.â G-Man
Aug 8 at 1:33
Your solution works for the sample data.â ThatâÂÂs a pretty low bar to clear; we prefer answers that work for any data consistent with the question.â Try your answer with an input of
*,2018-08-08
.â G-Man
Aug 8 at 1:33
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