The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extracting date from a filename laiko UNIX for Dummies Questions & Answers 7 11-25-2008 03:24 PM
Concatenating the filename with date Aswarth Shell Programming and Scripting 9 05-26-2008 09:56 AM
Appending date to filename aemunathan Shell Programming and Scripting 5 05-12-2008 07:21 AM
filename to contain date rkap Shell Programming and Scripting 4 04-06-2005 08:53 AM
Add date to a filename richard1975 UNIX for Dummies Questions & Answers 3 02-08-2005 01:37 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-15-2008
icy_blu_blu icy_blu_blu is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
Get date from filename

Hi all,
I have this files:
aaa20080714.log
bbbb20080714.log
ccccccc20080714.log

Can i get the 20080714 from each file?
  #2 (permalink)  
Old 07-15-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,262
Code:
sed 's/^[a-z]*\([0-9]*\).log/\1/g' infile
20080714
20080714
20080714
  #3 (permalink)  
Old 07-15-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
ls *.log | awk -v d=$(date +%Y) -F"." '{ pat=".*"d; gsub(pat,"",$1);print d$1}'
  #4 (permalink)  
Old 07-15-2008
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2005
Location: Switzerland - GE
Posts: 1,569
Just for the fun, and to give a good reason why one should learn awk and sed to survive in shell scripting:

ant:/tmp $ ll ccc*
-rw-r--r-- 1 vbe sys 0 Jul 15 15:26 ccccccc20080714.log
ant:/tmp $ ls|grep ccc*.log|cut -c$(echo $(expr $(ls|grep ccc*.log|wc -c) - 12)"-"$(expr $(ls|grep ccc*.log|wc -c) - 5))
20080714
  #5 (permalink)  
Old 07-15-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Or... try tr
Code:
echo aaa20080714.log| tr -dc '[:digit:]'
# if the filenames are in a file
tr -dc '[:digit:]'  < infile
  #6 (permalink)  
Old 07-15-2008
sudhamacs sudhamacs is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 98
ls *.log | awk -F'.' '{print substr($1,length($1)-7)}'
  #7 (permalink)  
Old 07-15-2008
icy_blu_blu icy_blu_blu is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
It's working, thanks alot guys.
I'm just learning awk & sed and got this trouble. Gonna dig deeper.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0