Counting a chars IF == "x"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting a chars IF == "x"
# 1  
Old 02-07-2009
Data Counting a chars IF == "x"

I'm new at this script stuff... only have minor exposure to java.

My problem is largely syntax and being unable to figure out what the manuals are telling me what each option does. Basically I have a hard time understanding the documentation and need help with what awk is capable of on the shell command line.

I have a file and if the line does not start with a ">" I want to count all the characters but not spaces. Just [A-Z]. In this case I know the characters are going to be A E or Z.

My thinking has got me this far so far..

If myfile is "ATHROUGHZ SPACES"

grep -v ">" myfile.tab | awk if nextChar == "A || E || Z" {++count} END {print count}'


I want it to return 4, because there are two A, one E and one Z.



The grep part was to select for the lines that did not contain ">".



The {++count} END {print count}' part is to count and then print the count


However I am stuck in the middle part which is highlighted in red.



I haven't found (or understood how to use) anything that lets me go through the line character by character to compare it with if it is a desired letter and count it if it is a match.


I found the following threads but don't understand them enough to apply them to my situation.



https://www.unix.com/shell-programming-scripting/9721-counting-characters.html
From this one it appears that -F won't work to seperate the characters because it is used to seperate strings?



https://www.unix.com/unix-dummies-questions-answers/58760-counting-occurence-particular-characters.html
From this one, I don't have seperators between the characters


https://www.unix.com/shell-programming-scripting/39765-counting-number-occurances-all-characters-z-string.html
This thread is in reference to PEARL which I am not familar with, and not sure how is to be applied to just the command line?

If anyone has any suggestions or good directions to point me for what feels like should be incredibly easy to do, it would be awesome!

Thanks a bunch!

-Diana
# 2  
Old 02-07-2009
Don't know about awk, but in Perl (if you only want it only on the command line) you would write it like this
Code:
$ perl -e '$count=0; while(<>){next if /^>/; @line=split //; foreach(@line){$count++ if /[AEZ]/;}} print $count."\n";' myfile.tab

# 3  
Old 02-07-2009
Thank you for the reply.

I don't know anything about PEARL yet, but I am sure your example there will be useful once I can read up on it more.

I'm about 30 hours in to my first Linux style command line experience so am still a n00b.
# 4  
Old 02-07-2009
To count chars in the A-Z range
Code:
$ echo 'ATHROUGHZ SPACES' | nawk '!/^>/ {print gsub(/[A-Z]/, "")}'
15

$ echo '>ATHROUGHZ SPACES' | nawk '!/^>/ {print gsub(/[A-Z]/, "")}'

To count ALL the chars, but spaces ' '
Code:
$ echo 'ATHROUGHZ SPACES' | nawk '!/^>/ {print gsub(/[^ ]/, "")}'
15

$ echo '>ATHROUGHZ SPACES' | nawk '!/^>/ {print gsub(/[^ ]/, "")}'

Pick your poison.
# 5  
Old 02-10-2009
Thank you for the help.

I will play with that for a bit and see what I can do!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Programming

Bug in "Word Counting" Program

I have written a simple program that counts the number of words in the input stream. There is a small bug in the code and i am not able to figure out the cause of this bug. #include <stdio.h> int main() { int ichar = 0; int in_word = 1; // in_word = 1 *outside a word* in_word = 0... (4 Replies)
Discussion started by: sreeharshasn
4 Replies

4. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Delete chars after dot "."

Hi, I require to delete all characters after a dot "." from a string. For e.g. input_var="/home/dips/file_1_20100726.txt.gz /home/dips/file_2_20100726.txt.gz /home/dips/file_3_20100726.txt.gz" output_var="/home/dips/file_1_20100726 /home/dips/file_2_20100726... (3 Replies)
Discussion started by: dips_ag
3 Replies

7. UNIX for Dummies Questions & Answers

How to replace special chars like " ' " (Apostrophe)

I'm goin to drive crazy soon, if i can not do this. I have a text file (570kb) and i have to replace the apostrophe " ' " and minus "-" with space " ". i have done it for minus: sed 's/-/ /g' aaa.txt >zzz.txt this replaced minus with space. but i can not use the same command for ' . ... (4 Replies)
Discussion started by: onculo
4 Replies

8. Shell Programming and Scripting

Removing " " chars using Awk

HI Friends, I am trying to elliminate the " " characters from the word: "hello" using awk. I need the output to be just = hello (without " " chars). Is there any way to do this ? Thanks! (3 Replies)
Discussion started by: vijaya2006
3 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question