I need help to compare file name with regular expression and do something with file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I need help to compare file name with regular expression and do something with file
# 1  
Old 02-27-2017
I need help to compare file name with regular expression and do something with file

Code:
#!/bin/sh
Today=date '+%Y%m%d' 
for file in `ls *.csv *txt` 
do          
        echo "Start woprking with ${file}"
         if [ "${file}" = "*_${Today}*" ];  then
                 do something
         elif [ "${file}" = "*.${Today*}" ];  then
               do something 
               else
                   echo "Unkniowned file name"
               fi
        fi
done

Files names like this:
Code:
asd_ajfh_kjghjkg_20170227.csv
asd_ajfh_kjghjkg.20170227.csv
iytouy_jkhk_jghfadj_123_6453_20170227_U.csv

Script print first line before first if and doing nothing. Can somebody let me know where I am wrong? I am on AIX, korn, born, or c

I need it because depends of _Today .Today files will be processed differently

Last edited by vgersh99; 02-27-2017 at 07:30 PM.. Reason: code tags, please!
# 2  
Old 02-28-2017
Why not only select the files that match your required pattern when listing the files in the first place? This might get you started:-
Code:
#!/bin/ksh
Today=`date '+%Y%m%d'`
for file in *[._]${Today}*
do
   something with "${file}"
done

Note that I've added ` around the date command. Although I think this is deprecated, I'm not sure if your shell will support the $(date '+%Y%m%d') form.

Are there any files with spaces in the name that we have to handle?



Does this help?
Robin
# 3  
Old 02-28-2017
I have 70 different files. Some of them are: XXX_Today.csv, some of them XXXX.Today.csv. All of them can have different amount of XXXXX (fro 2 to 5) divide by _. The main problem is that I have to find the file, find Today and cut it from the main part, and what has left, for example XXX_CCCC_BBBB I open file find this part and make short form, like XCB and son on. I will check about Today, may be you are right. I will let you know as soon as I get to work

Last edited by rbatte1; 02-28-2017 at 08:52 AM.. Reason: Added ICODE tags and correct spelling
# 4  
Old 02-28-2017
How would iytouy_jkhk_jghfadj_123_6453_20170227_U.csv fit into that scheme?
# 5  
Old 02-28-2017
Do you have another solution?

---------- Post updated at 09:26 AM ---------- Previous update was at 08:53 AM ----------

Quote:
Originally Posted by rbatte1
Why not only select the files that match your required pattern when listing the files in the first place? This might get you started:-
Code:
#!/bin/ksh
Today=`date '+%Y%m%d'`
for file in *[._]${Today}*
do
   something with "${file}"
done

Note that I've added ` around the date command. Although I think this is deprecated, I'm not sure if your shell will support the $(date '+%Y%m%d') form.

Are there any files with spaces in the name that we have to handle?

Yes, my shell handle this format and before catch file I print ${Today} and it show me 20170228
Quote:
Does this help?
Robin
---------- Post updated at 09:35 AM ---------- Previous update was at 09:26 AM ----------

Robin

Note that I've added ` around the date command. Although I think this is deprecated, I'm not sure if your shell will support the $(date '+%Y%m%d') form.

Are there any files with spaces in the name that we have to handle?


Yes, my shell handle this format and before catch file I print ${Today} and it show me 20170228

Oleg

Last edited by RudiC; 02-28-2017 at 10:34 AM.. Reason: Adapted quoting to best guess.
# 6  
Old 02-28-2017
Guessing from your original code that you only want to process files with names ending with .csv or .txt, I would tend to try something like:
Code:
#!/bin/ksh
Today=$(date '%Y%m%d')
for file in *$Today*.csv *$Today*.txt
do	if [ -f "$file" ]
	then	echo do whatever with "$file"
	fi
done

or if you know that you won't encounter filename extensions like cxt and cst, you could shorten:
Code:
for file in *$Today*.csv *$Today*.txt

to:
Code:
for file in *$Today*.[ct][sx][vt]

# 7  
Old 02-28-2017
No,
I need files *_${Today}*.csv *_${Today}*.txt
and *.${Today}*.csv *.${Today}*.txt

For the second line I made a loop and it is perfectly fine.
The first line is still there are still a lot of questions, because I don't know the precise files names. When I find out, I will ask for help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep with Regular expression now working on file directories

Hello Everyone, I have a file sam1 with the below content SYSYSID;MANDT;/SIE/AD_Z0M_INDX;/SIE/AD_Z0M_KEY1 echo $Regex \bSYSYSID\b|\bMANDT\b|\b/SIE/AD_Z0M_INDX\b|\b/SIE/AD_Z0M_KEY1\b cat sam1 | grep -Eo $Regex I expect the result as SYSYSID MANDT /SIE/AD_Z0M_INDX /SIE/AD_Z0M_KEY1... (4 Replies)
Discussion started by: sam99
4 Replies

2. UNIX for Dummies Questions & Answers

How to using Regular expression to find file.?

Hi Gurus, I need to identify the file with below format: ABC20110101.DAT ABCD2011010103.DAT If I use ABC*\.DAT, it get two file. I want to get file after "ABC' then number, the ".DAT". I tried ABC* but it doesn't work. Thanks in advance. (9 Replies)
Discussion started by: ken6503
9 Replies

3. Shell Programming and Scripting

How to find out whether a file exists with the help of regular expression?

Hi all I have a list of file names in array. But this file names are not exact. so i want to search whether a file exists or not using regular expression. code snippet: if ; then echo "File exists" else echo "File does not exits" fi over here "*EQST*" should be treated as a regular... (4 Replies)
Discussion started by: Ganesh_more
4 Replies

4. Shell Programming and Scripting

regular expression with shell script to extract data out of a text file

hi i am trying to extract some specific data out of a text file using regular expressions with shell script that is using a multiline grep .. and the tool i am using is pcregrep so that i can get compatibility with perl's regular expressions for a sample data like this, i am trying to grab... (6 Replies)
Discussion started by: vemkiran
6 Replies

5. UNIX for Dummies Questions & Answers

Compare files with regular expression

Readers, Reading a previous post about comparing files using awk ('awk-compare-2-columns-2-files-output-whole-line', https://www.unix.com/shell-programming-scripting/168432-awk-compare-2-columns-2-files-output-whole-line.html), it is possible to adjust this, so that regular expression can be used... (8 Replies)
Discussion started by: linuxr
8 Replies

6. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

7. Shell Programming and Scripting

How to compare a file name with a regular expression !!

Hi, I need to compare file names in a folder with several strings(which are in regular expression format): For example: there is a file "objectMyHistoryBook" and there are several strings to compare this file name with: objectMyMaths*, objectMyEnglish*, objectMyHistory*,... (2 Replies)
Discussion started by: Lucifer_123
2 Replies

8. Shell Programming and Scripting

Using grep and regular expression to find class references in a c++ file

I'm trying to math all class references in a C++ file using grep with regular expression. I'm trying to know if a specific include is usuless or not, so I have to know if there is a refence in cpp. I wrote this RE that searches for a reference from class ABCZ, but unfortunately it isn't working... (0 Replies)
Discussion started by: passerby
0 Replies

9. Shell Programming and Scripting

AWK - compare $0 to regular expression + variable

Hi, I have this script: awk -v va=45 '$0~va{print}' flo2 That returns: "4526745 1234 " (this is the only line of the file "flo2". However, I would like to get "va" to match the begining of the line, so that is "va" is different than 45 (eg. 67, 12 ...) I would not have any output. That... (3 Replies)
Discussion started by: jolecanard
3 Replies

10. Shell Programming and Scripting

compare variable against regular expression?

is it possible? if so, how? i want to check a variable whether is it a number or letter in an if-else statement (6 Replies)
Discussion started by: finalight
6 Replies
Login or Register to Ask a Question