How to count number of delimiters in a file name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to count number of delimiters in a file name
# 1  
Old 06-27-2008
Computer How to count number of delimiters in a file name

I have a list of files with names as "FULL_abcd_xyz_timestamp.txt" and "FULL_xx_abcd_xyz_timestamp.txt". I am writing a script with a 'for loop' to take each file, strip the "FULL" and "timestamp" from the file name and do some actions on the contains of the file. So I need to know the number of "_" in each file name obtained as $file (for file in `ls *.txt`) so that I can cut 2-3 or 2-4 fields accordingly using- "echo $file | cut -f 2-3 -d "_""
# 2  
Old 06-27-2008
Code:
n_fields=`echo $var|tr _ "\n"|wc -l`
n=$[ $n_fields - 1 ]

# 3  
Old 06-27-2008
This works in bash or ksh
Code:
$ str="FULL_abcd_xyz_timestamp.txt"
$ str=${str//[^_]}
$ n=${#str)
$ print $n
3

# 4  
Old 06-27-2008
Or:

Code:
number=$(echo $var|awk -F_ '{print NF-1}')

Regards
# 5  
Old 07-02-2008
Thanks a million folks.
 
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 Number Of Delimiters using awk or better

What to know the way to count the number of delimiters in each record by ignoring the escape delimiters. Sample Data: 12345678|ABN\|XYZ MED CHEM PTY. LTD.|C||100.00|22|AB"C\|Corp|"XYZ|CDEF"| I'm using awk -F'|' '{ print NF-1 }' command to find the number of delimiters. this command... (8 Replies)
Discussion started by: BrahmaNaiduA
8 Replies

2. Shell Programming and Scripting

Skip the delimiter with in double quotes and count the number of delimiters during data extract

Hi All, I'm stuck-up in finding a way to skip the delimiter which come within double quotes using awk or any other better option. can someone please help me out. Below are the details: Delimited: | Sample data: 742433154|"SYN|THESIS MED CHEM PTY.... (2 Replies)
Discussion started by: BrahmaNaiduA
2 Replies

3. Shell Programming and Scripting

Insert Columns before the last Column based on the Count of Delimiters

Hi, I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number. Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field awk -F 'Ç' '{ if (NF-1 < 139)} END { "Insert 2... (5 Replies)
Discussion started by: arunkesi
5 Replies

4. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

5. Shell Programming and Scripting

Count the number of lines in a file with one condition

Hi Everyone, 1.txt Mon 08 Feb 2010 12:30:44 AM MYT;1265560244;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:46 AM MYT;1265560246;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:48 AM MYT;1265560248;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:50 AM... (1 Reply)
Discussion started by: jimmy_y
1 Replies

6. UNIX for Dummies Questions & Answers

Count of Number of Lines in a File

Dear Members, I want to count the number of lines in a file; for that i am using the following command : FILE_LINE_COUNT=`wc -l $INT_IN/$RAW_FILE_NAME` if i do an echo on FILE_LINE_COUNT then i get 241 /home/data/testfile.txt I don't want the directory path to be displayed. Variable... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

7. Shell Programming and Scripting

Count the number of files with only the partial file name

I have 1800 files in a directory. The file name is like out_cpty_XXXX. The "XXXX" vaires from file to file. I want to get the count of files with file name out_cpty_XXXX. How to get the count with just the partial file name? Any help would be appreciated? (4 Replies)
Discussion started by: Sangtha
4 Replies

8. Shell Programming and Scripting

File number count

hi all i want to count the number of files in a particlar dir i have wrote the codes as below fileCount= ls | wc -l if then echo " " elif then echo " " fi however when i excute the program there are error happens " unary operator expected" i just woundering fileCount=... (4 Replies)
Discussion started by: cryogen
4 Replies

9. Programming

to count the number of commented lines in a file

can someone guide me how to have a C pgm to count the number of commented lines? (3 Replies)
Discussion started by: naan
3 Replies

10. Shell Programming and Scripting

hw can i count the number of character in a file by perl

i want to count the number of character contained in afile using perl cript help me out (1 Reply)
Discussion started by: trupti_rinku
1 Replies
Login or Register to Ask a Question