How to count a string in a line and report it?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to count a string in a line and report it?
# 1  
Old 05-22-2013
How to count a string in a line and report it?

Hi,
I have a text file full of such line (this is only 1 line, tab delimited):
Code:
1       108     .       C       T       553.90  .       AC=17;AF=0.189;AN=90;BaseQRankSum=-3.284;DP=2979;Dels=0.01;FS=0.302;HaplotypeScore=15.4732;InbreedingCoeff=-0.2151;MLEAC=15;MLEAF=0.167;MQ=28.29;MQ0=364;MQRankSum=2.229;QD=0.50;ReadPosRankSum=4.330   GT:AD:DP:GQ:PL  0/0:54,4:57:63:0,63,588 0/0:76,7:81:48:0,48,918 0/0:72,5:75:99:0,103,1312 0/1:66,12:76:99:102,0,1010

Here, I want to count '0/1' in each line and report it in the last column. Is there anyone who can help me with this?
Thanks
# 2  
Old 05-22-2013
You mean literal string "0/1" or 0's and 1's in the input line?
# 3  
Old 05-22-2013
No, I meant literal string "0/1"
# 4  
Old 05-22-2013
Try: see if this helps...

nawk 'BEGIN{FS=OFS="<tab>"}{print}{gsub("[^0\/1]","");print length}' FileA

I am sure you would need to amend this script as I didn't tested it... something else is keeping me real busy.. Smilie
one thing I am sure about is that it will print length on next line... at the moment I really do not know how to take care of that problem....
# 5  
Old 05-22-2013
Thanks for your code but it's not working. It counts all the 0s and 1s but not my specific string "1/0"
# 6  
Old 05-22-2013
To display the count in the end of each record:
Code:
awk '{n=gsub("0/1","&");print $0,n}' file

To display total count:
Code:
awk '{n=gsub("0/1","&");c+=n}END{print c}' file

This User Gave Thanks to Yoda For This Post:
# 7  
Old 05-22-2013
Hi Yoda,
That works perfect.
Thanks a lot
 
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 pipes "|" in line and delete line if count greter then number.

Hello, I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17". i.e need to get records having exact 17 pipe separated fields(no more or less) currently i have below : awk... (1 Reply)
Discussion started by: ketanraut
1 Replies

2. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

3. Shell Programming and Scripting

Count and print the most repeating string in each line

Hi all, I have a file in which each string from column 1 is associated with one or multiple strings from column 2. For an example, in the sample input below, Gene1 from column1 is associated with two different strings from column 2 (BP1 and BP2).For every unique string from column 1, I need to... (9 Replies)
Discussion started by: AshwaniSharma09
9 Replies

4. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

5. Shell Programming and Scripting

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

6. Shell Programming and Scripting

Extract string from multiple file based on line count number

Hi, I search all forum, but I can not find solutions of my problem :( I have multiple files (5000 files), inside there is this data : FILE 1: 1195.921 -898.995 0.750312E-02-0.497526E-02 0.195382E-05 0.609417E-05 -2021.287 1305.479-0.819754E-02 0.107572E-01 0.313018E-05 0.885066E-05 ... (15 Replies)
Discussion started by: guns
15 Replies

7. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

8. Shell Programming and Scripting

awk: sort lines by count of a character or string in a line

I want to sort lines by how many times a string occurs in each line (the most times first). I know how to do this in two passes (add a count field in the first pass then sort on it in the second pass). However, can it be done more optimally with a single AWK command? My AWK has improved... (11 Replies)
Discussion started by: Michael Stora
11 Replies

9. Shell Programming and Scripting

Generating a count report

Hi, I want to generate a report for count mismatching. Steps followed for creating a script for file in 1). I have to fetch the file name from the checksum.out #customer_information_ 2). Added Detail #customer_information_Detail 3). Check the file exist or not. 3.1.1)if the the file... (1 Reply)
Discussion started by: number10
1 Replies

10. Shell Programming and Scripting

Get the line count from 2nd line of the file ?

Hi, I want to get the line count of the file from the 2nd line of the file ? The first line is header so want to skip that. Thanks. (8 Replies)
Discussion started by: smc3
8 Replies
Login or Register to Ask a Question