want to get last year and month from the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting want to get last year and month from the file
# 1  
Old 05-18-2011
want to get last year and month from the file

Hi
I have files like

abc_cd_20110302_123423
abc_cd_ef_20110301_123423
abc_cd_ef_20110403_123423
abc_ef_20110401_123423

I want to extract the
the year and month associated with each file.
I tried
Code:
logfileyearmonth=`echo $logfile | awk -F_'{print $NF}'`

Any other way can I get the yearmonthdate from the file which is in bold.
This is basicaly the last second field.

Thanks and Regards
Dgmm
# 2  
Old 05-18-2011
If "NF" is the last field, then "(NF-1)" is the second last field:
Code:
logfileyearmonth=$(awk -F_ '{print $(NF-1)}' $logfile)

# 3  
Old 05-18-2011
I have one of the file as
acs_log_purge_20110517_234239
Got following error.
Code:
]+ + awk -F_ {print $(NF-1)} acs_log_purge_20110517_234239 
awk: input file "acs_log_purge_20110517_234239": The system cannot find the file specified.

# 4  
Old 05-18-2011
Sorry, I misread your question slightly.

Code:
logfileyearmonth=`echo $logfile | awk -F_ '{print $(NF-1)}'`

(I don't know where "$logfile" is set, and making no assumptions, $(NF-1) will give you the field you are looking for)

Code:
# Test, create some files:
echo "abc_cd_20110302_123423
abc_cd_ef_20110301_123423
abc_cd_ef_20110403_123423
abc_ef_20110401_123423" | xargs touch

$ ls * | awk -F_ '{print $(NF-1)}'
20110302
20110301
20110403
20110401

# 5  
Old 05-18-2011
Hi

I am not looking from ls.
Here is my code and I want to fetch year and month from the file which I have alread read.

Code:
cd Log_dir
 
for f in *.log
do
  let  cnt="cnt + 1"
  logfilename=$f
  Log "Log file Name $logfilename"
  logfile=`basename $logfilename .log`
  Log "logfile without extension $logfile"
  logfiledate=$`awk -F_ '{print $(NF-1)}' $logfile`
  Log "logfiledate $logfiledate"
done
Log "Total file count $cnt"

The problem is I am not getting logfiledate.
Please let me know how can I extract this field. My datetime in file is last 15 characters. f_CCYYMMDD_HHMMSS.
Just want to get CCYYMM from the file.
Thanks and Regards\
Dgmm
# 6  
Old 05-18-2011
If you have the logfile name already, then go back to what you had, and use $(NF-1) from the beginning:

Code:
...
logfiledate=$(echo $logfile | awk -F_ '{print $(NF-1)}')
...



Code:
cd Log_dir
 
for f in *.log
do
  let  cnt="cnt + 1"
  logfilename=$f
  Log "Log file Name $logfilename"
  logfile=${logfilename%.*}
  Log "logfile without extension $logfile"
  logfiledate=$(echo $logfile | awk -F_ '{print $(NF-1)}')
  Log "logfiledate $logfiledate"
done
Log "Total file count $cnt"

This User Gave Thanks to Scott For This Post:
# 7  
Old 05-18-2011
Thank you.
This worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find all files other than last two dates per month and year?

Hi All, lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 a.txt -rwxr-xr-x 1 user userg 1596 Mar 19 2012 b.txt -rwxr-xr-x 1 user userg 1596 Mar 22 2012 c.txt -rwxr-xr-x 1 user userg 1596 Mar 24 2012 d.txt... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

2. Shell Programming and Scripting

How to find all files for same month and year?

Hi All, I find all files for same month and year lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 c.txt -rwxr-xr-x 1 user userg 1596 Mar 21 2012 d.txt -rwxr-xr-x 1 user userg 1596 Mar 22 2012 f.txt -rwxr-xr-x 1... (8 Replies)
Discussion started by: Makarand Dodmis
8 Replies

3. UNIX for Dummies Questions & Answers

Simpler next month year program

I have created this program to get the next month and year. Is there a simpler way. #!/bin/ksh string=`cat Date.txt` year=`echo $string | cut -c 1-4` month=`echo $string | cut -c 5-6` echo $year$month mon=`expr $month + 1` if ; then mon=0$mon echo $mon fi if ; then month=01 ... (2 Replies)
Discussion started by: w020637
2 Replies

4. Forum Support Area for Unregistered Users & Account Problems

June is indeed the 6th month of the year

So why does the password reset page tell me I've answered the question incorrectly? First, I tried to register. Never received an email. So I tried to register with a different email/username. I was told it was taken. I tried to have a password sent to my email, but June is not the 6th month... (1 Reply)
Discussion started by: Unregistered
1 Replies

5. Programming

Interval year to month

Hi, I'm working on a Informix4gl module. I'm just trying to find out any built-in function to fetch only the year/month from an INTERVAL YEAR TO MONTH data value. Please let me know, if there are any functions to do this. If not, let me know for any alternative solutions to attain this. ... (5 Replies)
Discussion started by: dvah
5 Replies

6. Shell Programming and Scripting

how to increment days according to year & month

Hiii i have a file with data as shown below: a.dat: RAO 1900 2 7 0 0 0.00 10.8000 76.8000 10.0 0 0.00 0 6.00 0.00 0.00 0 0.00 6.00 0 NULL LEE 1901 2 15 0 0 0.00 26.0000 100.0000 0.0 0 0.00 0 0.00 0.00 0.00 0 6.00 6.00 0 NULL RAO 1901 4... (3 Replies)
Discussion started by: reva
3 Replies

7. Shell Programming and Scripting

Concatenating Files In A Year/Month/Day File Structure

Hi Im trying to concatenate a specific file from each day in a year/month/day folder structure using Bash or equivalent. The file structure ends up like this: 2009/01/01/products 2009/01/02/products .... 2009/12/31/products The file I need is in products everyday and I need the script to... (3 Replies)
Discussion started by: Grizzly
3 Replies

8. Shell Programming and Scripting

Day of month and year to mmddyyyy

I have a date that looks like this: 2008/100:18:40:47.040 I need it to look like this: 2008 04 09 18 40 47 040 I have looked at datecalc and it doesn't seem like it takes the day of year for this year (or whatever year is input) and converts it into month and day. It also has to account... (2 Replies)
Discussion started by: ajgwin
2 Replies

9. UNIX for Advanced & Expert Users

how to get the last month and year in UNIX

how to get the last month and year in UNIx (2 Replies)
Discussion started by: Vijay06
2 Replies
Login or Register to Ask a Question