how to count pariticular char in a location in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to count pariticular char in a location in a file
# 1  
Old 07-18-2007
how to count pariticular char in a location in a file

Hi ..

I am having file say at 53rd position it will be as 0's or 1's .. How can i count the total number of 1's and 0's in the files at the 53 rd location.

Thanks,
Arun
# 2  
Old 07-18-2007
To get count of 0's
Code:
sed -e "s/^.\{52\}\(.\).*/\1/g" input.txt | grep -c 0

To get count of 1's
Code:
sed -e "s/^.\{52\}\(.\).*/\1/g" input.txt | grep -c 1

# 3  
Old 07-18-2007
Grep only.
Count of 1's
Code:
grep -c -e "^.\{52\}1" input.txt

Count of 0's
Code:
grep -c -e "^.\{52\}0" input.txt

# 4  
Old 07-18-2007
HI VINO ..
THANKS FOR YOU REPLY ...

FOR THE SECOND GREP STATEMENT EVEN I HAVE THE 0'S IN 53 RD POSITION I AM GETTING 0...

ALSO IN THE SED IT IS GIVING ME THE WRING COUNT ..Please let me know where i am wrong ..
# 5  
Old 07-18-2007
Quote:
Originally Posted by arunkumar_mca
HI VINO ..
THANKS FOR YOU REPLY ...

FOR THE SECOND GREP STATEMENT EVEN I HAVE THE 0'S IN 53 RD POSITION I AM GETTING 0...

ALSO IN THE SED IT IS GIVING ME THE WRING COUNT ..Please let me know where i am wrong ..
Would certainly appreciate if you dropped the CAPS. Smilie

Here is my sample input file and the output for position 7

Code:
[/tmp]$ cat t
1010101
10010101
0101010
10101001
[/tmp]$ grep -c -e "^.\{6\}0" t
3
[/tmp]$ sed -e "s/^.\{6\}\(.\).*/\1/g" t | grep -c 0
3
[/tmp]$ grep -c -e "^.\{6\}1" t
1
[/tmp]$ sed -e "s/^.\{6\}\(.\).*/\1/g" t | grep -c 1
1
[/tmp]$

Perhaps if you showed us your sample input, it could help.
# 6  
Old 07-18-2007
Vino ..

Appologize from my side ... And it is mistake from my side, sorry to take your time on this explanation and thank you a ton for the command ..

Thanks,
Arun ..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the number of files copied from source to destination location

Hi Guys, how to count number of files successfully copied while coping files from source to destination path ex:10 files from source to target location copying if 8 files copied successfully then echo successfully copied=8 failure=2 files if two files get error to coping files from... (23 Replies)
Discussion started by: sravanreddy
23 Replies

2. UNIX for Dummies Questions & Answers

How to count number files successfully copied from source to target location?

Hi Guys, how to count number of files successfully copied while coping files from source to destination path ex:10 files from source to target location copying if 8 files copied successfully then echo successfully copied=8 failure=2 files if two files get error to coping files... (2 Replies)
Discussion started by: sravanreddy
2 Replies

3. Post Here to Contact Site Administrators and Moderators

How to count successfully copy files source to target location with check directory in Linux?

Hi guys...please any one help me .... how to copy files from source to target location if 5 files copied successfully out of 10 files then implement success=10 and if remaining 5 files not copied successfully then count error=5 how to implement this condition with in loop i need code linux... (0 Replies)
Discussion started by: sravanreddy
0 Replies

4. Shell Programming and Scripting

How count number of char?

hello how can i cont number of char with loop coomand? i dont want to use wc or other special command the script should check all word's char. one by one also a counter can handle the number As noted in other threads started today. This is not the correct forum for homework assignments. ... (2 Replies)
Discussion started by: nimafire
2 Replies

5. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

6. Shell Programming and Scripting

Count char, sum and change

Hello, I have some problem in counting char of word, sum and change. I'm not sure shell script can do this. Input data: Sam1 BB BB AA AA BB BB BB Sam2 BB BB AA AA AB AB AB Sam3 BB BB BB AA BB BB BB Sam4 AB AB AB AB AB AB AA Sam5 BB BB AA AA BB BB -- If I count in column 2, B is 9... (3 Replies)
Discussion started by: awil
3 Replies

7. Shell Programming and Scripting

File created in a different location instead of desired location on using crontab

Hi, I am logging to a linux server through a user "user1" in /home directory. There is a script in a directory in 'root' for which all permissions are available including the directory. This script when executed creates a file in the directory. When the script is added to crontab, on... (1 Reply)
Discussion started by: archana.n
1 Replies

8. UNIX for Dummies Questions & Answers

Grep char count & pipe to sed command

Hi I am having a 'grep' headache Here is the contents of my file: (PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1))) I would like to count out how many times 'PBZ' occurs and then place that number in the line above 3... (8 Replies)
Discussion started by: cavanac2
8 Replies

9. Shell Programming and Scripting

Put one string from one location to another location in a file

Hi Everyone, I have 1.txt here a b c' funny"yes"; d e The finally output is: here a b c d e' funny"yes"; (1 Reply)
Discussion started by: jimmy_y
1 Replies

10. Shell Programming and Scripting

How do I count # of char. in a word?

I havent done shell scripting in quite some time. I want to know how to count the number of characters in a word, specifically a parameter. Example: myscript hello I want "myscript" to return the number of charcaters in the parameter "hello". Any ideas? (9 Replies)
Discussion started by: xadamz23
9 Replies
Login or Register to Ask a Question