Sponsored Content
Top Forums UNIX for Dummies Questions & Answers AWK - number of specified characters in a string Post 302488664 by m1xram on Tuesday 18th of January 2011 06:02:07 AM
Old 01-18-2011
Tricky

So..

A char is RE ([a-zA-Z][\^\$\~])

A fill is RE [\.,]

Freq = fills / (chars + fills)

Input:
Code:
F1 F2 F3 F4 Aa$.c$A..,.$,.,$.^~.
F1 F2 F3 F4 one^~.two.a$threeA$four.five$six
F1 F2 F3 F4 a$,b~c.d^efg
F1 F2 F3 F4 abc
F1 F2 F3 F4 ,,,,
F1 F2 F3 F4 789790

Code:
BEGIN {
    myRE = "(\\^~\\.)|(A\\$)|(\\.\\$)|[\\.\\$]";
    myChar = "[a-zA-Z][\\$\\~\\^]?";
    myFill = "[\\.\\,]";
    m = "^~. .$ . A$ A";
    acnt = split(m, astr, " ");
}
{
    print $0;
    str = $5;
    print str;
    chars = gsub(myChar, "+", str);
    print "/" str "/, found " chars;
        fills = gsub(myFill, "x", str);
    print "/" str "/, found " fills;
    if (chars + fills) {
        printf("freq = %.1f%%\n", fills * 100.0 / (fills + chars));
    } else {
        print "freq = undef";
    }
}

Output:
Code:
F1 F2 F3 F4 Aa$.c$A..,.$,.,$.^~.
Aa$.c$A..,.$,.,$.^~.
/++.++..,.$,.,$.^~./, found 4
/++x++xxxx$xxx$x^~x/, found 10
freq = 71.4%
F1 F2 F3 F4 one^~.two.a$threeA$four.five$six
one^~.two.a$threeA$four.five$six
/+++~.+++.+++++++++++.+++++++/, found 24
/+++~x+++x+++++++++++x+++++++/, found 3
freq = 11.1%
F1 F2 F3 F4 a$,b~c.d^efg
a$,b~c.d^efg
/+,++.++++/, found 7
/+x++x++++/, found 2
freq = 22.2%
F1 F2 F3 F4 abc
abc
/+++/, found 3
/+++/, found 0
freq = 0.0%
F1 F2 F3 F4 ,,,,
,,,,
/,,,,/, found 0
/xxxx/, found 4
freq = 100.0%
F1 F2 F3 F4 789790
789790
/789790/, found 0
/789790/, found 0
freq = undef


//, found 0
//, found 0
freq = undef

There's some extra code in there, ignore it. The input has illegal numbers and a blank line at the end, just for fun.

Question: Can a character have more than one modifier? If so then alter the RE myChar '?' to be '*'.

You'll have to use the REs above with the match loop from the previous example if you want the actual strings. But, function gsub() is a good hack for what you described.

Output should have some break between records, it's hard to read. Toss in a print "--------------" somewhere for clarity.

Last edited by m1xram; 01-18-2011 at 07:04 AM.. Reason: add "Output:"
This User Gave Thanks to m1xram For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting the number of occurances of all characters (a-z) in a string

Hi, I am trying out different scripts in PERL. I want to take a line/string as an input from the user and count the number of occurrances of all the alphabets (a..z) in the string. I tried doingit like this : #! /opt/exp/bin/perl print "Enter a string or line : "; $string = <STDIN>; chop... (5 Replies)
Discussion started by: rsendhilmani
5 Replies

2. Programming

Count the number of repeated characters in a given string

i have a string "dfasdfasdfadf" i want to count the number of times each character is repeated.. For instance, d is repeated 4 times, f is repeated 4 times.. can u give a program in c (1 Reply)
Discussion started by: pgmfourms
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

help: Awk to control number of characters per line

Hello all, I have the following problem: My input is two sorted files: file1 >1_19_130_F3 T01220131330230213311013000000110000 >1_23_69_F3 T01200211300200200010000001000000 >1_24_124_F3 T010203113002002111111200002010 file2 >1_19_130_F3 24 18 9 18 23 4 11 4 5 9 5 8 15 20 4 4 7 4... (9 Replies)
Discussion started by: DerSeb
9 Replies

5. Shell Programming and Scripting

Awk to extract lines with a defined number of characters

This is my problem, my file (file A) contains the following information: Now, I would like to create a file (file B) containing only the lines with 10 or more characters but less than 20 with their corresponding ID: Then, I need to compare the entries and determine their frequency. Thus, I... (7 Replies)
Discussion started by: Xterra
7 Replies

6. Shell Programming and Scripting

How to truncate a string to x number characters?

Hello: I have a large file which contains lines like the following: 1/t123ab, &Xx:1:1234:12345:123456@ABCDEFG... at -$100.00% /t is a tab, spaces are as indicated the string "&Xx:1:1234:12345:123456$ABCDEFG..." has a slightly variable number of numbers and letters, but it always starts... (9 Replies)
Discussion started by: Tectona
9 Replies

7. Shell Programming and Scripting

Help awk/sed: putting a space after numbers:to separate number and characters.

Hi Experts, How to sepearate the list digit with letters : with a space from where the letters begins, or other words from where the digits ended. file 52087mo(enbatl) 52049mo(enbatl) 52085mo(enbatl) 25051mo(enbatl) The output should be looks like: 52087 mo(enbatl) 52049... (10 Replies)
Discussion started by: rveri
10 Replies

8. Shell Programming and Scripting

Replace characters in string with awk gsub

Hi I have a source file that looks like a,b,c,d,e,f,g,h,t,DISTI(USD),MSRP(USD),DIST(EUR),MSRP(EUR),EMEA-DISTI(USD),EMEA-MSRP(USD),GLOBAl-DISTI(USD),GLOBAL-MSRP(USD),DISTI(GBP), MSRP(GBP) I want to basically change MSRP(USD) to MSRP,USD and DIST(EUR) to DIST,EUR and likewise for all i'm using... (3 Replies)
Discussion started by: r_t_1601
3 Replies

9. Shell Programming and Scripting

awk to print column number while ignoring alpha characters

I have the following script that will print column 4 ("25") when column 1 contains "123". However, I need to ignore the alpha characters that are contained in the input file. If I were to ignore the characters my output would be column 3. What is the best way to print my column of interest... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

10. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies
All times are GMT -4. The time now is 06:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy