Count character in one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count character in one line
# 1  
Old 12-28-2011
Count character in one line

Please check the attachment for the example. 1111.txt
Purpose: count how many "|" character in one line and also display the line number.

expect result:
Code:
Line 1 : there are 473 "|" characters
Line 2 : there are 473 "|" characters

I have tried to use awk to count it, it's ok when the statistic character is less than 199.
Code:
awk -F'|' '{print NR,FS,NF-1}' 1111.txt
awk: Line ||||00000000000033||4 cannot have more than 199 fields.
 The input line number is 1. The file is 1111.txt.
 The source line number is 1.

Thanks for your advice.

Last edited by Franklin52; 12-29-2011 at 10:38 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 12-28-2011
Try this...
Code:
awk '{print "Count : "gsub(/\|/,"1")}' infile

--ahamed
# 3  
Old 12-28-2011
if his awk can't even have 200 separators in a line, it sure won't have gsub.

Please explain what your system is so we know what tools we can advise you to use.

Try /usr/xpg4/bin/awk instead of plain awk if you're on solaris.
# 4  
Old 12-29-2011
The Perl way.
Code:
$ perl -ne 'print "Line $. has ".tr/|//." pipes.\n"' inputfile
Line 1 has 473 pipes.
Line 2 has 473 pipes.

# 5  
Old 12-29-2011
Code:
 
nawk -F\| '{printf("Line %s : there are \"%s\" Characters\n",NR,NF)}' inputfile.txt


If you want to find out any the lines which doesnt have 473 fields, then use the below

Code:
 
nawk -F\| 'NF!=473{print NR}' inputfile.txt

# 6  
Old 12-29-2011
Quote:
Originally Posted by itkamaraj
Code:
 
nawk -F\| '{printf("Line %s : there are \"%s\" Characters\n",NR,NF)}' inputfile.txt

NF-1 will give the actual data.

--ahamed
# 7  
Old 12-29-2011
Thanks for your comments. the environment as follows:

HPUX 11
Bourne Shell, no bash
AWK, no NAWK

---------- Post updated at 01:14 AM ---------- Previous update was at 01:01 AM ----------

Quote:
Originally Posted by balajesuri
The Perl way.
Code:
$ perl -ne 'print "Line $. has ".tr/|//." pipes.\n"' inputfile
Line 1 has 473 pipes.
Line 2 has 473 pipes.

It's cool. Thanks! I am not good at PERL :-(
If I want to summarize the strings "abc" in one line(also more than 200 times). The script of PERL should be ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. UNIX for Dummies Questions & Answers

Getting the character count of the last line

I need the character count of the last line of each file in a directory, and not the total. Now I have been doing this but unfortunately, -exec doesn't support pipes: find sent/ -type f -exec tail -1|wc -c {} \; If I try this: find sent/ -type f -exec tail -1 {} \; | wc -c It will give... (6 Replies)
Discussion started by: MIA651
6 Replies

3. Shell Programming and Scripting

Character count of each line

Hi, I have a file with more than 1000 lines. Most of the lines have 16 characters. I want to find out lines that have less than 14 characters (usually 12 or 13). wc -l gives me the line count and wc -c gives me the total characters in a file. I could not get the total characters for each line.... (1 Reply)
Discussion started by: bobbygsk
1 Replies

4. UNIX for Dummies Questions & Answers

Script to count character present in a line

Suppose i have multiple line like below in a file. ASDFAFAGAHAHAHA AGAHAHAJGAFAGAH AHAHAKAHAHAHAKA I need a bash script to count a character and also Also count the number of character present in each line . suppose for line 1: A=x, S=y, D=x and so on and total character=15. where x y and z is... (3 Replies)
Discussion started by: XXLMMN
3 Replies

5. Shell Programming and Scripting

Delete line based on count of specific character

I'm looking for what I hope might be a one liner along these lines: sed '/a line with more than 3 pipes in it/d' I know how to get the pipe count in a string and store it in a variable, but I'm greedy enough to hope that it's possible via regex in the /.../d context. Am I asking too much? ... (5 Replies)
Discussion started by: tiggyboo
5 Replies

6. UNIX for Advanced & Expert Users

Count specific word or character per line

Hi, I need help regarding counting specific word or character per line and validate it against a specific number i.e 10. And if number of character equals the specific number then that line will be part of the output. Specific number = 6 Specific word or char = || Sample data:... (1 Reply)
Discussion started by: janzper
1 Replies

7. 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

8. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies

9. UNIX for Dummies Questions & Answers

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution. (5 Replies)
Discussion started by: sumit207
5 Replies

10. UNIX for Advanced & Expert Users

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution.:) (1 Reply)
Discussion started by: sumit207
1 Replies
Login or Register to Ask a Question