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?
# 15  
Old 05-24-2013
Try this modified code:
Code:
awk '{n=gsub("0/1","&");n+=gsub("1/1","&");n+=gsub(/\.\/\./,"&");print $0,n}' file

# 16  
Old 05-24-2013
It didn't work. it counts all "/" and "0/1" and "1/1"
# 17  
Old 05-24-2013
Quote:
Originally Posted by a_bahreini
It didn't work. it counts all "/" and "0/1" and "1/1"
I edited the code that I posted. Can you retry?
# 18  
Old 05-24-2013
Now it worked.
Thanks a lot
# 19  
Old 05-24-2013
Yet another awk 1 liner...
Code:
awk -F"[.][/][.]|[01][/][1]" '{print $0, NF-1}' file

# 20  
Old 05-25-2013
@OP: I don't know why there should be a difference #13 and #15 and why one should work and the other one would not. What is your OS ?

Also do you get a difference between:
Code:
awk '{print $0, gsub("[01]/1|\\./\\.",x)}' file

and
Code:
awk '{print $0, gsub(/[01]\/1|\.\/\./,x)}' file

 
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