Visit Our UNIX and Linux User Community


Script to locate date in filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to locate date in filename
# 1  
Old 05-29-2014
Script to locate date in filename

I am looking for a way to find a date in the file without using find. for example something like this:

Code:
files=`ls |grep txt`
YEST=`TZ="GMT+24" date +'%m-%d-%Y'|sed 's/^0//g' |sed 's/$/.txt/g'`
YES1=`TZ="GMT+48" date +'%m-%d-%Y'|sed 's/^0//g' |sed 's/$/.txt/g'`
if [[ $files = "5-28-2014" ]]; then echo yes;else echo no;fi

There are a number of files that have 5-28-2014.txt in the name, even one file that is named just 5-28-2014.txt, but I keep getting "no" when the script returns the value. Any ideas?

Also the YEST variable does contain 5-28-2014.txt, I echoed to check it
# 2  
Old 05-29-2014
Code:
if [[ $files == "5-28-2014" ]]; then echo yes;else echo no;fi

# 3  
Old 05-29-2014
In bash, use regexp comparison operator:
Code:
#!/bin/bash

for file in *.txt
do
        [[ "$file" =~ "5-28-2014" ]] && echo "yes" || echo "no"
done

# 4  
Old 05-29-2014
Its not clear for me, about your aim, sorry if I misunderstood

try

Code:
if grep "5-28-2014" <(ls *.txt)>/dev/null; then echo yes; else echo no;fi

if grep "5-28-2014" <<<$(ls *.txt)>/dev/null; then echo yes; else echo no;fi


Last edited by Akshay Hegde; 05-29-2014 at 02:23 PM..
This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 05-29-2014
Quote:
Originally Posted by newbie2010
I am looking for a way to find a date in the file without using find. for example something like this:

Code:
files=`ls |grep txt`
YEST=`TZ="GMT+24" date +'%m-%d-%Y'|sed 's/^0//g' |sed 's/$/.txt/g'`
YES1=`TZ="GMT+48" date +'%m-%d-%Y'|sed 's/^0//g' |sed 's/$/.txt/g'`
if [[ $files = "5-28-2014" ]]; then echo yes;else echo no;fi

There are a number of files that have 5-28-2014.txt in the name, even one file that is named just 5-28-2014.txt, but I keep getting "no" when the script returns the value. Any ideas?

Also the YEST variable does contain 5-28-2014.txt, I echoed to check it
First, note that the two lines marked in red above make absolutely no difference to the following if statement. The variables you define on those lines are not referenced after they are defined.

Second, if we supposed that you have two files named with names containing "txt", say for instance 5-28-2014.txt and txtfile. In this case the 1st line of your script sets the variable files to the value:
Code:
5-28-2014.txt
txtfile

The last line in your script then compares this two line value to the string "5-28-2014" and will find (even after changing the = to ==) that the strings "5-28-2014.txt\ntxtfile" and "5-18-2014" are not the same.

Yoda already suggested a way around this assuming you are looking for the date in the file's name. You could also try something like the following for that:
Code:
for file in *5-28-2014*.txt
do
        [ "$file" = "*5-28-2014*.txt" ] && echo 'no' && break
        printf "process file: %s\n" "$file"
done

which will work with any shell that recognizes basic POSIX standard shell syntax.

If you're looking for the date as text inside a file, rather than in the file's name, Akshay suggested a way to do that.
This User Gave Thanks to Don Cragun For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #91
Difficulty: Easy
To change to another directory in Linux, you use the cd command.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to change existing date to current date in a filename?

Suppose i have a list of files in a directory as mentioned below 1. Shankar_04152019_ny.txt 2. Gopi_shan_03122019_mi.txt 3. Siva_mourya_02242019_nd.txt .. . . . . 1000 . Jiva_surya_02282019_nd.txt query : At one shot i want to modify the above all filenames present in one path with... (4 Replies)
Discussion started by: Shankar455
4 Replies

2. Shell Programming and Scripting

How to append date to filename, but base it on yesterday's date?

Hello, I'd like to write a monthly archive script that archives some logs. But I'd like to do it based on yesterday's date. In other words, I'd like to schedule the script to run on the 1st day of each month, but have the archive filename include the previous month instead. Here's what I... (5 Replies)
Discussion started by: nbsparks
5 Replies

3. UNIX for Advanced & Expert Users

Tring to locate a script

Hello, We have a process on our Linux RedHat machine creating symbolic links and moving around some files from domain to domain. The issue is that the programmer has left a long time ago and nobody knows how the program is called, where it is and how the scheduling is setup. It runs every day for... (7 Replies)
Discussion started by: Indalecio
7 Replies

4. Shell Programming and Scripting

Please help I want script to check filename, size and date in specify path.

Please help, I want script to check filename, size and date in specify path. I want output as: DATE: YYYYMMDD HH:MM ------------------------------------------------ fileA,filesize,yyyy mm dd HH:MM fileA,filesize,yyyy mm dd HH:MM fileA,filesize,yyyy mm dd HH:MM fileA,filesize,yyyy mm dd... (1 Reply)
Discussion started by: akeji
1 Replies

5. Shell Programming and Scripting

Get the oldest date based on date in the filename

I am using ksh93 on Solaris. Ok, this may seem like a simple request at first. I have a directory that contains sets of files with a YYYYMMDD component to the name, along with other files of different filespecs. something like this: 20110501_1.dat 20110501_2.dat 20110501_3.dat... (2 Replies)
Discussion started by: gary_w
2 Replies

6. Shell Programming and Scripting

date from filename

Hi all I have the following question: With this command, I get the latest file in a directory. lastfile =`ls -1tr | tail -n 1` echo $lastfile The output is then: partner131210.txt (meaning 13th December 2010) My goal is to get the date into a variable and to obtain a final variable... (4 Replies)
Discussion started by: davis77
4 Replies

7. UNIX for Dummies Questions & Answers

Date in filename

how do i add the date for the filename? for example filename20080917 (3 Replies)
Discussion started by: khestoi
3 Replies

8. Shell Programming and Scripting

Get date from filename

Hi all, I have this files: aaa20080714.log bbbb20080714.log ccccccc20080714.log Can i get the 20080714 from each file? (6 Replies)
Discussion started by: icy_blu_blu
6 Replies

9. Shell Programming and Scripting

script to locate servers

I am a newbie in shell scripting and I need to produce a script to work and achieve the following: 1) Look into the file /etc/defaultrouter , and store the value in it 2) If the value is a number, go to LIST and print out the second column corresponding to the value.(eg London) 3) If the... (5 Replies)
Discussion started by: ibroxy
5 Replies

10. Shell Programming and Scripting

filename to contain date

hello, can anyone tell me how to rename a filename in a script to contain the current date? i have searched for the answer but with little success! many thanks rkap (4 Replies)
Discussion started by: rkap
4 Replies

Featured Tech Videos