LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-03-2008, 01:26 AM   #1
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Rep: Reputation: 16
Finding Perticular Character in a file


we are successfully count the NO. of words in a file . But i am unable to count a Particular Character in that file . i mean i just want to know how many times the particular CHARACTER is used in the file.
 
Old 07-03-2008, 01:36 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 56
Show what you have so far.
 
Old 07-03-2008, 06:00 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,528

Rep: Reputation: 56
Code:
#!/usr/bin/perl -np
# A highly not-understandable implementation in Perl ;)
# run this with the argument being the file to analyse

END () {
        print "COUNT Character\n";
        print "----- ---------\n";
        map {
                $c = $_;
                s/\n/\\n/;
                s/\t/\\t/;
                printf "%5d %s\n", $n{$c}, $_;
        } sort { $n{$a} <=> $n{$b} } keys %n;
}

# process input (don't forget we use the -n and -p options on the
# shabang line, so we don't need to explicitly iterate over input
# lines

map {$n{$_}++; } split("");

Last edited by matthewg42; 07-03-2008 at 06:03 AM.
 
Old 07-03-2008, 08:30 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by dina3e View Post
we are successfully count the NO. of words in a file . But i am unable to count a Particular Character in that file . i mean i just want to know how many times the particular CHARACTER is used in the file.
suggestion:
Code:
sed 's/[[:print:]]/&\n/g' file
use sort and uniq (check uniq man page for option that can count occurences )
 
Old 07-08-2008, 08:37 PM   #5
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by ghostdog74 View Post
suggestion:
Code:
sed 's/[[:print:]]/&\n/g' file
use sort and uniq (check uniq man page for option that can count occurences )
well this works very interstingly with the use of the sort & uniq -c .
 
Old 07-11-2008, 02:31 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,016

Rep: Reputation: 91
C for a change
Code:
#include <stdio.h>



char * ascii[] = {
"NUL", "SOH", "STX", "ETX",
 "EOT", "ENQ", "ACK", "BEL",
 "BS", "HT", "NL", "VT",
 "NP", "CR", "SO", "SI",
 "DLE", "DC1", "DC2", "DC3",
 "DC4", "NAK", "SYN", "ETB",
 "CAN", "EM", "SUB", "ESC",
 "FS", "GS", "RS", "US",
 "SP", "!", "\"", "#", "$",
 "%", "&", "'", "(", ")",
 "*", "+", ",", "-", ".",
 "/", "0", "1", "2", "3",
 "4", "5", "6", "7", "8",
 "9", ":", ";", "<", "=",
 ">", "?", "@", "A", "B",
 "C", "D", "E", "F", "G",
 "H", "I", "J", "K", "L",
 "M", "N", "O", "P", "Q",
 "R", "S", "T", "U", "V",
 "W", "X", "Y", "Z", "[",
 "\\", "]", "^", "_", "`",
 "a", "b", "c", "d", "e",
 "f", "g", "h", "i", "j",
 "k", "l", "m", "n", "o",
 "p", "q", "r", "s", "t",
 "u", "v", "w", "x", "y",
 "z", "{", "|", "}", "~",
 "DEL"};


static int array[256];
static int total;
main()
{

    int ch;
    while ((ch = fgetc(stdin)) != EOF){
	array[ch]++;
	total++;
    }
    for (ch=0; ch < 256; ch++) {
	printf("%d (%s) %d %.3f%%\n", ch,
				    isascii(ch)? ascii[ch]:"",
				    array[ch],
				    100 * (float) array[ch]/ (float) total

				    );
    }
}
 
Old 07-11-2008, 11:07 PM   #7
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
Thanks for the C code to find out the Number of the character in the inputed file .
 
  


Reply

Tags
character, count


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
log of a perticular folder mujahed_khan Linux - Newbie 4 02-10-2008 04:03 PM
Finding a grave character (~) atari05 Linux - Newbie 3 01-09-2008 02:54 AM
How to give authenticated access to a web site for perticular IP addresses sharad Linux - Networking 1 05-29-2006 04:40 AM
How to know on termination or exit of a perticular process in Unix Pavan mahoorker Linux - Software 1 06-07-2005 07:28 AM
run a perticular script before loggin in rohan208 Linux - General 1 05-07-2004 11:37 AM


All times are GMT -5. The time now is 11:33 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration