List files with a certain condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List files with a certain condition
# 1  
Old 05-07-2008
List files with a certain condition

Guys help me out here....

I have the following var: date_system=20080507

And I have a folder with many files with the following pattern:
test_%date%_reference, like

test_20080201_tokyo.csv
test_20080306_ny.csv

and so on...

What I need is:
1) list all files and them compare the date_system against the %date% that is shown in the files.
2) Remove all files with the %date% sixty days ago the current date_system.

How can I do it?
# 2  
Old 05-07-2008
One way:
Code:
#!/bin/ksh
sec()
{
        perl -e '
                use POSIX qw(strftime);
                $fmt = "%s";  # %s = seconds in epoch
                $mday = substr("$ARGV[0]", 6, 2);
                $mon =  substr("$ARGV[0]", 4 ,2);
                $year = substr("$ARGV[0]", 0 ,4);
                $hour = 0;
                $min =  0;
                $sec = 0 ;
                $secs= strftime($fmt, $sec, $min, $hour, $mday , $mon - 1, $year - 1900, -1, -1, -1);
                print int $secs;
                ' "$1"
}

today=$( date "+%Y%m%d" )
today=$( sec $today )

for file in *.csv
do
   filedate=$( echo $file | awk -F'%' '{print $2}')
   if [[ -z $filedate ]] ; then
        continue
   fi     
   olddat=$(sec $filedate)
   ddiff=$(( $today - $oldat ))
   echo "$today minus $oldat = $ddiff which is \c"
   if [[ $ddiff -lt 5184000 ]] ; then
      echo "less than 60 days"
   else
      echo "greater than equal to 60 days"
   fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare between two files with condition

Hello there. I am trying to compare two files. File1 Austria Mobile 1 United Kingdom Mobile 1 ... File2 Austria Mobile Vien 2 Austria Mobile Ostr 0 United Kingdom Mobile Dev 0.7 United Kingdom Mobile OST 1.5 What i want to do is to compare both files and... (12 Replies)
Discussion started by: dragonfly85
12 Replies

2. Shell Programming and Scripting

How to list files with specific condition?

Hi All, Seeking for your assistance on how to list all files with letter h15 above on files. ex. ab0320443h14.zip ab0320443b13.zip ar0330016b15.zip ar0330016h15.zip ar0330267a16.zip ar0330263c16.zip output: ar0330016h15.zip ar0330267a16.zip ar0330263c16.zip Please advise, (5 Replies)
Discussion started by: znesotomayor
5 Replies

3. Shell Programming and Scripting

I have this list of files . Now I will have to pick the latest file based on some condition

3679 Jul 21 23:59 belk_rpo_error_**po9324892**_07212014.log 0 Jul 22 23:59 belk_rpo_error_**po9324892**_07222014.log 3679 Jul 23 23:59 belk_rpo_error_**po9324892**_07232014.log 22 Jul 22 06:30 belk_rpo_error_**po9324267**_07012014.log 0 Jul 20 05:50... (5 Replies)
Discussion started by: LoneRanger
5 Replies

4. UNIX for Dummies Questions & Answers

Merging two files baased on condition

Hi All, I have two below files(fileds separated by space). File1 001078401 A 5A1 001078401 B 085 001030035 A 5A1 001030035 B 085 File2 001078401 C 001 001078401 D 065 001030035 C 001 001030035 D 065... (6 Replies)
Discussion started by: satyar
6 Replies

5. Shell Programming and Scripting

Merge two files by condition

I have two files as below A file /* comment for id1 */ "id1" = "x1" /* comment for id2 */ "id2" = "x2" /* comment for id3 */ "id3" = "x3" B file /* comment for id1 */ "id1" = "y1" /* comment for id2 */ "id2" = "x2" (22 Replies)
Discussion started by: mikezang
22 Replies

6. Shell Programming and Scripting

Merging two files with condition

I have two files of the type 111 222 10 112 223 20 113 224 30 114 225 20 and 111 222 9 444 555 8 113 224 32 666 777 25 I want to merge files based on 1 and 2nd column. if 1st and 2nd column are unique in file 1 and 2 keep... (3 Replies)
Discussion started by: digipak
3 Replies

7. Shell Programming and Scripting

How to check a list of 4-5 strings in IF condition?

Hi Unix Champs, I am a newbe to UNIX. I need to create a condition where I want to check the value of a variable with 5 different strings, if matched then exit 1. For ex: if ] then echo "yes, condition satisfied...." else echo "no, condition not satisfied..." fi ... (9 Replies)
Discussion started by: ustechie
9 Replies

8. Shell Programming and Scripting

Split a files into many files when condition

Hi Everyone, file.txt +++ a b c +++ d +++ asdf fefe fff Would like to have the output: file1.txt (22 Replies)
Discussion started by: jimmy_y
22 Replies

9. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

10. Shell Programming and Scripting

remove some files on a condition..

Hi.. when I do a ls -lt, I get a listing of about 200 files.. These are trace files and some of it I might not need.. To be clear, say in a given week , I might not need files that have been traced between 11 and 11:30 am on a particular day. How can I delete based on this condition ? Thanks,... (4 Replies)
Discussion started by: ST2000
4 Replies
Login or Register to Ask a Question