To find number of char occur


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To find number of char occur
# 1  
Old 03-30-2010
To find number of char occur

To find out number of "|" symbol is available in file:

Input:

Code:
a|b|c|d|z

Ouput:
Code:
4

I am using below set of commands,It is working... Anybody have anyother solution using sed / awk.
Code:
    cnt=`wc -c <1.txt`
    cnt1=`tr -d "|" <1.txt >c.dat`
    cnt2=`wc -c <c.dat`
    outp=`expr $cnt - $cnt2`

Thanks in advance.

Last edited by radoulov; 03-30-2010 at 07:10 AM.. Reason: Please use code tags!
# 2  
Old 03-30-2010
Code:
% print 'a|b|c|d|z' | awk -F\| '$0=NF-1'
4

For 0 occurrences you won't get any output.
# 3  
Old 03-30-2010
It is really great. Can you please explain the working follow of above awk command.

Thanks a lot for your help.
# 4  
Old 03-30-2010
It sets the pipe as a input field separator and then sets the internal variable $0 (the current input record) to the number of fields - 1.
# 5  
Old 03-30-2010
another way in Perl.
Code:
perl -pe '$_ = s#\|#\|#g'  < input_file 
4

# 6  
Old 03-30-2010
Or:

Code:
% perl -le'print shift=~tr/|//' 'a|b|c|d|z'
4

# 7  
Old 03-30-2010
or,

Code:
echo 'a|b|c|d|z' | tr -dc '|' | wc -c

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. UNIX for Dummies Questions & Answers

Remove char if not a number

I need to write a BASH script that takes a 2 character string and removes the second character if it is not a digit e.g. If the string is numberical value >9 e.g. string1 = '34' then leave string1 = '34'. However if the string is <10 e.g. string1 = '3X' then remove the second char (which... (7 Replies)
Discussion started by: millsy5
7 Replies

3. Shell Programming and Scripting

To find char field and replace null

hi, i having a file with | seperated in which i need to search char in 3rd column and replace with null. i need to replace only the coulmn where character occurs in 3rd field for eg: file1.txt xx|yy|xx|12 output file: xx|yy||12 (5 Replies)
Discussion started by: rohit_shinez
5 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

awk to find lines containing word that occur multiple times

i have a script that scans a log file every 10 minutes. this script remembers the last line of the log and then uses it to continue monitoring the log when it runs again 10 minutes later. the script searches the log for a string called MaxClients. now, how can i make it so that when the... (7 Replies)
Discussion started by: SkySmart
7 Replies

6. Shell Programming and Scripting

find only 6 char long

find /tmp -type f -mtime +180 I have this script get the list to clean up files older than 180 days under /tmp. But, I want to make sure to grep only a type of files, which have only 6 character long. .... LT3hqa dRMoya ... (16 Replies)
Discussion started by: Daniel Gate
16 Replies

7. Shell Programming and Scripting

Find and replace all extended char.

Hi Guys, I wand find and replace all Extended ASCII Codes from all my log files. My Log files: /home/Kalr/PPool/Output i have logs file in sub dir. /home/Kalr/PPool/Output/X /home/Kalr/PPool/Output/Y /home/Kalr/PPool/Output/Z My Abc.log file input: Extended ASCII Codes :– ... (4 Replies)
Discussion started by: asavaliya
4 Replies

8. Shell Programming and Scripting

how to get number char from a string

for example: i hav a string like : /rmsprd/arch01/rmsprd/rmsprdarch72736.log how I can extract my_num=72736? I know I can echo "/rmsprd/arch01/rmsprd/rmsprdarch72736.log" | tr "/" " " | awk '{ print $4 }' to get rmsprdarch72736.log (4 Replies)
Discussion started by: netbanker
4 Replies

9. Shell Programming and Scripting

Number of specific char in a string.

I wish to compute the number of dot chars in a string. Example: VAR="aaaa.bbbbb.cccc" I try the shortest command to solve this test. Thanks in advance for your help. Regards, Giovanni (7 Replies)
Discussion started by: gio123bg
7 Replies
Login or Register to Ask a Question