replace space and empty cells in csv file
I have a csv file that has 7 columns. It has empty cells and some spaces between cells. How can I replace the empty cells with NA and remove extra spaces? Thank you very much!
Here is what my file looks like, but here it seems to warp around when I copy and past it.
130070078,PPW0001,1,4,4HW ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3,3WE ,1,14.43394935
sed csv awk
add a comment |
I have a csv file that has 7 columns. It has empty cells and some spaces between cells. How can I replace the empty cells with NA and remove extra spaces? Thank you very much!
Here is what my file looks like, but here it seems to warp around when I copy and past it.
130070078,PPW0001,1,4,4HW ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3,3WE ,1,14.43394935
sed csv awk
Please be careful: if your CSV file contains spaces and commas (for examplefoo,"bar, baz",bar
- it has two cells:foo
,bar, baz
andbar
) it isn't easy to parse (and change) withsed
orawk
.
– uzsolt
Mar 12 '17 at 17:49
Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
– Elham
Mar 12 '17 at 20:24
@uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.
– Elham
Mar 15 '17 at 14:02
if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)
– uzsolt
Mar 15 '17 at 17:19
add a comment |
I have a csv file that has 7 columns. It has empty cells and some spaces between cells. How can I replace the empty cells with NA and remove extra spaces? Thank you very much!
Here is what my file looks like, but here it seems to warp around when I copy and past it.
130070078,PPW0001,1,4,4HW ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3,3WE ,1,14.43394935
sed csv awk
I have a csv file that has 7 columns. It has empty cells and some spaces between cells. How can I replace the empty cells with NA and remove extra spaces? Thank you very much!
Here is what my file looks like, but here it seems to warp around when I copy and past it.
130070078,PPW0001,1,4,4HW ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3,3WE ,1,14.43394935
sed csv awk
sed csv awk
edited Mar 12 '17 at 17:22
Cyrus
3,77611024
3,77611024
asked Mar 12 '17 at 16:45
Elham
13
13
Please be careful: if your CSV file contains spaces and commas (for examplefoo,"bar, baz",bar
- it has two cells:foo
,bar, baz
andbar
) it isn't easy to parse (and change) withsed
orawk
.
– uzsolt
Mar 12 '17 at 17:49
Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
– Elham
Mar 12 '17 at 20:24
@uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.
– Elham
Mar 15 '17 at 14:02
if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)
– uzsolt
Mar 15 '17 at 17:19
add a comment |
Please be careful: if your CSV file contains spaces and commas (for examplefoo,"bar, baz",bar
- it has two cells:foo
,bar, baz
andbar
) it isn't easy to parse (and change) withsed
orawk
.
– uzsolt
Mar 12 '17 at 17:49
Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
– Elham
Mar 12 '17 at 20:24
@uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.
– Elham
Mar 15 '17 at 14:02
if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)
– uzsolt
Mar 15 '17 at 17:19
Please be careful: if your CSV file contains spaces and commas (for example
foo,"bar, baz",bar
- it has two cells: foo
, bar, baz
and bar
) it isn't easy to parse (and change) with sed
or awk
.– uzsolt
Mar 12 '17 at 17:49
Please be careful: if your CSV file contains spaces and commas (for example
foo,"bar, baz",bar
- it has two cells: foo
, bar, baz
and bar
) it isn't easy to parse (and change) with sed
or awk
.– uzsolt
Mar 12 '17 at 17:49
Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
– Elham
Mar 12 '17 at 20:24
Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
– Elham
Mar 12 '17 at 20:24
@uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.
– Elham
Mar 15 '17 at 14:02
@uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.
– Elham
Mar 15 '17 at 14:02
if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)
– uzsolt
Mar 15 '17 at 17:19
if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)
– uzsolt
Mar 15 '17 at 17:19
add a comment |
3 Answers
3
active
oldest
votes
Using sed
that will work for repeated empty fields as well:
sed ':l;s/,,/,NA,/;tl; s/[[:blank:]]*//g'
Or using awk
:
awk '{i=0;while(i++<2){gsub(/,,/,",na,");gsub(/ /, "")}}1'
Input:
130070078,PPW0001,1,4,4HW ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3, 3WE ,1,14.43394935
120040043,PPW0003,1 ,3,3WE ,1,14.43,,,3,,94,,,,9,,,,,35
120040043,PPW0003,0, 2, 3WE ,1,14.43,,,3,,94,,,,9,,,,,35
Output:
130070078,PPW0001,1,4,4HW,2,15.61943874
120040039,PPW0002,0,0,,0,0
120040043,PPW0003,1,3,3WE,1,14.43394935
120040043,PPW0003,1,3,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
120040043,PPW0003,0,2,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
add a comment |
Your answer:
sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
To get 'NA' in the last field if blank:
sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'> file2
You could also use :
sed 's/,,/,NA,/g' file1 | tr -d ' ' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'
add a comment |
αғsнιη's answer worked for me, but I'd just like to explain it a bit.
I was trying something like this:
echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed 's/,,/,-,/g'
Which outputs
1,-,2,-,,3,-,,4,-,,-,5,-,,-,,-,,-,,-,6
Because of the repeated empty fields the last comma is part of the first replacement and the start of the next desired replacement, so you just get every second empty field replaced.
Now you could do something like:
echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed -e 's/,,/,-,/g' -e 's/,,/,-,/g'
or
sed 's/,,/,-,/g;s/,,/,-,/g'
Which will replace all the cells, as the second command will get the ones that are missed, but it's a bit messy.
αғsнιη's command does essentially the same thing, using a label and a jump, which I was not aware you could do.
sed ':MYLABEL; s/,,/,-,/g; t MYLABEL;'
output:
1,-,2,-,-,3,-,-,4,-,-,-,5,-,-,-,-,-,-,-,-,-,6
So the first part of the command creates a label.
Then we have the same substitution.
Then we have the t command which means jump to label if the previous substitution command was successful.
More information: http://www.grymoire.com/Unix/Sed.html#uh-59
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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
});
}
});
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%2fsuperuser.com%2fquestions%2f1187941%2freplace-space-and-empty-cells-in-csv-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using sed
that will work for repeated empty fields as well:
sed ':l;s/,,/,NA,/;tl; s/[[:blank:]]*//g'
Or using awk
:
awk '{i=0;while(i++<2){gsub(/,,/,",na,");gsub(/ /, "")}}1'
Input:
130070078,PPW0001,1,4,4HW ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3, 3WE ,1,14.43394935
120040043,PPW0003,1 ,3,3WE ,1,14.43,,,3,,94,,,,9,,,,,35
120040043,PPW0003,0, 2, 3WE ,1,14.43,,,3,,94,,,,9,,,,,35
Output:
130070078,PPW0001,1,4,4HW,2,15.61943874
120040039,PPW0002,0,0,,0,0
120040043,PPW0003,1,3,3WE,1,14.43394935
120040043,PPW0003,1,3,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
120040043,PPW0003,0,2,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
add a comment |
Using sed
that will work for repeated empty fields as well:
sed ':l;s/,,/,NA,/;tl; s/[[:blank:]]*//g'
Or using awk
:
awk '{i=0;while(i++<2){gsub(/,,/,",na,");gsub(/ /, "")}}1'
Input:
130070078,PPW0001,1,4,4HW ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3, 3WE ,1,14.43394935
120040043,PPW0003,1 ,3,3WE ,1,14.43,,,3,,94,,,,9,,,,,35
120040043,PPW0003,0, 2, 3WE ,1,14.43,,,3,,94,,,,9,,,,,35
Output:
130070078,PPW0001,1,4,4HW,2,15.61943874
120040039,PPW0002,0,0,,0,0
120040043,PPW0003,1,3,3WE,1,14.43394935
120040043,PPW0003,1,3,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
120040043,PPW0003,0,2,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
add a comment |
Using sed
that will work for repeated empty fields as well:
sed ':l;s/,,/,NA,/;tl; s/[[:blank:]]*//g'
Or using awk
:
awk '{i=0;while(i++<2){gsub(/,,/,",na,");gsub(/ /, "")}}1'
Input:
130070078,PPW0001,1,4,4HW ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3, 3WE ,1,14.43394935
120040043,PPW0003,1 ,3,3WE ,1,14.43,,,3,,94,,,,9,,,,,35
120040043,PPW0003,0, 2, 3WE ,1,14.43,,,3,,94,,,,9,,,,,35
Output:
130070078,PPW0001,1,4,4HW,2,15.61943874
120040039,PPW0002,0,0,,0,0
120040043,PPW0003,1,3,3WE,1,14.43394935
120040043,PPW0003,1,3,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
120040043,PPW0003,0,2,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
Using sed
that will work for repeated empty fields as well:
sed ':l;s/,,/,NA,/;tl; s/[[:blank:]]*//g'
Or using awk
:
awk '{i=0;while(i++<2){gsub(/,,/,",na,");gsub(/ /, "")}}1'
Input:
130070078,PPW0001,1,4,4HW ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3, 3WE ,1,14.43394935
120040043,PPW0003,1 ,3,3WE ,1,14.43,,,3,,94,,,,9,,,,,35
120040043,PPW0003,0, 2, 3WE ,1,14.43,,,3,,94,,,,9,,,,,35
Output:
130070078,PPW0001,1,4,4HW,2,15.61943874
120040039,PPW0002,0,0,,0,0
120040043,PPW0003,1,3,3WE,1,14.43394935
120040043,PPW0003,1,3,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
120040043,PPW0003,0,2,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
answered Oct 20 '17 at 7:42
αғsнιη
3841217
3841217
add a comment |
add a comment |
Your answer:
sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
To get 'NA' in the last field if blank:
sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'> file2
You could also use :
sed 's/,,/,NA,/g' file1 | tr -d ' ' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'
add a comment |
Your answer:
sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
To get 'NA' in the last field if blank:
sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'> file2
You could also use :
sed 's/,,/,NA,/g' file1 | tr -d ' ' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'
add a comment |
Your answer:
sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
To get 'NA' in the last field if blank:
sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'> file2
You could also use :
sed 's/,,/,NA,/g' file1 | tr -d ' ' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'
Your answer:
sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
To get 'NA' in the last field if blank:
sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'> file2
You could also use :
sed 's/,,/,NA,/g' file1 | tr -d ' ' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'
edited Apr 29 '17 at 23:05
Stephen Rauch
2,27581725
2,27581725
answered Apr 29 '17 at 22:16
bstipe
1113
1113
add a comment |
add a comment |
αғsнιη's answer worked for me, but I'd just like to explain it a bit.
I was trying something like this:
echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed 's/,,/,-,/g'
Which outputs
1,-,2,-,,3,-,,4,-,,-,5,-,,-,,-,,-,,-,6
Because of the repeated empty fields the last comma is part of the first replacement and the start of the next desired replacement, so you just get every second empty field replaced.
Now you could do something like:
echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed -e 's/,,/,-,/g' -e 's/,,/,-,/g'
or
sed 's/,,/,-,/g;s/,,/,-,/g'
Which will replace all the cells, as the second command will get the ones that are missed, but it's a bit messy.
αғsнιη's command does essentially the same thing, using a label and a jump, which I was not aware you could do.
sed ':MYLABEL; s/,,/,-,/g; t MYLABEL;'
output:
1,-,2,-,-,3,-,-,4,-,-,-,5,-,-,-,-,-,-,-,-,-,6
So the first part of the command creates a label.
Then we have the same substitution.
Then we have the t command which means jump to label if the previous substitution command was successful.
More information: http://www.grymoire.com/Unix/Sed.html#uh-59
add a comment |
αғsнιη's answer worked for me, but I'd just like to explain it a bit.
I was trying something like this:
echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed 's/,,/,-,/g'
Which outputs
1,-,2,-,,3,-,,4,-,,-,5,-,,-,,-,,-,,-,6
Because of the repeated empty fields the last comma is part of the first replacement and the start of the next desired replacement, so you just get every second empty field replaced.
Now you could do something like:
echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed -e 's/,,/,-,/g' -e 's/,,/,-,/g'
or
sed 's/,,/,-,/g;s/,,/,-,/g'
Which will replace all the cells, as the second command will get the ones that are missed, but it's a bit messy.
αғsнιη's command does essentially the same thing, using a label and a jump, which I was not aware you could do.
sed ':MYLABEL; s/,,/,-,/g; t MYLABEL;'
output:
1,-,2,-,-,3,-,-,4,-,-,-,5,-,-,-,-,-,-,-,-,-,6
So the first part of the command creates a label.
Then we have the same substitution.
Then we have the t command which means jump to label if the previous substitution command was successful.
More information: http://www.grymoire.com/Unix/Sed.html#uh-59
add a comment |
αғsнιη's answer worked for me, but I'd just like to explain it a bit.
I was trying something like this:
echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed 's/,,/,-,/g'
Which outputs
1,-,2,-,,3,-,,4,-,,-,5,-,,-,,-,,-,,-,6
Because of the repeated empty fields the last comma is part of the first replacement and the start of the next desired replacement, so you just get every second empty field replaced.
Now you could do something like:
echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed -e 's/,,/,-,/g' -e 's/,,/,-,/g'
or
sed 's/,,/,-,/g;s/,,/,-,/g'
Which will replace all the cells, as the second command will get the ones that are missed, but it's a bit messy.
αғsнιη's command does essentially the same thing, using a label and a jump, which I was not aware you could do.
sed ':MYLABEL; s/,,/,-,/g; t MYLABEL;'
output:
1,-,2,-,-,3,-,-,4,-,-,-,5,-,-,-,-,-,-,-,-,-,6
So the first part of the command creates a label.
Then we have the same substitution.
Then we have the t command which means jump to label if the previous substitution command was successful.
More information: http://www.grymoire.com/Unix/Sed.html#uh-59
αғsнιη's answer worked for me, but I'd just like to explain it a bit.
I was trying something like this:
echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed 's/,,/,-,/g'
Which outputs
1,-,2,-,,3,-,,4,-,,-,5,-,,-,,-,,-,,-,6
Because of the repeated empty fields the last comma is part of the first replacement and the start of the next desired replacement, so you just get every second empty field replaced.
Now you could do something like:
echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed -e 's/,,/,-,/g' -e 's/,,/,-,/g'
or
sed 's/,,/,-,/g;s/,,/,-,/g'
Which will replace all the cells, as the second command will get the ones that are missed, but it's a bit messy.
αғsнιη's command does essentially the same thing, using a label and a jump, which I was not aware you could do.
sed ':MYLABEL; s/,,/,-,/g; t MYLABEL;'
output:
1,-,2,-,-,3,-,-,4,-,-,-,5,-,-,-,-,-,-,-,-,-,6
So the first part of the command creates a label.
Then we have the same substitution.
Then we have the t command which means jump to label if the previous substitution command was successful.
More information: http://www.grymoire.com/Unix/Sed.html#uh-59
answered Dec 18 '18 at 10:36
Jack Culhane
1011
1011
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fsuperuser.com%2fquestions%2f1187941%2freplace-space-and-empty-cells-in-csv-file%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
Please be careful: if your CSV file contains spaces and commas (for example
foo,"bar, baz",bar
- it has two cells:foo
,bar, baz
andbar
) it isn't easy to parse (and change) withsed
orawk
.– uzsolt
Mar 12 '17 at 17:49
Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
– Elham
Mar 12 '17 at 20:24
@uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.
– Elham
Mar 15 '17 at 14:02
if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)
– uzsolt
Mar 15 '17 at 17:19