Script pick elements according to the date in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script pick elements according to the date in file
# 1  
Old 01-16-2013
Script pick elements according to the date in file

Hello,

i am having file details having elements mentioned below

Code:
Asia jan 18
Europe Jan 19
America Jan 20

I made script but it picks all the elements, i want to pick the elements according to the date to run the script i made cron job.

Please advise how it can work....

Last edited by Scott; 01-16-2013 at 03:20 PM.. Reason: Code tags for code and data, please
# 2  
Old 01-16-2013
Code:
mon=$( date +"%b" )
dd=$( date +"%d" )

awk -v M=$mon -v D=$dd '$2==M&&$3==D{ print }' filename

# 3  
Old 01-16-2013
I was not able to run

Thanks Bipin for prompt reply but it didn't worked can you please provide code based on below example

I will be making cron job which will be running every morning at 9:00 AM and i want to run the script only on the elements according to the date,

if there is no element on the specified date than the scipt should not run....

Code:
Asia   Jan 17
Europe Jan 19
North America Jan 20
South America Jan 22


Last edited by Scott; 01-16-2013 at 03:21 PM.. Reason: Code tags for code and data, please
# 4  
Old 01-16-2013
This is pretty much straightforward problem. I recommend always searching this forum or internet and put some effort by yourself before posting such questions.

Here is the code that will check if an entry with current date exists. If no, then exit:
Code:
DT=$( date +"%b %d" )
[[ $( grep -c "$DT" filename ) -eq 0 ]] && exit 1

# 5  
Old 01-16-2013
Thanks

Thanks Bipin.

---------- Post updated at 03:55 PM ---------- Previous update was at 02:26 PM ----------

I tried the code but it didnt worked and searched the forum also didnt find the solution than i tired one command mentioned below but this also not giving the proper output.

Code:
grep -c "date +"%d-%b"" File.txt
0
cat File.txt
Asia 16-Jan
Europe 17-Jan
America 18-Jan

output should only show Asia......

Please advise if there is some existing forum on this issue.

Last edited by Scott; 01-17-2013 at 01:39 AM.. Reason: Code tags
# 6  
Old 01-16-2013
That is not going to work. You cannot put a command just inside double quote and run it.

Use sub-shell:-
Code:
grep "$( date +"%d-%b" )" File.txt

Or use back-ticks:-
Code:
grep "`date +"%d-%b"`"  File.txt

I recommend using the sub-shell.
# 7  
Old 01-16-2013
Second commad worked

Thanks Bipin second command worked and showed output but first one showed error.....

Code:
grep  "$(date +"%d-%b")" File.txt
Illegal variable name.


Last edited by Scott; 01-17-2013 at 01:37 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Should pick latest file within past 3 days using UNIX script and perform steps in message below.

Hi , Can anyone help me how do perform below requirement in unix. Step1:we will receive multiple files weekly with same name(as below) in a folder(In folder we will have other files also def.dat,ghf.dat) Filenames: 1) abc_20171204_052389.dat 2)abc_20171204_052428.dat DON'T modify... (23 Replies)
Discussion started by: sunnykamal59
23 Replies

2. Shell Programming and Scripting

Needs perl script - pick right file name

Hi., I need in perl type ...where this is an example : assume a directory has 3 files namely: file1_1445566_201501022.txt file1_1445566_201502024.txt file1_1445566_201503009.txt where ,after second underscore, the number is in YYYYMMDD time-stamp format, I need perl script to pick up... (3 Replies)
Discussion started by: alnhk
3 Replies

3. Shell Programming and Scripting

Script to determine Date,TotalFile,total size of file based on date

I have file listed like below -rw-r--r--+ 1 test test 17M Nov 26 14:43 test1.gz -rw-r--r--+ 1 test test 0 Nov 26 14:44 test2.gz -rw-r--r--+ 1 test test 0 Nov 27 10:41 test3.gz -rw-r--r--+ 1 test test 244K Nov 27 10:41 test4.gz -rw-r--r--+ 1 test test 17M Nov 27 10:41 test5.gz I... (5 Replies)
Discussion started by: krish2014
5 Replies

4. UNIX for Dummies Questions & Answers

How to pick the latest file with date as one among the file name.( not exactly present date.)?

i have files like 1)20131112_abc_01.csv and 2)20131113_abc_01.csv and 3)20131113_abc_02.csv when i try to fetch the file in the next day. it shud pick the third file.. plz help me.. and i use `date +"%Y%m%d"` command to fetch..it fetches the current date... (2 Replies)
Discussion started by: applepie
2 Replies

5. Shell Programming and Scripting

Control - M Scheduler is not able to pick my shell script

Hi, Below is a shell script that i made:- #!/bin/ksh #path=/opt/tibco/shared/adaptadores/SSCC/EVEREST/input/ if ; then echo "ZIP Exists and now Processing" for files in /opt/tibco/shared/adaptadores/SSCC/EVEREST/input/T010B04.* do unzip $files echo "Files Unzipped" echo $files... (4 Replies)
Discussion started by: mmtrexon
4 Replies

6. Shell Programming and Scripting

Shell script to pick the oldest file

hi i need shell scrip to pick the oldest file... i.e i need a script to pick the oldest file I had input files in my input folder that are created on saturday,monday,tuesday .. i want a script such that first the script should pick saturday file ,then monday and then file created on... (5 Replies)
Discussion started by: hemanthsaikumar
5 Replies

7. Shell Programming and Scripting

Pick out the various extension file from log of the same date

extension file from log (3 Replies)
Discussion started by: NehaKrish
3 Replies

8. UNIX for Dummies Questions & Answers

unix script that will pick up first 10 file

Suppose I have a unix file which contain a lost of 60 files like filename1 filename2 ... .. ... filename60 I want to write a unix script that will pick up first 10 files in first run 10-20 files in 2 run 20-30 files in 3 run 30-40 files in 4 run 40-50 files in 5 run 50-60 files in 6... (1 Reply)
Discussion started by: er_zeeshan05
1 Replies

9. Shell Programming and Scripting

Script to pick out files from a file which are older than 15 days

Hi , I have a file abc.txt which actually contains a directory and file list -rwxrwxr-x 1 dmadmin mmasapp 334 Dec 03 2001 aafs_006.ksh -rwxrwxr-x 1 dmadmin mmasapp 1270 Nov 13 2001 mcl_deposit_ftp.ksh -rwxrwxr-x 1 dmadmin mmasapp 925 Oct 31 2001 ... (1 Reply)
Discussion started by: viv1
1 Replies

10. Shell Programming and Scripting

Sort files by Date-timestamps available in filename & pick the sortedfiles one by one

Hi, I am new to Unix shell scripting. Can you please help me with this immediate requirement to code.. The requirement is as given below. In a directory say Y, I have files like this. PP_100000_28062006_122731_746.dat PP_100000_28062006_122731_745.dat PP_100000_28062006_122734_745.dat... (4 Replies)
Discussion started by: Chindhu
4 Replies
Login or Register to Ask a Question