count characters in multiple files and sort

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count characters in multiple files and sort
# 1  
Old 06-24-2011
count characters in multiple files and sort

I am trying to count a special character in several thousand files and sort the result according to the number the character occured in each files.


files look like:

I want the number of B in each file and a result file that lists the occurence of B in each file.

3.txt 4
1.txt 1
2.txt 3

I have tried:
ls *txt | xargs grep -c "B"
from there I want to pipe the output into 'sort'
But 'grep -c' gives only the number of lines, not the number of characters.
# 2  
Old 06-24-2011
Try this in directory containing those thousands of files:
perl -n0e '@n=/B/g;print "$ARGV ",$#n+1,"\n"' * | sort -rnk2 > result

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-24-2011
Not much time, but perhaps a start...

$ cat sample1.txt

$ cat sample1.txt | sed s/./\&~/g | tr "~" "\n" | grep "B" | wc -l

This User Gave Thanks to joeyg For This Post:
# 4  
Old 06-24-2011
Thanks a lot bartus11.
The script works perfectly fine.
Best regards
# 5  
Old 06-24-2011
# cat file

try search "B" ...
# sed 's/[^B]//g' file|sed ':a;$!N;s/\n//;ta'|sed -e 's/B/&\n/g'|sed -ne '/^$/!p'|sed -n '$='
# awk -F'B' '{n+=NF-1}END{print n}' file
# grep -o "B" file |wc -l
# tr -cd 'B' <file| wc -c

This User Gave Thanks to ygemici For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort and move multiple files by modification date

Hi I have a problem, I have a large group of archive files in a folder some are later versions of the same archive, the only difference btween them is that the archiving program we use appends the name with a code for it to keep track of in its data base, and the modification date. I am starting... (6 Replies)
Discussion started by: Paul Walker
6 Replies

2. Shell Programming and Scripting

Count Unique values from multiple lists of files

Looking for a little help here. I have 1000's of text files within a multiple folders. YYYY/ /MM /1000's Files Eg. 2014/01/1000 files 2014/02/1237 files 2014/03/1400 files There are folders for each year and each month, and within each monthly folder there are... (4 Replies)
Discussion started by: whegra
4 Replies

3. Shell Programming and Scripting

Count files between multiple directories

Hi All, Everyday we will receive 33 files in our source directory(/prd/pk) with the current date. Once our jobs are completed all the 33 files immediately will be moved to backup folder (/prd/pk/backup). Now, I need to check between source file directory (/prd/pdk) and backup file directory... (3 Replies)
Discussion started by: suresh_target
3 Replies

4. Shell Programming and Scripting

Count the number of occurances for multiple files

I have some text files as shown below. I would like to add the values of each string. Your help would be appreciated!! file1.txt SUS 2 PRS 2 ALI 1 PRS 1 GLK 2 file2.txt PRS 3 GLK 6 SUS 18 Desired output SUS 20 PRS 6 (2 Replies)
Discussion started by: arch
2 Replies

5. Shell Programming and Scripting

Count lines from multiple files (3)

Hey everyone, I've to count lines from string of files names then to show sum output of lines. for example: read x = F1 F2 F3 F1 = 12 lines F2 = 14 lines F3 = 10 lines = 36 what I did is: read x echo $x >|temp for x in $(cat temp) do wc -l < $x (3 Replies)
Discussion started by: Aviv
3 Replies

6. Shell Programming and Scripting

How to count the number of occurrence of words from multiple files?

File 1 aaa bbb ccc File 2 aaa xxx zzz bbb File 3 aaa bbb xxx Output: (4 Replies)
Discussion started by: Misa-Misa
4 Replies

7. UNIX for Dummies Questions & Answers

Unix command to count the number of files with specific characters in name

Hey all, I'm looking for a command that will search a directory (and all subdirectories) and give me a file count for the number of files that contain specific characters within its filename. e.g. I want to find the number of files that contain "-a.jpg" in their name. All the searching I've... (6 Replies)
Discussion started by: murphysm
6 Replies

8. UNIX for Advanced & Expert Users

grep count across multiple files

I have a number of simulation log files and I want to get a total count of the "PASSED" expression in them. If I use grep -c <files>, grep would give a tally for each file. I just want one number, the total count. How do I do that? (4 Replies)
Discussion started by: CrunchMunch
4 Replies

9. UNIX for Dummies Questions & Answers

trying to count lines in multiple files

Hi there, I need help. I want to run the command: less filename | wc -l But on multiple files in a directory So to get those files I would run ls -ltr | grep filename_2000123 or of course ls -ltr *filename_2000123* But I am having a problem running a loop to get a count of each... (1 Reply)
Discussion started by: llsmr777
1 Replies

10. Shell Programming and Scripting

sort & match multiple files

Hi, I have some question and need some guidance how to sort and match multiple files. 1. all the data in the files are numbers e.g. 1234567 1584752 2563156 2. each sorted file have their own ouput. e.g. test.csv -> test_sorted.csv 3. Then, I need to match all... (4 Replies)
Discussion started by: nazri76
4 Replies
Login or Register to Ask a Question