To count underscore in filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To count underscore in filename
# 1  
Old 04-23-2015
To count underscore in filename

Hi All

I have a filename like below
Code:
adsf_ghjt_kop_pol.csv

Can you please get me a command for counting the number of underscore in the filename.

In the above filename I should get output as 3, since the file name is having three underscore
# 2  
Old 04-23-2015
Hello ginrkf,

Could you please try following and let me know if this helps you.
Code:
echo "adsf_ghjt_kop_pol.csv" | awk '{A=gsub(/_/,X,$0)}END {print A}'
OR
echo FILENAME |  awk '{A=gsub(/_/,X,$0)}END {print A}'

Thanks,
R. Singh
# 3  
Old 04-23-2015
Thanks a lot Singh.Its working fine
# 4  
Old 04-23-2015
Another one Smilie
Code:
$ echo adsf_ghjt_kop_pol.csv | awk -F_ '{print NF-1}'
3


Last edited by zaxxon; 04-23-2015 at 11:46 AM.. Reason: removed useless END
# 5  
Old 04-23-2015
One more

Code:
$ echo 'adsf_ghjt_kop_pol.csv' | awk '{print split($0,A,/_/)-1}'
3

# 6  
Old 04-23-2015
bash / ksh93 / zsh, all in shell without subshells:
Code:
fname=adsf_ghjt_kop_pol.csv
uscores=${fname//[!_]}
echo ${#uscores}


Last edited by Scrutinizer; 04-23-2015 at 12:45 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Count and FileName

Hi I have a set of files in one directory like below FIN_TASK1.csv FINA_TASK2.csv FIN_TASK3.csv. I want a command to print the count in each file and the correspong file name into one file.My expected output is given below cat filcount.txt 2 "FIN_TASK1.csv" 4 "FIN_TASK2.csv" ... (3 Replies)
Discussion started by: ginrkf
3 Replies

2. HP-UX

sed help with underscore problems

Hello, I have spent a couple of hours trying to answer this myself, so forgive me if the answer is simple but I have tried. I have a text file generated from svn log output which contains a list of files. Two regexps im using are * and * They both work but some lines has a mixture... (7 Replies)
Discussion started by: YogaBija
7 Replies

3. Shell Programming and Scripting

Finding a certain character in a filename and count the characters up to the certain character

Hello, I do have folders containing having funny strings in their names and one space. First, I do remove the funny strings and replace the space by an underscore. find . -name '* *' | while read file; do target=`echo "$file" | sed 's/... (2 Replies)
Discussion started by: tempestas
2 Replies

4. Shell Programming and Scripting

underscore to dots

Hi, I have been trying to change underscores to dots. For example: 1122_91 1022_233 . 2237_23 9382_2339 2998_234 345_257 . . Desired output: 1122.91 1022.233 . 2237.23 9382.2339 2998.234 345.257 . . Any idea? Thanks (4 Replies)
Discussion started by: iconig
4 Replies

5. Shell Programming and Scripting

Changing the character after the Underscore(_)

Hi Everyone, I am looking for a command that would do the following: 1) Change all the letters/words in a file to Lower case Letters/words. 2) Remove the Underscore (_) and Change the Character after the underscore (_) to an Uppercase letter. Example: File contains the below words: ... (5 Replies)
Discussion started by: filter
5 Replies

6. UNIX for Advanced & Expert Users

google search changes with an underscore

How does adding an underscore change a google search? I was searching for John Elway and got different results with and without an unscore. (3 Replies)
Discussion started by: cokedude
3 Replies

7. Shell Programming and Scripting

Filename pattern count

Hi I have the following files in a directory - ds_pl_W_Test ds_pl_list_Test ds_pl_list_Test ds_pl_listl_Test ds_pl_file_Test ds_pl_xx_Test I would like to count the number files in the directory that contain the word "list" I tried the following but it returns zero - ... (3 Replies)
Discussion started by: Prega
3 Replies

8. Shell Programming and Scripting

allow only alphnumeric entry and an underscore

I am taking an user input which should only be an alphanumeric character or an underscore. How to i do it? (2 Replies)
Discussion started by: vickylife
2 Replies

9. Shell Programming and Scripting

usage of underscore in awk

Hi what is the purpose of using underscore in awk. I suppose it is for defining macro's and reducing repeatation but can some one show me an example? (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies

10. UNIX for Dummies Questions & Answers

Count caraters on filename

Hello people. I need some help to do this: I've got some files that i need to count the caracters of filenames. 20080209_2000591574_237004076372.xml 20080202_1360584_267002636042.xml The minimum is 15 . If some file with less must move them to another directory. Obviously these... (3 Replies)
Discussion started by: osramos
3 Replies
Login or Register to Ask a Question