Sponsored Content
Top Forums Shell Programming and Scripting Counting the number of occurances of all characters (a-z) in a string Post 302126017 by craigp84 on Monday 9th of July 2007 05:42:05 PM
Old 07-09-2007
Not trying to win any awards for maintainability... Smilie

Code:
#!/usr/bin/perl
#
# count.pl
#

use strict;
use warnings;

my $char;
my %chars=();

print "Enter a line: ";

while( $char = getc() ) {
  last if( $char =~ /\n/ );
  $chars{ $char }++;
}

my @keys = sort keys( %chars );
foreach my $key (@keys) {
  print "$key = $chars{ $key }\n";
}

Code:
$ perl count.pl
Enter a line: This is a test
  = 3
T = 1
a = 1
e = 1
h = 1
i = 2
s = 3
t = 2

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Counting Occurances in Two Files

I have two files I want to compare, one is a list of variables and the other is a text file COBOL program. Basically what I want to do is display only those variables that appear in the COBOL program only once. However I would also accept a count of each variable as it appears in the COBOL... (2 Replies)
Discussion started by: Keith Gergel
2 Replies

2. UNIX for Dummies Questions & Answers

count the number of files which have a search string, but counting the file only once

I need to count the number of files which have a search string, but counting the file only once if search string is found. eg: File1: Please note that there are 2 occurances of "aaa" aaa bbb ccc aaa File2: Please note that there are 3 occurances of "aaa" aaa bbb ccc... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies

3. Shell Programming and Scripting

number of characters in a string

Hi there, I have some user input in a variable called $VAR, and i need to ensure that the string is 5 or less characters .... does anybody know how i can count the characters in the variables ? any help would be great, cheers (2 Replies)
Discussion started by: rethink
2 Replies

4. Shell Programming and Scripting

How to count number of occurances of string in a file?

Gurus, Need little guidance. I have A.txt and B.txt file. B.txt file contains Unique strings. Sample content of B.txt file for which i cut the fourth column uniquely and output directed to B.txt file And A.txt file contains the above string as a fourth column which is last column. So A.txt... (7 Replies)
Discussion started by: Shirisha
7 Replies

5. UNIX for Dummies Questions & Answers

AWK - number of specified characters in a string

Hello, I'm new to using AWK and would be grateful for some basic advice to get me started. I have a file consisting of 10 fields. Initially I wish to calculate the number of . , ~ and ^ characters in the 9th field ($9) of each line. This particular string also contains alphabetical... (6 Replies)
Discussion started by: Olly
6 Replies

6. UNIX for Dummies Questions & Answers

counting occurrence of characters in a string

Hello, I have a string like this 0:1:2:0:2:2:4:0:0:0:-200:500...... what i want is to break down how many different characters are there and their count. For example for above string it should display 0 - 5 times 1 - 1 times 2 - 3 times 4 - 1 times . . . I am stuck in writing... (8 Replies)
Discussion started by: exit86
8 Replies

7. Shell Programming and Scripting

Counting number of records with string row delimiter

HI, i have a file like this t.txt f1|_f2|_ f1|_f2|_ f1|_f2|_ as if col delimiter is |_ and row delimiter |_\n trying to count number of records using awk $ awk 'BEGIN{FS="|_" ; RS="~~\n"} {n++}END{print n} ' t.txt 7 wondering how can i count this to 3 ? thx (9 Replies)
Discussion started by: aksforum
9 Replies

8. Shell Programming and Scripting

counting the number of characters in the filename of all files in a directory?

I am trying to display the output of ls and also print the number of characters in EVERY file name. This is what I have so far: #!/bin/sh for x in `ls`; do echo The number of characters in x | wc -m done Any help appreciated (1 Reply)
Discussion started by: LinuxNubBrah
1 Replies

9. Shell Programming and Scripting

Counting the number of characters

Hi all, Can someone help me in getting the following o/p I/p:... (7 Replies)
Discussion started by: Sri3001
7 Replies

10. Shell Programming and Scripting

Counting number of single quotes in a string

i need to be able to count the number of single quotes ' in the entire string below: "description":"DevOps- Test VM's, System Admins Test VM's ", awk can most likely do this, but here's my attempt using egrep: echo "${STRING}" | egrep -wc '"'"\'"'"' or echo "${STRING}" | egrep -wc... (11 Replies)
Discussion started by: SkySmart
11 Replies
ESCAPESHELLCMD(3)							 1							 ESCAPESHELLCMD(3)

escapeshellcmd - Escape shell metacharacters

SYNOPSIS
string escapeshellcmd (string $command) DESCRIPTION
escapeshellcmd(3) escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands. This function should be used to make sure that any data coming from user input is escaped before this data is passed to the exec(3) or system(3) functions, or to the backtick operator. Following characters are preceded by a backslash: #&;`|*?~<>^()[]{}$, x0A and xFF. ' and " are escaped only if they are not paired. In Windows, all these characters plus % are replaced by a space instead. PARAMETERS
o $command - The command that will be escaped. RETURN VALUES
The escaped string. EXAMPLES
Example #1 escapeshellcmd(3) example <?php // We allow arbitrary number of arguments intentionally here. $command = './configure '.$_POST['configure_options']; $escaped_command = escapeshellcmd($command); system($escaped_command); ?> Warning escapeshellcmd(3) should be used on the whole command string, and it still allows the attacker to pass arbitrary number of argu- ments. For escaping a single argument escapeshellarg(3) should be used instead. SEE ALSO
escapeshellarg(3), exec(3), popen(3), system(3), backtick operator. PHP Documentation Group ESCAPESHELLCMD(3)
All times are GMT -4. The time now is 08:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy