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?
# 8  
Old 05-24-2013
Hi Yoda,
I have one more question. How should I change the code if I want it to count "0/1" and "1/1"?
Thanks
# 9  
Old 05-24-2013
Assuming you want to count "0/1" & "1/1" separately for each record:
Code:
awk '{i=gsub("0/1","&");j=gsub("1/1","&");print $0,"0/1 count: " i, "1/1 count: ", j}' file

# 10  
Old 05-24-2013
No, actually, I want to count both together and report it in the last column.
# 11  
Old 05-24-2013
Quote:
Originally Posted by a_bahreini
No, actually, I want to count both together and report it in the last column.
Code:
awk '{n=gsub(/0\/1|1\/1/,"&");print $0,n}' file

This User Gave Thanks to Yoda For This Post:
# 12  
Old 05-24-2013
Sorry again, it turned out that I also have "./." strings in my text which I need to count. However, when I replace it with previous ones, it only counts all the "/" instead of "./."
Could you please help me fix this?
Thanks
# 13  
Old 05-24-2013
Period . is a metacharacter, hence it has to be escaped to preserve its literal meaning:
Code:
awk '{n=gsub(/0\/1|1\/1|\.\/\./,"&");print $0,n}' file

# 14  
Old 05-24-2013
Sorry, but it doesn't count it. Try on the following section:
Code:
AC=21;AF=0.256;AN=82;BaseQRankSum=-2.712;DB;DP=369;Dels=0.00;FS=1.862;HaplotypeScore=0.4505;InbreedingCoeff=0.2067;MLEAC=20;MLEAF=0.244;MQ=49.76;MQ0=0;MQRankSum=-0.555;QD=8.10;ReadPosRankSum=0.944    GT:AD:DP:GQ:PL  0/0:6,0:6:15:0,15,159   0/1:2,5:7:15:107,0,15   0/1:8,11:20:99:154,0,141        0/0:9,0:9:15:0,15,170   0/0:4,1:5:9:0,9,105     0/1:12,6:18:99:99,0,205 0/1:2,3:5:50:50,0,54    0/1:18,3:20:9:9,0,437   0/0:16,0:16:33:0,33,3700/0:12,0:12:24:0,24,260  0/0:6,0:7:12:0,12,118   0/0:11,0:11:27:0,27,280 0/0:8,0:8:21:0,21,227   1/1:0,4:4:12:127,12,0   0/0:5,0:5:9:0,9,98      0/0:9,0:9:21:0,21,222  1/1:0,1:1:3:34,3,0       0/0:5,0:5:15:0,15,165   ./.     0/1:4,3:9:50:50,0,86    0/1:6,5:11:85:85,0,143  0/0:9,0:9:18:0,18,204   0/0:12,0:12:27:0,27,291 1/1:0,5:5:15:151,15,0   1/1:1,5:7:12:123,12,0   0/0:10,0:10:24:0,24,265 0/0:5,0:5:6:0,6,58      1/1:0,7:7:18:181,18,0   0/0:3,0:3:6:0,6,67      0/0:7,0:7:18:0,18,186   0/0:12,0:12:24:0,24,245 0/1:4,1:6:20:20,0,85    ./.     ./.     0/1:5,2:7:28:28,0,37    0/1:4,4:8:52:79,0,52    0/0:13,0:13:30:0,30,318 0/0:3,0:3:6:0,6,67      0/0:5,0:5:12:0,12,128  0/0:8,0:8:18:0,18,191    0/0:2,0:2:6:0,6,64      0/1:10,8:18:99:125,0,182        0/0:11,0:12:21:0,21,250 0/0:11,0:12:27:0,27,281 ./.

 
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