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 characters. Lets call this value "count". I presume the length function would be used here, but am unsure how to specify multiple criteria.
I'd then like to create a new field at the end of each line that equals count/$8.
A slightly more complicated query - Can the split function in gawk count instances of multiple characters that act as separators? For example ".$" or ".^~".
A typical line of data for me looks something like:
F1 F2 F3 F4 Aa$.c$A..,.$,.,$.^~.
In $5 I'd like to count instances of "A" "A$" "." ".$" "^~." etc.
There are some issues with the match strings you supplied. For one some match strings are substrings of others. If you wanted to know occurrences of ".$" and "." you'd have to look at the count of long strings - count of short strings to determine unique short strings. Order is important.
Output:
Matches that have a count of 1 essentially didn't do a split. If you change "c" to "c - 1" in the program it will show the number of matches that caused splits. You can see that matches 1 and 4 didn't do anything, match 1 probably because it was at the end.
If you take match 3 - match 2 you'd have the unique matches for ".". The same is true for match 4 and 5 relative to "$".
Ok, enough of that, lets talk about actual delimiters.
Suppose you wrote a regular expression (RE) to split $5. That RE could be used in the AWK match() function which would return starting position and length. Those values could be used to pull substrings from $5 which include the split values and the actual delimiters. From the delimiter strings you could build a frequency list per line and or per file. Is that what you really had in mind to do?
For me it would be a bit easier to create it in PERL. Here it is in AWK.
Input:
Output:
Well that should do it. You could sort the frequency list of course but don't use AWK as it either whacks the indices or sorts the indices, both of which you don't want.
Thankyou once again m1xram for your detailed & insightful ideas.
Your code does a great job and actually outputs much more information that I had expected - but thats because of my superficial problem description. Nonetheless, it will be a valuable resource for me.
In the interim I did some tinkering with my crude code, and it seems to work too - though outputting much less. What I ultimately was trying to achieve was to insert a new field that calculates the frequency of "," or "." in my string relative to "[a-zA-Z]" - BUT once all those same characters with either $ or ~^ next to them had been removed (those extra characters act as indicators/modifiers of their adjacent character).
Despite what I'd thought, split does seem to search for groups of characters if you enclose them in parentheses.
This is my input data:
$8 is the stringof interest. $7 is the number of "characters" in $8, where a character with a modifier like A$ is treated as one single character. $9 is the frequency of . or , in the $8 string.
The code I used was:
Which gave me an outputs of:
This adds the new frequencies, and the number of "non-modified" characters in the $8 string. You can see that where there are $ in the string the new & old frequencies differ as does the number of "characters" in the string, but otherwise they remain unchanged.
Cheers,
Olly
Last edited by Franklin52; 01-17-2011 at 04:56 AM..
Reason: Please use code tags and indent your code
Input:
Output:
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:"
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)
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)
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)
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)
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)
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)
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)
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)