To count total of specific character in a file and save its value to a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers To count total of specific character in a file and save its value to a variable
# 1  
Old 05-03-2015
Ubuntu To count total of specific character in a file and save its value to a variable

Hi all, I have a file that contains characters. How do I get total of spesific character from that file and save the count to a variable for doing for calculation.

data.txt
Code:
1
2
2
2
2
3
3
4
5
6
7
8
5
4
3
4
5
6

i want to get total of 4's character in that file and save it for calculation for axample.

thanks first for your help..

Last edited by weslyarfan; 05-03-2015 at 11:50 AM.. Reason: wrong sentences
# 2  
Old 05-03-2015
With one character per line, try:
Code:
grep -Fxc 4 file

or
Code:
awk '$1==4{t++} END{print t}' file


--
or for any number of 4's, try:

Code:
tr -cd 4 < file | wc -c

or perhaps: (bash/ksh93/zsh)
Code:
v=$(<file)
v=${v//[!4]}
echo ${#v}


Last edited by Scrutinizer; 05-03-2015 at 10:57 PM..
# 3  
Old 05-03-2015
If you ever want to look for the number of strings (not just single characters) matching an extended regular expression with zero or more occurrences of the ERE on each line in a file, try:
Code:
awk -v pat="ERE" '{c+=gsub(pat,"")}END{print c}' data.txt

For example, if you wanted to find the number of occurrences of 4 and 6 in data.txt:
Code:
awk -v pat="[46]" '{c+=gsub(pat,"")}END{print c}' data.txt

which, with your sample input file, produces the output:
Code:
6

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 05-03-2015
How do I get the total of spesific character and make a variable to take in the total spesific character?

---------- Post updated at 09:18 AM ---------- Previous update was at 08:02 AM ----------

How do I get the total of spesific character and make a variable to take in the total spesific character?
# 5  
Old 05-03-2015
You can take any of the commands above and use command substitution:
Code:
var=$(cmd)

or in the case of the last example in post #2, you can assign the result to a variable:
Code:
var=${v#}


Last edited by Don Cragun; 05-03-2015 at 11:25 PM.. Reason: Fix typo (s/#1/#2/).
# 6  
Old 05-04-2015
Better: var=${#v}
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Save an specific part of a expect_out in a variable

I have a expect file like this #!/opt/tools/unsupported/expect-5.39/bin/expect spawn ssh -l user ip expect_after eof {exit 0} set timeout 10 log_file /report.txt expect "Password:" { send "pasword\r" } expect "$ " { send "date\r" } expect "$ " { send "readlink /somelink\r" } set... (7 Replies)
Discussion started by: bebehnaz
7 Replies

3. Shell Programming and Scripting

Total count in each category for given file list

I have list of file names in filename.txt below is file format >>File1 _________________________ 01~12345~Y~YES~aaaaa~can 02~23456~N~NO~bbbbb~can . . . 99~23__________________________ Need to find total count from each file depending on specific string and add them to have total count... (17 Replies)
Discussion started by: santoshdrkr
17 Replies

4. UNIX for Dummies Questions & Answers

How to find count total number of pattern in a file …?

How to find count total number of pattern in a file … File contains : a.txt ------------- aaa bbb nnn ccc aaa bbb aaa ddd aaa aaa aaa aaa grep -c aaa a.txt Op: 4 ( But my requirement is should count the total no of patterns as 7 ) (4 Replies)
Discussion started by: Jitten
4 Replies

5. Shell Programming and Scripting

Delete line based on count of specific character

I'm looking for what I hope might be a one liner along these lines: sed '/a line with more than 3 pipes in it/d' I know how to get the pipe count in a string and store it in a variable, but I'm greedy enough to hope that it's possible via regex in the /.../d context. Am I asking too much? ... (5 Replies)
Discussion started by: tiggyboo
5 Replies

6. UNIX for Advanced & Expert Users

Count specific word or character per line

Hi, I need help regarding counting specific word or character per line and validate it against a specific number i.e 10. And if number of character equals the specific number then that line will be part of the output. Specific number = 6 Specific word or char = || Sample data:... (1 Reply)
Discussion started by: janzper
1 Replies

7. UNIX for Advanced & Expert Users

Count total file downloaded using FTP

Hi All, I'm developing a FTP script as below: ftp -v -n <IP_ADDRESS> << EOF user avery jSqaqUU2 lcd /directory/folder/ ascii prompt mget * bye EOF I would like to enhance the script to count the total file downloaded. For example, once the script run i want the message "Total <n>... (1 Reply)
Discussion started by: cas553
1 Replies

8. Shell Programming and Scripting

Count specific character(s) very large file

I'm trying to count the number of 2 specific characters in a very large file. I'd like to avoid using gsub because its taking too long. I was thinking something like: awk '-F' { t += NF - 1 } END {print t}' infile > outfile which isn't working Any ideas would be great. (3 Replies)
Discussion started by: dcfargo
3 Replies

9. HP-UX

count occurences of specific character in the file

For counting the occurences of specific character in the file I am issuing the command grep -o 'character' filename | wc -w It works in other shells but not in HP-UX as there is no option -o for grep. What do I do now? (9 Replies)
Discussion started by: superprogrammer
9 Replies
Login or Register to Ask a Question