How do I output required entries from a CSV file into a new file and count the number of entries?
I have a CSV file of a table that has some entries that are blank. I want my code to look through all the entries for each row (except the first) and count the number of blank entries > store the blank entries per row and separate them with a comma into a file called temp_file > then I want the code to count the number of blank entries in that file and give me a value e.g. if it is,,, then the output should be 3.
This is currently what I have:
sed 1d ${FILENAME} | while read ln
do
echo $ln | tail -1 PMRExceptions.csv | tr "," "n" | grep -nx '^$' | cut -d":" -f1
PMRNUM=echo $ln | awk -F"," '{print $2}'
PMRMGR=echo $ln | awk -F"," '{print $3}'
COUNT=less $TEMPFILE | wc -l
this part over here is supposed to count the number of blank entries
while read line
do
columns=`head -1 $FILENAME | awk -F"," -v ln=$line '{print $ln}'`
The value that I get is 0, no matter what I change. I think that the entries are not being stored in the tempfile.
bash shell-script
add a comment |
I have a CSV file of a table that has some entries that are blank. I want my code to look through all the entries for each row (except the first) and count the number of blank entries > store the blank entries per row and separate them with a comma into a file called temp_file > then I want the code to count the number of blank entries in that file and give me a value e.g. if it is,,, then the output should be 3.
This is currently what I have:
sed 1d ${FILENAME} | while read ln
do
echo $ln | tail -1 PMRExceptions.csv | tr "," "n" | grep -nx '^$' | cut -d":" -f1
PMRNUM=echo $ln | awk -F"," '{print $2}'
PMRMGR=echo $ln | awk -F"," '{print $3}'
COUNT=less $TEMPFILE | wc -l
this part over here is supposed to count the number of blank entries
while read line
do
columns=`head -1 $FILENAME | awk -F"," -v ln=$line '{print $ln}'`
The value that I get is 0, no matter what I change. I think that the entries are not being stored in the tempfile.
bash shell-script
add a comment |
I have a CSV file of a table that has some entries that are blank. I want my code to look through all the entries for each row (except the first) and count the number of blank entries > store the blank entries per row and separate them with a comma into a file called temp_file > then I want the code to count the number of blank entries in that file and give me a value e.g. if it is,,, then the output should be 3.
This is currently what I have:
sed 1d ${FILENAME} | while read ln
do
echo $ln | tail -1 PMRExceptions.csv | tr "," "n" | grep -nx '^$' | cut -d":" -f1
PMRNUM=echo $ln | awk -F"," '{print $2}'
PMRMGR=echo $ln | awk -F"," '{print $3}'
COUNT=less $TEMPFILE | wc -l
this part over here is supposed to count the number of blank entries
while read line
do
columns=`head -1 $FILENAME | awk -F"," -v ln=$line '{print $ln}'`
The value that I get is 0, no matter what I change. I think that the entries are not being stored in the tempfile.
bash shell-script
I have a CSV file of a table that has some entries that are blank. I want my code to look through all the entries for each row (except the first) and count the number of blank entries > store the blank entries per row and separate them with a comma into a file called temp_file > then I want the code to count the number of blank entries in that file and give me a value e.g. if it is,,, then the output should be 3.
This is currently what I have:
sed 1d ${FILENAME} | while read ln
do
echo $ln | tail -1 PMRExceptions.csv | tr "," "n" | grep -nx '^$' | cut -d":" -f1
PMRNUM=echo $ln | awk -F"," '{print $2}'
PMRMGR=echo $ln | awk -F"," '{print $3}'
COUNT=less $TEMPFILE | wc -l
this part over here is supposed to count the number of blank entries
while read line
do
columns=`head -1 $FILENAME | awk -F"," -v ln=$line '{print $ln}'`
The value that I get is 0, no matter what I change. I think that the entries are not being stored in the tempfile.
bash shell-script
bash shell-script
edited Dec 16 at 21:36
Rui F Ribeiro
38.9k1479129
38.9k1479129
asked Aug 10 at 8:45
infiniteoscillation
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Using awk
to count the number of comma-delimited fields on each line of input that are empty (excluding the header line):
awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv
This assumes that no field contains embedded commas.
Example:
$ cat file.csv
A,B,C
1,,0
all,bar,none
my,field,here
no,data,
$ awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv
1
0
0
1
Related:
- Why is using a shell loop to process text considered bad practice?
which part of the code will i need to replace this with?
– infiniteoscillation
Aug 10 at 9:08
@infiniteoscillation All of it. Theawk
command I have in the answer reads the original CSV file and counts the number of empty fields on each line.
– Kusalananda
Aug 10 at 9:10
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f461747%2fhow-do-i-output-required-entries-from-a-csv-file-into-a-new-file-and-count-the-n%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
Using awk
to count the number of comma-delimited fields on each line of input that are empty (excluding the header line):
awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv
This assumes that no field contains embedded commas.
Example:
$ cat file.csv
A,B,C
1,,0
all,bar,none
my,field,here
no,data,
$ awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv
1
0
0
1
Related:
- Why is using a shell loop to process text considered bad practice?
which part of the code will i need to replace this with?
– infiniteoscillation
Aug 10 at 9:08
@infiniteoscillation All of it. Theawk
command I have in the answer reads the original CSV file and counts the number of empty fields on each line.
– Kusalananda
Aug 10 at 9:10
add a comment |
Using awk
to count the number of comma-delimited fields on each line of input that are empty (excluding the header line):
awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv
This assumes that no field contains embedded commas.
Example:
$ cat file.csv
A,B,C
1,,0
all,bar,none
my,field,here
no,data,
$ awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv
1
0
0
1
Related:
- Why is using a shell loop to process text considered bad practice?
which part of the code will i need to replace this with?
– infiniteoscillation
Aug 10 at 9:08
@infiniteoscillation All of it. Theawk
command I have in the answer reads the original CSV file and counts the number of empty fields on each line.
– Kusalananda
Aug 10 at 9:10
add a comment |
Using awk
to count the number of comma-delimited fields on each line of input that are empty (excluding the header line):
awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv
This assumes that no field contains embedded commas.
Example:
$ cat file.csv
A,B,C
1,,0
all,bar,none
my,field,here
no,data,
$ awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv
1
0
0
1
Related:
- Why is using a shell loop to process text considered bad practice?
Using awk
to count the number of comma-delimited fields on each line of input that are empty (excluding the header line):
awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv
This assumes that no field contains embedded commas.
Example:
$ cat file.csv
A,B,C
1,,0
all,bar,none
my,field,here
no,data,
$ awk -F ',' 'NR > 1 { c=0; for (i = 1; i <= NF; ++i) if ($i == "") ++c; print c }' file.csv
1
0
0
1
Related:
- Why is using a shell loop to process text considered bad practice?
answered Aug 10 at 8:53
Kusalananda
121k16229372
121k16229372
which part of the code will i need to replace this with?
– infiniteoscillation
Aug 10 at 9:08
@infiniteoscillation All of it. Theawk
command I have in the answer reads the original CSV file and counts the number of empty fields on each line.
– Kusalananda
Aug 10 at 9:10
add a comment |
which part of the code will i need to replace this with?
– infiniteoscillation
Aug 10 at 9:08
@infiniteoscillation All of it. Theawk
command I have in the answer reads the original CSV file and counts the number of empty fields on each line.
– Kusalananda
Aug 10 at 9:10
which part of the code will i need to replace this with?
– infiniteoscillation
Aug 10 at 9:08
which part of the code will i need to replace this with?
– infiniteoscillation
Aug 10 at 9:08
@infiniteoscillation All of it. The
awk
command I have in the answer reads the original CSV file and counts the number of empty fields on each line.– Kusalananda
Aug 10 at 9:10
@infiniteoscillation All of it. The
awk
command I have in the answer reads the original CSV file and counts the number of empty fields on each line.– Kusalananda
Aug 10 at 9:10
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f461747%2fhow-do-i-output-required-entries-from-a-csv-file-into-a-new-file-and-count-the-n%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