How to count the times a specific character appears in a file?
For example, we want to count all quote ("
) characters; we just worry if files have more quotes than it should.
For example:
cluster-env,"manage_dirs_on_root","true"
cluster-env,"one_dir_per_partition","false"
cluster-env,"override_uid","true"
cluster-env,"recovery_enabled","false"
expected results:
16
text-processing
add a comment |
For example, we want to count all quote ("
) characters; we just worry if files have more quotes than it should.
For example:
cluster-env,"manage_dirs_on_root","true"
cluster-env,"one_dir_per_partition","false"
cluster-env,"override_uid","true"
cluster-env,"recovery_enabled","false"
expected results:
16
text-processing
add a comment |
For example, we want to count all quote ("
) characters; we just worry if files have more quotes than it should.
For example:
cluster-env,"manage_dirs_on_root","true"
cluster-env,"one_dir_per_partition","false"
cluster-env,"override_uid","true"
cluster-env,"recovery_enabled","false"
expected results:
16
text-processing
For example, we want to count all quote ("
) characters; we just worry if files have more quotes than it should.
For example:
cluster-env,"manage_dirs_on_root","true"
cluster-env,"one_dir_per_partition","false"
cluster-env,"override_uid","true"
cluster-env,"recovery_enabled","false"
expected results:
16
text-processing
text-processing
edited Aug 22 '17 at 22:03
Braiam
23.1k1976138
23.1k1976138
asked Aug 22 '17 at 12:32
yaelyael
2,44112362
2,44112362
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
You can combine tr
(translate or delete characters) with wc
(count words, lines, characters):
tr -cd '"' < yourfile.cfg | wc -c
(-d
elete all characters in the c
omplement of "
, and then count the c
haracters.)
add a comment |
grep approach:
grep -o '"' file | wc -l
16
-o
- output only matched substrings
Or with single gawk:
awk -v RS='' -v FPAT='"' '{print NF}' file
16
RS=''
- empty record separator (instead of newline)FPAT='"'
- pattern defining field value
-o
is a non-standard GNU extension to the standardgrep
utility. It's not mentioned in the POSIX documentation forgrep
.
– Andrew Henle
Aug 23 '17 at 11:09
add a comment |
If two lines in the file has an odd number of double quotes, the total sum of double quotes will be even, and you will not detect unbalanced quotes (this is what I presume you'd like to actually do, but I might be wrong).
This awk
script reports any line in the input line that has an odd number of quotes:
awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }'
We set the field separator (FS
) to "
with -F'"'
which means that if a line has an even number of fields it has odd quotes. NF
is the number of fields in the recent record, and NR
is the ordinal number of the current record ("the line number").
Given the following input:
$ cat file
cluster-env,"manage_dirs_on_root","true"
cluster-env,"one_dir_per_partition","false"
cluster-env,override_uid","true"
cluster-env,recovery_enabled","false"
we get
$ awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }' file
Line 3 has odd quoting: cluster-env,override_uid","true"
Line 4 has odd quoting: cluster-env,recovery_enabled","false"
Something like
$ grep -o '"' | wc -l
would return "14" for this file.
add a comment |
Another single gawk
approach:
awk -v RS=" 'END{print NR-1}'
add a comment |
Pure BASH:
var="$(< file.txt)"
tmp="${var//[^"]/}"
echo ${#tmp}
Istmp
an array? If yes,tmp
is an array of what?
– Tim
Aug 23 '17 at 0:14
@Tim, no.tmp
in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var
) rather than in a file as specified in the question.
– Wildcard
Aug 23 '17 at 3:33
add a comment |
try:
grep -0 '"' File -c
however, this will not work if two or more char in the same line. They will be counted as one char
1
Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
– fra-san
Dec 1 '18 at 20:12
add a comment |
grep -oF ' " ' file | wc -l
-F stands for fixed string
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%2f387656%2fhow-to-count-the-times-a-specific-character-appears-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can combine tr
(translate or delete characters) with wc
(count words, lines, characters):
tr -cd '"' < yourfile.cfg | wc -c
(-d
elete all characters in the c
omplement of "
, and then count the c
haracters.)
add a comment |
You can combine tr
(translate or delete characters) with wc
(count words, lines, characters):
tr -cd '"' < yourfile.cfg | wc -c
(-d
elete all characters in the c
omplement of "
, and then count the c
haracters.)
add a comment |
You can combine tr
(translate or delete characters) with wc
(count words, lines, characters):
tr -cd '"' < yourfile.cfg | wc -c
(-d
elete all characters in the c
omplement of "
, and then count the c
haracters.)
You can combine tr
(translate or delete characters) with wc
(count words, lines, characters):
tr -cd '"' < yourfile.cfg | wc -c
(-d
elete all characters in the c
omplement of "
, and then count the c
haracters.)
answered Aug 22 '17 at 12:35
Ulrich SchwarzUlrich Schwarz
9,60512846
9,60512846
add a comment |
add a comment |
grep approach:
grep -o '"' file | wc -l
16
-o
- output only matched substrings
Or with single gawk:
awk -v RS='' -v FPAT='"' '{print NF}' file
16
RS=''
- empty record separator (instead of newline)FPAT='"'
- pattern defining field value
-o
is a non-standard GNU extension to the standardgrep
utility. It's not mentioned in the POSIX documentation forgrep
.
– Andrew Henle
Aug 23 '17 at 11:09
add a comment |
grep approach:
grep -o '"' file | wc -l
16
-o
- output only matched substrings
Or with single gawk:
awk -v RS='' -v FPAT='"' '{print NF}' file
16
RS=''
- empty record separator (instead of newline)FPAT='"'
- pattern defining field value
-o
is a non-standard GNU extension to the standardgrep
utility. It's not mentioned in the POSIX documentation forgrep
.
– Andrew Henle
Aug 23 '17 at 11:09
add a comment |
grep approach:
grep -o '"' file | wc -l
16
-o
- output only matched substrings
Or with single gawk:
awk -v RS='' -v FPAT='"' '{print NF}' file
16
RS=''
- empty record separator (instead of newline)FPAT='"'
- pattern defining field value
grep approach:
grep -o '"' file | wc -l
16
-o
- output only matched substrings
Or with single gawk:
awk -v RS='' -v FPAT='"' '{print NF}' file
16
RS=''
- empty record separator (instead of newline)FPAT='"'
- pattern defining field value
edited Aug 22 '17 at 12:50
answered Aug 22 '17 at 12:43
RomanPerekhrestRomanPerekhrest
22.9k12346
22.9k12346
-o
is a non-standard GNU extension to the standardgrep
utility. It's not mentioned in the POSIX documentation forgrep
.
– Andrew Henle
Aug 23 '17 at 11:09
add a comment |
-o
is a non-standard GNU extension to the standardgrep
utility. It's not mentioned in the POSIX documentation forgrep
.
– Andrew Henle
Aug 23 '17 at 11:09
-o
is a non-standard GNU extension to the standard grep
utility. It's not mentioned in the POSIX documentation for grep
.– Andrew Henle
Aug 23 '17 at 11:09
-o
is a non-standard GNU extension to the standard grep
utility. It's not mentioned in the POSIX documentation for grep
.– Andrew Henle
Aug 23 '17 at 11:09
add a comment |
If two lines in the file has an odd number of double quotes, the total sum of double quotes will be even, and you will not detect unbalanced quotes (this is what I presume you'd like to actually do, but I might be wrong).
This awk
script reports any line in the input line that has an odd number of quotes:
awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }'
We set the field separator (FS
) to "
with -F'"'
which means that if a line has an even number of fields it has odd quotes. NF
is the number of fields in the recent record, and NR
is the ordinal number of the current record ("the line number").
Given the following input:
$ cat file
cluster-env,"manage_dirs_on_root","true"
cluster-env,"one_dir_per_partition","false"
cluster-env,override_uid","true"
cluster-env,recovery_enabled","false"
we get
$ awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }' file
Line 3 has odd quoting: cluster-env,override_uid","true"
Line 4 has odd quoting: cluster-env,recovery_enabled","false"
Something like
$ grep -o '"' | wc -l
would return "14" for this file.
add a comment |
If two lines in the file has an odd number of double quotes, the total sum of double quotes will be even, and you will not detect unbalanced quotes (this is what I presume you'd like to actually do, but I might be wrong).
This awk
script reports any line in the input line that has an odd number of quotes:
awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }'
We set the field separator (FS
) to "
with -F'"'
which means that if a line has an even number of fields it has odd quotes. NF
is the number of fields in the recent record, and NR
is the ordinal number of the current record ("the line number").
Given the following input:
$ cat file
cluster-env,"manage_dirs_on_root","true"
cluster-env,"one_dir_per_partition","false"
cluster-env,override_uid","true"
cluster-env,recovery_enabled","false"
we get
$ awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }' file
Line 3 has odd quoting: cluster-env,override_uid","true"
Line 4 has odd quoting: cluster-env,recovery_enabled","false"
Something like
$ grep -o '"' | wc -l
would return "14" for this file.
add a comment |
If two lines in the file has an odd number of double quotes, the total sum of double quotes will be even, and you will not detect unbalanced quotes (this is what I presume you'd like to actually do, but I might be wrong).
This awk
script reports any line in the input line that has an odd number of quotes:
awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }'
We set the field separator (FS
) to "
with -F'"'
which means that if a line has an even number of fields it has odd quotes. NF
is the number of fields in the recent record, and NR
is the ordinal number of the current record ("the line number").
Given the following input:
$ cat file
cluster-env,"manage_dirs_on_root","true"
cluster-env,"one_dir_per_partition","false"
cluster-env,override_uid","true"
cluster-env,recovery_enabled","false"
we get
$ awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }' file
Line 3 has odd quoting: cluster-env,override_uid","true"
Line 4 has odd quoting: cluster-env,recovery_enabled","false"
Something like
$ grep -o '"' | wc -l
would return "14" for this file.
If two lines in the file has an odd number of double quotes, the total sum of double quotes will be even, and you will not detect unbalanced quotes (this is what I presume you'd like to actually do, but I might be wrong).
This awk
script reports any line in the input line that has an odd number of quotes:
awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }'
We set the field separator (FS
) to "
with -F'"'
which means that if a line has an even number of fields it has odd quotes. NF
is the number of fields in the recent record, and NR
is the ordinal number of the current record ("the line number").
Given the following input:
$ cat file
cluster-env,"manage_dirs_on_root","true"
cluster-env,"one_dir_per_partition","false"
cluster-env,override_uid","true"
cluster-env,recovery_enabled","false"
we get
$ awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }' file
Line 3 has odd quoting: cluster-env,override_uid","true"
Line 4 has odd quoting: cluster-env,recovery_enabled","false"
Something like
$ grep -o '"' | wc -l
would return "14" for this file.
edited Aug 22 '17 at 16:21
answered Aug 22 '17 at 12:49
KusalanandaKusalananda
122k16230375
122k16230375
add a comment |
add a comment |
Another single gawk
approach:
awk -v RS=" 'END{print NR-1}'
add a comment |
Another single gawk
approach:
awk -v RS=" 'END{print NR-1}'
add a comment |
Another single gawk
approach:
awk -v RS=" 'END{print NR-1}'
Another single gawk
approach:
awk -v RS=" 'END{print NR-1}'
answered Aug 22 '17 at 19:06
αғsнιηαғsнιη
16.6k102865
16.6k102865
add a comment |
add a comment |
Pure BASH:
var="$(< file.txt)"
tmp="${var//[^"]/}"
echo ${#tmp}
Istmp
an array? If yes,tmp
is an array of what?
– Tim
Aug 23 '17 at 0:14
@Tim, no.tmp
in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var
) rather than in a file as specified in the question.
– Wildcard
Aug 23 '17 at 3:33
add a comment |
Pure BASH:
var="$(< file.txt)"
tmp="${var//[^"]/}"
echo ${#tmp}
Istmp
an array? If yes,tmp
is an array of what?
– Tim
Aug 23 '17 at 0:14
@Tim, no.tmp
in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var
) rather than in a file as specified in the question.
– Wildcard
Aug 23 '17 at 3:33
add a comment |
Pure BASH:
var="$(< file.txt)"
tmp="${var//[^"]/}"
echo ${#tmp}
Pure BASH:
var="$(< file.txt)"
tmp="${var//[^"]/}"
echo ${#tmp}
edited Aug 23 '17 at 7:15
answered Aug 22 '17 at 18:49
ThunderbeefThunderbeef
1606
1606
Istmp
an array? If yes,tmp
is an array of what?
– Tim
Aug 23 '17 at 0:14
@Tim, no.tmp
in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var
) rather than in a file as specified in the question.
– Wildcard
Aug 23 '17 at 3:33
add a comment |
Istmp
an array? If yes,tmp
is an array of what?
– Tim
Aug 23 '17 at 0:14
@Tim, no.tmp
in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var
) rather than in a file as specified in the question.
– Wildcard
Aug 23 '17 at 3:33
Is
tmp
an array? If yes, tmp
is an array of what?– Tim
Aug 23 '17 at 0:14
Is
tmp
an array? If yes, tmp
is an array of what?– Tim
Aug 23 '17 at 0:14
@Tim, no.
tmp
in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var
) rather than in a file as specified in the question.– Wildcard
Aug 23 '17 at 3:33
@Tim, no.
tmp
in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var
) rather than in a file as specified in the question.– Wildcard
Aug 23 '17 at 3:33
add a comment |
try:
grep -0 '"' File -c
however, this will not work if two or more char in the same line. They will be counted as one char
1
Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
– fra-san
Dec 1 '18 at 20:12
add a comment |
try:
grep -0 '"' File -c
however, this will not work if two or more char in the same line. They will be counted as one char
1
Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
– fra-san
Dec 1 '18 at 20:12
add a comment |
try:
grep -0 '"' File -c
however, this will not work if two or more char in the same line. They will be counted as one char
try:
grep -0 '"' File -c
however, this will not work if two or more char in the same line. They will be counted as one char
answered Dec 1 '18 at 19:41
Abdulkarim MalkadiAbdulkarim Malkadi
1
1
1
Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
– fra-san
Dec 1 '18 at 20:12
add a comment |
1
Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
– fra-san
Dec 1 '18 at 20:12
1
1
Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
– fra-san
Dec 1 '18 at 20:12
Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
– fra-san
Dec 1 '18 at 20:12
add a comment |
grep -oF ' " ' file | wc -l
-F stands for fixed string
add a comment |
grep -oF ' " ' file | wc -l
-F stands for fixed string
add a comment |
grep -oF ' " ' file | wc -l
-F stands for fixed string
grep -oF ' " ' file | wc -l
-F stands for fixed string
answered 13 mins ago
shinekshinek
87116
87116
add a comment |
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%2f387656%2fhow-to-count-the-times-a-specific-character-appears-in-a-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