How to check file name format using shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check file name format using shell script?
# 1  
Old 10-07-2010
How to check file name format using shell script?

Hi,

I am writting a script, which accepts input file as parameter.
Input file name is aa_bb_cc_dd_ee.<ext>
I need to check that input file name should be of 5 fileds.
Please help me out. Smilie
# 2  
Old 10-07-2010
I can think of a quick way to do this via 'cut'.
The first part would be to get rid of the file extension:
Code:
FILE_SHORT_NAME=`echo $FILE_NAME | cut -d'.' -f1`

The next would be to split the remaining name into 5 parts and check if each part is present:

Code:
PART_1=`echo $FILE_SHORT_NAME | cut -d'_' -f1`
PART_2=`echo $FILE_SHORT_NAME | cut -d'_' -f2`
PART_3=`echo $FILE_SHORT_NAME | cut -d'_' -f3`
PART_4=`echo $FILE_SHORT_NAME | cut -d'_' -f4`
PART_5=`echo $FILE_SHORT_NAME | cut -d'_' -f5`

You could then do basic checks to make sure that none of these parts is an empty string:

Code:
if [ -z $PART_1 ]
then
        echo "Please enter a correct filename"
fi

And similarly for the other parts as well. Up to you whether you want to get them in bulk and check them or get them one by one and check them and throw an error at the first place itself!
# 3  
Old 10-07-2010
case could be even more useful.

assuming your pattern is ;

Code:
<any_lowercase_alphabet>_<any_lowercase_alphabet>_<any_lowercase_alphabet>_<any_lowercase_alphabet>_<any_lowercase_alphabet>.ext

try something like:

Code:
case $file in
 
[a-z]*_[a-z]*_[a-z]*_[a-z]*_[a-z].ext ) echo yes ;;
* ) echo no;;
 
esac

# 4  
Old 10-07-2010
Once I get file name excluding extension, I just want to count number of fields insted of each field value check.
for example,
if my file name is "aa_bb_cc_dd_ee.<ext>"
using below,
Code:
FILE_SHORT_NAME=`echo $FILE_NAME | cut -d'.' -f1`

now i receive only file name as "aa_bb_cc_dd_ee"
Now I need to count number of fields seperated by "_"underscore.
Please help me out.

Last edited by Poonamol; 10-07-2010 at 08:07 AM.. Reason: example is added
# 5  
Old 10-07-2010
Code:
n=$(echo $FILE_NAME |awk -F _ '{print NF}' )

if [ "$n" -ne 5 ] 
then
        echo "Please enter a correct filename aa_bb_cc_dd_ee.<ext>"
        exit
fi

# 6  
Old 10-07-2010
I need code in korn shell script and not in awk with bash.
# 7  
Old 10-13-2010
I had mentioned this earlier in my post. To get the individual parts via cut, do the following:

Code:
PART_1=`echo $FILE_SHORT_NAME | cut -d'_' -f1`
PART_2=`echo $FILE_SHORT_NAME | cut -d'_' -f2`
PART_3=`echo $FILE_SHORT_NAME | cut -d'_' -f3`
PART_4=`echo $FILE_SHORT_NAME | cut -d'_' -f4`
PART_5=`echo $FILE_SHORT_NAME | cut -d'_' -f5`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to check a file

I'm required to write a simple shell script that when it runs it writes the output which is a simple barcode to a tmp flat file which I can do the bit I'm struggling with... The next time it runs I need to check the tmp output file to see if that barcode is in the output file and if it is send... (5 Replies)
Discussion started by: worky
5 Replies

2. Shell Programming and Scripting

Shell script change new format on the file.

Hi---Is there's way can write small shell script or perl script open "abc.txt" file and create new "new_abc.txt" file with format output below? Thanks cat abc.txt ###########################Readme############################### Contained with this README.TXT file are all of the file... (7 Replies)
Discussion started by: dotran
7 Replies

3. UNIX for Dummies Questions & Answers

Need shell script to check file

Hi Experts, I am not good in writing script. Just stared.I am looking for shell script to check following parameters. 1) Number of files on remote Linux SUSE server.- Any directory and sub directory. 2) I should define number of files in script. Files should be variable. 3) Age of... (2 Replies)
Discussion started by: ApmPerfMonitor
2 Replies

4. Shell Programming and Scripting

Script to check dos format in a file

Hi All, I am trying to check if the file is in dos format using simple grep command but the problem is lines inside the file with have special characters in between and in some lines end of the line will have the '^M' character. I tried the below command in simple line(without special... (7 Replies)
Discussion started by: Optimus81
7 Replies

5. Shell Programming and Scripting

Format CSV file from a shell script

I have a shell script which creates a CSV files. there are 3 fields, field1, field2 and comp. I will generates the values for field1 and field2 and Want to compare both. If field1>filed2 then comp should be success written in green in CSV file, else it should fail in red color. How can I change the... (5 Replies)
Discussion started by: sauravrout
5 Replies

6. Shell Programming and Scripting

Conversion of below Tabs Tex file into CSV format file : shell script needed

Request if some one could provide me shell script that converts the below "input file" to "CSV format file" given Name Domain Contact Phone Email Location ----------------------- ------------------------------------------------ ------- ----- ---------------------------------... (7 Replies)
Discussion started by: sreenath1037
7 Replies

7. Shell Programming and Scripting

How to write shell script for input file name format checking?

Hello, I had written a shell script that accepts input file as cmd line argument and process this file. if ; then if ; then . $1 LOGFILE="$LOG_FILE/MIG_BIOS.log"; get_input_file else ERROR_CODE=MSCRM0005_003 error "$ERROR_CODE : Input file $1 is not available"; exit... (3 Replies)
Discussion started by: Poonamol
3 Replies

8. Solaris

format file using shell script

my question "format file using shell script " is not a homework. bad guess. my actual file is much more complex. the requirement is to format the file before i can read it from SAP. so i'd appreciate if any inputs can be provide. i've tried most of the commands like tr and sed and nawk, no... (2 Replies)
Discussion started by: balajim
2 Replies

9. Solaris

format file using shell script

Hi All, I am new to shell scripts. I have a requirement to change the format of a file. Here is the original file: #student layout student_name student_class student_subject david 5 chemistry paul 4 physics steve 6 mathematics This is the format i need: k1,david,5,chemistry... (1 Reply)
Discussion started by: balajim
1 Replies

10. UNIX for Advanced & Expert Users

format csv file using shell script

i have a report.csv file from oracle datavase In that file data is like this with report heading and date SALES DAILY REPORT DATE: 06-26-2007 REPORT NAME: SALES DATA AA.BB.CCCC.DDDD,BBBBB,06-26-2007,0,BEGIN,END ZZ.VV.DDDD.XXXXXXX,MMMMMM,06-25-2007,18,BEGIN,END... (3 Replies)
Discussion started by: raosurya
3 Replies
Login or Register to Ask a Question