The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to replace special chars like " ' " (Apostrophe) onculo UNIX for Dummies Questions & Answers 4 10-26-2008 09:59 PM
Removing " " chars using Awk vijaya2006 Shell Programming and Scripting 3 04-07-2008 12:13 PM
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 03:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 04:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-07-2009
silkiechicken silkiechicken is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 21
Unhappy 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.



http://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?



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


http://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 (permalink)  
Old 02-07-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,839
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 (permalink)  
Old 02-07-2009
silkiechicken silkiechicken is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 21
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 (permalink)  
Old 02-07-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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 (permalink)  
Old 02-10-2009
silkiechicken silkiechicken is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 21
Thank you for the help.

I will play with that for a bit and see what I can do!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0