LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-11-2009, 11:42 PM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Rep: Reputation: 15
Script to count # of chars per line (if line meets certain criteria) and get avg #?


Hi all,

I have several files with many lines something like this:

>Contig155; 141 117 387 minus strand; translated
MVRQEVGRYSYNPLAQCRCFTGLRKLGNLISTEVEFTLVSIPKPCSISVLGTLWVQNLGTHTFLTGAGQSQGRDKKKGDDAGFHVVDVR
>Contig154 188 1 440 ; translated
SFSSFLHSHLEESFKMVRMNVLADALKSICNAEKRGKRQVLIRPCSKVIVKFLTVMMKHGYIGEFEIVDDHRNGKIVVNLTGRINKCGVISPRFDVALRD LETWTTNLLPSRQFGFLVLTTSGGIMDHEEARRKHLGGKILGFFF
>Contig153 27 1 184 ; translated
AILTADGAAAGHHAPHVLYNFFAIDLGXCTVDDAVGFIXALVYRDVVLYRNAPDNVYVAV
>Contig152 193 1 544 ; translated
LPVSSDQGKMPLPHYEEWGLALVGFAGAIGFNIVKRRPPYARIHMHAIGAVGGYLLGGIVHNDWERRARAEKIYIEDYVRLHPEDFVEPPPKLYKDVFYL GLQFEEHSQKKRLQINCVRVIVTSEKAEAIVNVTLVWCGQCEYGYSSFQSRSSTNATGRAILFITLSRKTLEHCEYMYS
>Contig151 -4 1 311 ; translated
WHCRPWAPGVLEYSVLFTRFFALINNVIHHDFLAQAGLNYIQYSSICARAVRRCLKGDFKIEALRREDSVIKTNRWEGGKVVKREAGQHVEHLPEVIQAD LV
>Contig150 179 1 407 ; translated
FPRYKRLRYVGTICIGVFAVIALVVAQDSTTMSGTTSGGSGTTQKSSFVMMTGIATVTVLDLEDSAVRTTTCATIAKPQPKPAVLLKGTVWKAVVIIMSS LVSVSMDNVDVRTLVEXGKMKERADICNGDIVKC
>Contig149; 156 313 471 minus strand; translated
MARFLNNPSSCFTLFLSYGMPLITVTMRQAANSTTMALNILDSCVGTVLNTNQ

I'm trying to write a script that will count the number of characters per line that doesn't contain a ">" symbol and give me an average of those values. I have most of the script together but I can't figure out how to connect some of the steps. Can anyone help? Thanks!!!

Code:
grep -v \> Chaetoderma_nitidulum | wc -m | ?? divide outout of wc -m by value returned by this command: grep -c \> file.txt ??
 
Old 09-12-2009, 01:09 AM   #2
adamben
Member
 
Registered: Feb 2007
Distribution: slackware,gentoo,ubuntu
Posts: 50

Rep: Reputation: 17
Well - you could try just 1 command - awk is quite powerful....

Code:
awk '!/^>/{ lines++; total+= length($1) } END { average=total/lines; printf("\n Average %2d\n", average);} '

Last edited by adamben; 09-12-2009 at 01:10 AM. Reason: Corrected less than to greater than sign
 
Old 09-12-2009, 01:10 AM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by kmkocot View Post
Hi all,

I have several files with many lines something like this:

[ snip ... ]

I'm trying to write a script that will count the number of characters per line that doesn't contain a ">" symbol and give me an average of those values. I have most of the script together but I can't figure out how to connect some of the steps. Can anyone help? Thanks!!!

Code:
grep -v \> Chaetoderma_nitidulum | wc -m | ?? divide outout of wc -m by value returned by this command: grep -c \> file.txt ??
Please say what "average of those values" means. The average of the ASCII character code numbers for the characters? And do you want an average for each line, or for the entire file?

Also, very important -- have you considered writing a formal script to deal with this? Doing it in a single line is going to be very difficult, especially the part where you test and then have to edit the line using limited Bash editing features.
 
Old 09-13-2009, 12:05 PM   #4
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Original Poster
Rep: Reputation: 15
adamben, that did it!

Sorry if I was unclear. I wanted to get the average of the number of characters per line (not including lines containing ">"). I figured I was just missing a simple step in what I was trying to do above so I wasn't going to worry about writing a script file.

Thanks!
Kevin
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 07:07 PM
Script Help: How to count a matching pattern in one line? dv502 Programming 3 12-13-2008 02:53 PM
display only certain chars in each line cranium2004 Programming 2 02-21-2006 03:03 PM
how many chars can be displayed in a line? williamjxj Linux - Software 1 10-25-2004 04:24 AM


All times are GMT -5. The time now is 10:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration