How to check user entered correct file format or not?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check user entered correct file format or not?
# 1  
Old 04-02-2017
How to check user entered correct file format or not?

Hi Experts,

Code:
path=/db/files/
format=$1

User can enter any file format.compare the user file format with actual file format existed in the directory /db/files. User enter all characters as "A" apart from date format.

Code:
example1: user will be entering the file format AAA_AA_YYYYMMDD.AAA
Actual file in the directory /db/files/   as GEN_CA_20170224.txt
so file formats are matching then it should display message "file format matching"

example2: user will be entering the file format AAA_AAA_A_YYYYMM.AAA
Actual file in the directory /db/files/   as GEN_CAD_C_201702.txt
so file formats are matching then it should display message "file format matching"

example3: user will be entering the file format AA_YYYYMMDD-HHMMSS.AAA
Actual file in the directory /db/files/   as GE_20170222-153412.txt
so file formats are matching then it should display message "file format matching"

example4: user will be entering the file format AAA_A_YYYYMMDD.AAA
Actual file in the directory /db/files/   as GEN_20170222.txt
so file formats are matching then it should display message "file format NOT matching"

example5: user will be entering the file format AAA_YYYYMM.AAA
Actual file in the directory /db/files/   as GEN_20170222.txt
so file formats are matching then it should display message "file format NOT matching"

Please help me.

Thanks in advance.
# 2  
Old 04-02-2017
Hi Nalu,

What is your OS and version and what shell will you be using?

--
If you have bash/ksh93/zsh, you can try something like this:

Code:
#!/bin/bash
format=$1
path=/db/files

matching() {
  [ -e "$1" ]
}

format=${format//A/[[:alpha:]]}
format=${format//[YMDHS]/[[:digit:]]}

if matching "$path"/$format; then
  echo "file format matching"
else
  echo "file format NOT matching"
fi


Last edited by Scrutinizer; 04-02-2017 at 05:07 PM..
# 3  
Old 04-02-2017
Hi
Thanks a lot for your script.
If the file name is only file name GE_20170222-153412.txt it's working fine.
The script is not working the file name as /db/files/GE_20170222-153412.txt
But in the path /db/files/ my files are as below.Since it's in HDFS file system.
Code:
-rw-r--r--   3 ffuser ffgrp    0 2017-03-29 02:51 /db/files/GE_20170222-153412.txt

Please help me.

Thanks
# 4  
Old 04-02-2017
I get:
Code:
$ ./testpat AA_YYYYMMDD-HHMMSS.AAA
file format NOT matching
$ touch /db/files/GE_20170222-153412.txt
$ ./testpat AA_YYYYMMDD-HHMMSS.AAA
file format matching

What do you get?
# 5  
Old 04-02-2017
I got file format not matching.
# 6  
Old 04-02-2017
Can you show what you did exactly?
# 7  
Old 04-05-2017
Hi,

Thanks a lot for your support.

Regards,
Nalu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Noob question: How to check the total number of inputs entered by user?

Say I have this line: read -p "Enter 3 numbers: " num1 num2 num3; I want to write a while loop that repeatedly asks for input if the number of inputs entered is not equal to 3. I don't know the correct command to find the number of inputs entered. Help, please? (4 Replies)
Discussion started by: jejemonx
4 Replies

2. Shell Programming and Scripting

Extract the correct format of file Name

All, Objective : Extract the correct format of a file name. Please share the script command which will extract the correct format of the file after untarring. Example : If the file is of .csv format then extract filename.csv if the file is having .CSV then extract the same.CSV if the file... (1 Reply)
Discussion started by: Mahesh G
1 Replies

3. UNIX for Dummies Questions & Answers

After Ftp'ing file to destination how to check the file if it is in correct ASCII and not corrupted

Hi Folks, While transferring file from FTP software like Filezilla the files gets corrupted. Is there any way I can check if the recently transferred file is in ASCII and not corrupted. I have tried using file -i filename command which does tell if the file character set is ASCII or binary... (6 Replies)
Discussion started by: Khan28
6 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Downloaded file is not correct format

Hi, Recently we migrated an application in HP UX to Linux. The files(scripts & logs)download from HP UX coming in a formatted way in MS notepad. However the files in Linux are not in correct format in MS notepad(No spaces , some spl symbols like square).We tried both ASCII and binary but no... (2 Replies)
Discussion started by: nag_sathi
2 Replies

5. Shell Programming and Scripting

How to check if date format is correct?

Hi! how do i know if the input is the same as the required date format? the date should be dd/mm/YYYY ex. 2/3/2012 or 15/11/2012 all the following conditions must return an error: *input of string *day is > 31 or < 1 *month is > 12 or < 1 *year is < 2013 suppose the date format is stored... (1 Reply)
Discussion started by: angilulu
1 Replies

6. Shell Programming and Scripting

perl script to check the mail ids in the correct format or not

Hi Folks, I have few mailids in a text file and need to check whether the mailid is in correct format or not. If just to check whether the string is a mailid or not there is a perl module Email::Valid to do the business or we can implement our own logic. But the mail_ids I am having is... (4 Replies)
Discussion started by: giridhar276
4 Replies

7. Shell Programming and Scripting

validating user entered date

I need the date validation. I searched in the google but i didn't find my requirements. requirements: 1) user has to enter the date in YYYY/MM/DD format 2) MM validations 3) DD validations. and if the month is april it should allow 30 days only and for May month it should allow 31 days like... (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

8. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

9. UNIX for Advanced & Expert Users

Correct format in a log file

I'm trying to read the output of a .sql script (simple insert and commit oracle pl/slq script) to a log file using a shell script. My problem is I end up with a log file that looks like this: sd12@phenix97:/detain/sd12/logs > cat 20071205_detain_20071206.log 12320496 rows created. Commit... (11 Replies)
Discussion started by: sd12
11 Replies

10. Shell Programming and Scripting

Check for the correct date format in UNIx

Hi All, I am getting two input from User for Date from the command prompt when my script is executed . The date format i am taking is : DD-MM-YYYY so is there any method in Unix to validate the two input date. There might be many cases for these two date to be invalid.... (1 Reply)
Discussion started by: rawatds
1 Replies
Login or Register to Ask a Question