Changing second column in csv conditionally
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have a csv file that looks like:
data/train/4/36280.png,four
data/train/2/10317.png,two
data/train/2/57890.png,two
data/train/1/53448.png,one
data/train/8/58233.png,eight
data/train/4/23599.png,four
data/train/2/35051.png,two
data/train/1/12323.png,one
data/train/9/18562.png,nine
data/train/8/46629.png,eight
data/train/7/1746.png,seven
Where first column is path and second is class. I would like to change the second column conditionally. In pseudo code I want something like:
If second column "four" change it to the next row's class
So far I've tried just to see if I can change ne class to another, but without success:
awk '{ if ($2 == "zero") $2="one"; print $0 }' train.csv > new_file.csv
gives me the same csv.
Basically I would like a script that gives me following output csv:
data/train/4/36280.png,seven
data/train/2/10317.png,four
data/train/2/57890.png,two
data/train/1/53448.png,two
data/train/8/58233.png,one
data/train/4/23599.png,eight
data/train/2/35051.png,four
data/train/1/12323.png,two
data/train/9/18562.png,one
data/train/8/46629.png,nine
data/train/7/1746.png,eight
command-line text-processing awk csv
New contributor
add a comment |
I have a csv file that looks like:
data/train/4/36280.png,four
data/train/2/10317.png,two
data/train/2/57890.png,two
data/train/1/53448.png,one
data/train/8/58233.png,eight
data/train/4/23599.png,four
data/train/2/35051.png,two
data/train/1/12323.png,one
data/train/9/18562.png,nine
data/train/8/46629.png,eight
data/train/7/1746.png,seven
Where first column is path and second is class. I would like to change the second column conditionally. In pseudo code I want something like:
If second column "four" change it to the next row's class
So far I've tried just to see if I can change ne class to another, but without success:
awk '{ if ($2 == "zero") $2="one"; print $0 }' train.csv > new_file.csv
gives me the same csv.
Basically I would like a script that gives me following output csv:
data/train/4/36280.png,seven
data/train/2/10317.png,four
data/train/2/57890.png,two
data/train/1/53448.png,two
data/train/8/58233.png,one
data/train/4/23599.png,eight
data/train/2/35051.png,four
data/train/1/12323.png,two
data/train/9/18562.png,one
data/train/8/46629.png,nine
data/train/7/1746.png,eight
command-line text-processing awk csv
New contributor
The default field separator in awk is whitespace; if you have comma separated data, you will need to set the separator accordingly - either with the-F
command line option, or by settingFS=","
in aBEGIN
rule.
– steeldriver
2 days ago
What is "the next rows class" ? Could you please edit your question and add an example output?
– RoVo
2 days ago
You mean like `awk FS="," '{ if ($2 == "zero") $2="one"; print $0 }' train.csv > new_file.csv´ Next row class for line 1 is line 2, column 2. I basically want to shuffle the class in an easy way
– iKnowItAll
2 days ago
add a comment |
I have a csv file that looks like:
data/train/4/36280.png,four
data/train/2/10317.png,two
data/train/2/57890.png,two
data/train/1/53448.png,one
data/train/8/58233.png,eight
data/train/4/23599.png,four
data/train/2/35051.png,two
data/train/1/12323.png,one
data/train/9/18562.png,nine
data/train/8/46629.png,eight
data/train/7/1746.png,seven
Where first column is path and second is class. I would like to change the second column conditionally. In pseudo code I want something like:
If second column "four" change it to the next row's class
So far I've tried just to see if I can change ne class to another, but without success:
awk '{ if ($2 == "zero") $2="one"; print $0 }' train.csv > new_file.csv
gives me the same csv.
Basically I would like a script that gives me following output csv:
data/train/4/36280.png,seven
data/train/2/10317.png,four
data/train/2/57890.png,two
data/train/1/53448.png,two
data/train/8/58233.png,one
data/train/4/23599.png,eight
data/train/2/35051.png,four
data/train/1/12323.png,two
data/train/9/18562.png,one
data/train/8/46629.png,nine
data/train/7/1746.png,eight
command-line text-processing awk csv
New contributor
I have a csv file that looks like:
data/train/4/36280.png,four
data/train/2/10317.png,two
data/train/2/57890.png,two
data/train/1/53448.png,one
data/train/8/58233.png,eight
data/train/4/23599.png,four
data/train/2/35051.png,two
data/train/1/12323.png,one
data/train/9/18562.png,nine
data/train/8/46629.png,eight
data/train/7/1746.png,seven
Where first column is path and second is class. I would like to change the second column conditionally. In pseudo code I want something like:
If second column "four" change it to the next row's class
So far I've tried just to see if I can change ne class to another, but without success:
awk '{ if ($2 == "zero") $2="one"; print $0 }' train.csv > new_file.csv
gives me the same csv.
Basically I would like a script that gives me following output csv:
data/train/4/36280.png,seven
data/train/2/10317.png,four
data/train/2/57890.png,two
data/train/1/53448.png,two
data/train/8/58233.png,one
data/train/4/23599.png,eight
data/train/2/35051.png,four
data/train/1/12323.png,two
data/train/9/18562.png,one
data/train/8/46629.png,nine
data/train/7/1746.png,eight
command-line text-processing awk csv
command-line text-processing awk csv
New contributor
New contributor
edited yesterday
Zanna
51.2k13140243
51.2k13140243
New contributor
asked 2 days ago
iKnowItAlliKnowItAll
283
283
New contributor
New contributor
The default field separator in awk is whitespace; if you have comma separated data, you will need to set the separator accordingly - either with the-F
command line option, or by settingFS=","
in aBEGIN
rule.
– steeldriver
2 days ago
What is "the next rows class" ? Could you please edit your question and add an example output?
– RoVo
2 days ago
You mean like `awk FS="," '{ if ($2 == "zero") $2="one"; print $0 }' train.csv > new_file.csv´ Next row class for line 1 is line 2, column 2. I basically want to shuffle the class in an easy way
– iKnowItAll
2 days ago
add a comment |
The default field separator in awk is whitespace; if you have comma separated data, you will need to set the separator accordingly - either with the-F
command line option, or by settingFS=","
in aBEGIN
rule.
– steeldriver
2 days ago
What is "the next rows class" ? Could you please edit your question and add an example output?
– RoVo
2 days ago
You mean like `awk FS="," '{ if ($2 == "zero") $2="one"; print $0 }' train.csv > new_file.csv´ Next row class for line 1 is line 2, column 2. I basically want to shuffle the class in an easy way
– iKnowItAll
2 days ago
The default field separator in awk is whitespace; if you have comma separated data, you will need to set the separator accordingly - either with the
-F
command line option, or by setting FS=","
in a BEGIN
rule.– steeldriver
2 days ago
The default field separator in awk is whitespace; if you have comma separated data, you will need to set the separator accordingly - either with the
-F
command line option, or by setting FS=","
in a BEGIN
rule.– steeldriver
2 days ago
What is "the next rows class" ? Could you please edit your question and add an example output?
– RoVo
2 days ago
What is "the next rows class" ? Could you please edit your question and add an example output?
– RoVo
2 days ago
You mean like `awk FS="," '{ if ($2 == "zero") $2="one"; print $0 }' train.csv > new_file.csv´ Next row class for line 1 is line 2, column 2. I basically want to shuffle the class in an easy way
– iKnowItAll
2 days ago
You mean like `awk FS="," '{ if ($2 == "zero") $2="one"; print $0 }' train.csv > new_file.csv´ Next row class for line 1 is line 2, column 2. I basically want to shuffle the class in an easy way
– iKnowItAll
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
bash
solution:
readarray -t filename < <(cut -d ',' -f1 train.csv)
readarray -t class < <(cut -d ',' -f2 train.csv)
for (( i=0; i<${#filename[@]}; i++ )); do
printf '%s,%sn' "${filename[$i]}" "${class[$((i-1))]}";
done > new_file.csv
Equivalent awk
solution:
awk -F, '
{
filename[NR]=$1
class[NR]=$2
}
END {
OFS=","
print filename[1],class[NR]
for (i=2;i<=NR;i++) {
print filename[i],class[i-1]
}
}
' train.csv > new_file.csv
Both solutions first read the lines into arrays filename
and class
. Only difference is that the bash
array starts with 0
, the awk
array with 1
.
Then, we loop over the arrays and print the desired output. In the awk
solution, we need to treat the first line separately as, unlike in bash
, array[-1]
does not return the last element.
The awk
solution is a bit faster.
I tried it and it looks great! Could I ask how I would save this to a new CSV? I guess its putting > new_file.csv at the end maybe?
– iKnowItAll
2 days ago
exactly, added to the answer
– RoVo
2 days ago
Thanks alot!! Works great!
– iKnowItAll
2 days ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
iKnowItAll is a new contributor. Be nice, and check out our Code of Conduct.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1131364%2fchanging-second-column-in-csv-conditionally%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
bash
solution:
readarray -t filename < <(cut -d ',' -f1 train.csv)
readarray -t class < <(cut -d ',' -f2 train.csv)
for (( i=0; i<${#filename[@]}; i++ )); do
printf '%s,%sn' "${filename[$i]}" "${class[$((i-1))]}";
done > new_file.csv
Equivalent awk
solution:
awk -F, '
{
filename[NR]=$1
class[NR]=$2
}
END {
OFS=","
print filename[1],class[NR]
for (i=2;i<=NR;i++) {
print filename[i],class[i-1]
}
}
' train.csv > new_file.csv
Both solutions first read the lines into arrays filename
and class
. Only difference is that the bash
array starts with 0
, the awk
array with 1
.
Then, we loop over the arrays and print the desired output. In the awk
solution, we need to treat the first line separately as, unlike in bash
, array[-1]
does not return the last element.
The awk
solution is a bit faster.
I tried it and it looks great! Could I ask how I would save this to a new CSV? I guess its putting > new_file.csv at the end maybe?
– iKnowItAll
2 days ago
exactly, added to the answer
– RoVo
2 days ago
Thanks alot!! Works great!
– iKnowItAll
2 days ago
add a comment |
bash
solution:
readarray -t filename < <(cut -d ',' -f1 train.csv)
readarray -t class < <(cut -d ',' -f2 train.csv)
for (( i=0; i<${#filename[@]}; i++ )); do
printf '%s,%sn' "${filename[$i]}" "${class[$((i-1))]}";
done > new_file.csv
Equivalent awk
solution:
awk -F, '
{
filename[NR]=$1
class[NR]=$2
}
END {
OFS=","
print filename[1],class[NR]
for (i=2;i<=NR;i++) {
print filename[i],class[i-1]
}
}
' train.csv > new_file.csv
Both solutions first read the lines into arrays filename
and class
. Only difference is that the bash
array starts with 0
, the awk
array with 1
.
Then, we loop over the arrays and print the desired output. In the awk
solution, we need to treat the first line separately as, unlike in bash
, array[-1]
does not return the last element.
The awk
solution is a bit faster.
I tried it and it looks great! Could I ask how I would save this to a new CSV? I guess its putting > new_file.csv at the end maybe?
– iKnowItAll
2 days ago
exactly, added to the answer
– RoVo
2 days ago
Thanks alot!! Works great!
– iKnowItAll
2 days ago
add a comment |
bash
solution:
readarray -t filename < <(cut -d ',' -f1 train.csv)
readarray -t class < <(cut -d ',' -f2 train.csv)
for (( i=0; i<${#filename[@]}; i++ )); do
printf '%s,%sn' "${filename[$i]}" "${class[$((i-1))]}";
done > new_file.csv
Equivalent awk
solution:
awk -F, '
{
filename[NR]=$1
class[NR]=$2
}
END {
OFS=","
print filename[1],class[NR]
for (i=2;i<=NR;i++) {
print filename[i],class[i-1]
}
}
' train.csv > new_file.csv
Both solutions first read the lines into arrays filename
and class
. Only difference is that the bash
array starts with 0
, the awk
array with 1
.
Then, we loop over the arrays and print the desired output. In the awk
solution, we need to treat the first line separately as, unlike in bash
, array[-1]
does not return the last element.
The awk
solution is a bit faster.
bash
solution:
readarray -t filename < <(cut -d ',' -f1 train.csv)
readarray -t class < <(cut -d ',' -f2 train.csv)
for (( i=0; i<${#filename[@]}; i++ )); do
printf '%s,%sn' "${filename[$i]}" "${class[$((i-1))]}";
done > new_file.csv
Equivalent awk
solution:
awk -F, '
{
filename[NR]=$1
class[NR]=$2
}
END {
OFS=","
print filename[1],class[NR]
for (i=2;i<=NR;i++) {
print filename[i],class[i-1]
}
}
' train.csv > new_file.csv
Both solutions first read the lines into arrays filename
and class
. Only difference is that the bash
array starts with 0
, the awk
array with 1
.
Then, we loop over the arrays and print the desired output. In the awk
solution, we need to treat the first line separately as, unlike in bash
, array[-1]
does not return the last element.
The awk
solution is a bit faster.
edited 2 days ago
answered 2 days ago
RoVoRoVo
8,1511943
8,1511943
I tried it and it looks great! Could I ask how I would save this to a new CSV? I guess its putting > new_file.csv at the end maybe?
– iKnowItAll
2 days ago
exactly, added to the answer
– RoVo
2 days ago
Thanks alot!! Works great!
– iKnowItAll
2 days ago
add a comment |
I tried it and it looks great! Could I ask how I would save this to a new CSV? I guess its putting > new_file.csv at the end maybe?
– iKnowItAll
2 days ago
exactly, added to the answer
– RoVo
2 days ago
Thanks alot!! Works great!
– iKnowItAll
2 days ago
I tried it and it looks great! Could I ask how I would save this to a new CSV? I guess its putting > new_file.csv at the end maybe?
– iKnowItAll
2 days ago
I tried it and it looks great! Could I ask how I would save this to a new CSV? I guess its putting > new_file.csv at the end maybe?
– iKnowItAll
2 days ago
exactly, added to the answer
– RoVo
2 days ago
exactly, added to the answer
– RoVo
2 days ago
Thanks alot!! Works great!
– iKnowItAll
2 days ago
Thanks alot!! Works great!
– iKnowItAll
2 days ago
add a comment |
iKnowItAll is a new contributor. Be nice, and check out our Code of Conduct.
iKnowItAll is a new contributor. Be nice, and check out our Code of Conduct.
iKnowItAll is a new contributor. Be nice, and check out our Code of Conduct.
iKnowItAll is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1131364%2fchanging-second-column-in-csv-conditionally%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The default field separator in awk is whitespace; if you have comma separated data, you will need to set the separator accordingly - either with the
-F
command line option, or by settingFS=","
in aBEGIN
rule.– steeldriver
2 days ago
What is "the next rows class" ? Could you please edit your question and add an example output?
– RoVo
2 days ago
You mean like `awk FS="," '{ if ($2 == "zero") $2="one"; print $0 }' train.csv > new_file.csv´ Next row class for line 1 is line 2, column 2. I basically want to shuffle the class in an easy way
– iKnowItAll
2 days ago