Counting characters within a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting characters within a file
# 1  
Old 10-31-2010
Counting characters within a file

Ok say I wanted to count every Y in a data file.
Then set Y as my delimiter so that I can separate my file by taking all the contents that occur BEFORE the first Y and store them in a variable so that I may use this content later on in my program. Then I could do the same thing with the next Y's that are within the contents of my file.
q=`grep -c "Y" file`

What I am really trying to do is break apart the contents of a string and set my delimiter as the character that separates my string with other parameters within the same string ( * is my delimiter ) Is there a way to find out at which position a specific character is located at in a specific string?
For Example,

Lets say I do a character count of a, which is 11... now what I was thinking about doing is I could count the characters before my * and then set that equal to a variable of some sorts... hmm

a="hello*there"
I want to find out where the * is.

(This is me just playing with unix shell, I hope someone understands my gibberish! )
# 2  
Old 10-31-2010
Code:
$ A="hello*there"
$ B=${A#*\*}
$ echo $((${#A}-${#B}))
6

# 3  
Old 10-31-2010
Not quite sure what you are looking for, but here are two awk programmes that list the offset of the first splat (*) in each record, and one that counts the number of splats in the file:

Code:
# present the offset of first splat
# you can use index() on any variable; $0 is the whole input line
awk '
   {
        printf( "(%s) * is character %d\n", $0, index( $0, "*" ) );
   } ' <input-file

#count number of splats in the file
# uses splat as the field separator (-F) and assumes that number of fields
# less one per line is the number of splats per line. 
awk -F "*" '
        { count += (NF - 1 ); next }
        END { print count}
' <input-file

The grep -c command will count lines, not the number of characters. The way you worded your post you want to count all characters not just the number of lines containing that character.

Hope this gives you something to work with.
# 4  
Old 11-01-2010
short way:
Code:
tr -dc 'a' < inputfile | wc -c

tr -dc 'a' deletes every character that is not an 'a'.
# 5  
Old 11-01-2010
Quote:
What I am really trying to do is break apart the contents of a string and set my delimiter as the character that separates my string with other parameters
An example of your input and required output would help reduce the amount of guessing we are doing here.

This is one way to split a string into two pieces ("my string" and "other parameters") with '*' as the delimiter character:

Code:
STR1="$(echo $STRING | cut -f1 -d'*')"
STR2="$(echo $STRING | cut -f2 -d'*')"

# 6  
Old 11-02-2010
Is this what you need?:

Random string:
Code:
TEST="sdfsdfsdf*asdfsafsfsad*asdfsafsdf*asdf"

Separator: *
Code:
IFS='\*'

Code:
Code:
echo $TEST | while read LINE; do print "$LINE"; done | tr " " "\n"

Result:
Code:
sdfsdfsdf
asdfsafsfsad
asdfsafsdf
asdf



Or, a file:
Code:
cat /tmp/b
dfsdfsdfsdaf*asdfsadf
asdfsadfsadf
asdfsad*
*
asdfsdf*asdf



Code:
Code:
cat /tmp/b | tr '\*' '\n' | while read LINE; do print "$LINE"; done



Result:
Code:
dfsdfsdfsdaf
asdfsadf
asdfsadfsadf
asdfsad



asdfsdf
asdf



You can easily save $LINE inside the loop.
Tested on AIX and HP-UX; ksh93 and ksh88.



Last edited by radoulov; 11-02-2010 at 04:33 PM.. Reason: Please use code tags, thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting characters at each position

Hi All, here's a question from newbie I have a data like this, which set of small DNA sequences separated by new line GAATCCGGAAACAGCAACTTCAAANCA GTNATTCGGGCCAAACTGTCGAA TTNGGCAACTGTTAGAGCTCATGCGACA CCTGCTAAACGAGTTCGAGTTGAANGA TTNCGGAAGTGGTCGCTGGCACGG ACNTGCATGTACGGAGTGACGAAACCI... (6 Replies)
Discussion started by: amits22
6 Replies

2. Shell Programming and Scripting

Counting characters vertically

I do have a big file in the following format >A1 ATGCGG >A2 TCATGC >A3 -TGCTG The number of characters will be same under each subheader and only possible characters are A,T,G,C and - I want to count the number of A's, T's,G's, C's & -'s vertically for all the positions so that I... (5 Replies)
Discussion started by: Lucky Ali
5 Replies

3. Shell Programming and Scripting

Counting 2 characters with into 2 categories from a text file

I have a tab delimited file of the following format 2 L a 2 G b 2 L c 2 G a 3 G a 3 G b 3 L c 4 L a 4 G a 4 G b 4 L c 4 G a .. ... I want to count the number of G's and L's with in the first column and the third column/categories such that I would get an output file: (6 Replies)
Discussion started by: Lucky Ali
6 Replies

4. Shell Programming and Scripting

Counting the number of characters

Hi all, Can someone help me in getting the following o/p I/p:... (7 Replies)
Discussion started by: Sri3001
7 Replies

5. Shell Programming and Scripting

taking characters and counting them

Nevermind, I figured out a way using the sed command. But I forget the basic way of counting characters within a variable :( (4 Replies)
Discussion started by: puttster
4 Replies

6. Shell Programming and Scripting

counting characters

Hi All, I need some help in counting the number of letters in a big file with separations. Following is the file I have >AB_1 MLKKPIIIGVTGGSGGGKTSVSRAILDSFPNARIAMIQHDSYYKDQSHMSFEERVKTNYDHPLAFDTDFM IQQLKELLAGRPVDIPIYDYKKHTRSNTTFRQDPQDVIIVEGILVLEDERLRDLMDIKLFVDTDDDIRII... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

7. Shell Programming and Scripting

Deleting the blank line in a file and counting the characters....

Hi, I am trying to do two things in my script. I will really appreciate any help in this regards. Is there a way to delete a last line from a pipe delimited flat file if the last line is blank. If the line is not blank then do nothing..... Is there a way to count a word that are starting... (4 Replies)
Discussion started by: rkumar28
4 Replies

8. Shell Programming and Scripting

Counting characters between comma's

I have a comma delimited file that roughly has 300 fields. Not all fields are populated. This file is fed into another system, what I need to do is count the amount of characters in each field and give me an output similiar to this: 1 - 6,2 - 25 The first number is the field and the second... (2 Replies)
Discussion started by: dbrundrett
2 Replies

9. Shell Programming and Scripting

counting characters

Dears, I would like to count the number of "(" and ")" that occur in a file. (syntax checking script). I tried to use "grep -c" and this works fine as long as there is only one character (for which I do a search) on a line. Has anyone an idea how I can count the number of specific characters... (6 Replies)
Discussion started by: plelie2
6 Replies
Login or Register to Ask a Question