The UNIX and Linux Forums  

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
parsing xml with awk/sed ricgamch Shell Programming and Scripting 3 05-28-2008 11:39 AM
Awk Parsing bombcan Shell Programming and Scripting 2 04-24-2008 03:45 PM
parsing tungaw2004 UNIX for Dummies Questions & Answers 15 03-27-2007 09:33 PM
XML parsing handak9 High Level Programming 1 11-01-2004 08:13 PM
Text parsing question 98_1LE UNIX for Dummies Questions & Answers 3 03-24-2002 11:04 AM

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 05-13-2008
muser muser is offline
Registered User
  
 

Join Date: May 2008
Posts: 6
Thumbs up Parsing question

Hi Guys,

I was wondering if you could help me out -

I have a directory /home/users/datafiles/ which contain files "dat dd-mm-yy.xls"

I am trying to write a script which does the following -

(1) loops through all the files
(2) retrieves the dd-mm-yy string and converts it into a yyyymmdd to be stored as a variable


for xlsFile in dat*.xls
do
##scripting

echo $xlsDate
echo $formattedDate
echo $xlsFile
done

E.g. If I had file "dat 10-01-08.xls", these 3 would be the result -

10-01-08
20080110
dat 10-01-08.xls


Thanks in advance,
  #2 (permalink)  
Old 05-13-2008
muser muser is offline
Registered User
  
 

Join Date: May 2008
Posts: 6
Just re-bouncing this post. I would appreciate any help.
  #3 (permalink)  
Old 05-13-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
Please, don't rebounce your threads.

For your question, try something like this:


Code:
for xlsFile in dat*.xls
do
   d=${x:4:2}
   m=${x:7:2}
   y=${x:10:2}

   xlsDate="$d-$m-$y"
   formattedDate="20$y$m$d"

   echo $xlsDate
   echo $formattedDate
   echo $xlsFile
done

Obviously if $y is greater than a certain (imposed) value you need to add an if statement and use "19" instead of "20" in the formattedDate expression.
  #4 (permalink)  
Old 05-13-2008
muser muser is offline
Registered User
  
 

Join Date: May 2008
Posts: 6
Quote:
Originally Posted by robotronic View Post
Please, don't rebounce your threads.

For your question, try something like this:


Code:
for xlsFile in dat*.xls
do
   d=${x:4:2}
   m=${x:7:2}
   y=${x:10:2}

   xlsDate="$d-$m-$y"
   formattedDate="20$y$m$d"

   echo $xlsDate
   echo $formattedDate
   echo $xlsFile
done

Obviously if $y is greater than a certain (imposed) value you need to add an if statement and use "19" instead of "20" in the formattedDate expression.
Thanks but this errors on the first line after 'do', stating bad substitution.
  #5 (permalink)  
Old 05-13-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753

Code:
for file in "$(echo dat*)"
do
   xlsdate=$(echo $i | awk '{split($2,a,"\.xls");print a[1]}')
   fmtdate=$(echo $xlsdate | awk -F"-" '{for(i=NF;i>=1;i--) s=s""$i;print "20"s}')
   echo $xlsdate
   echo $fmtdate
   echo $file
done

  #6 (permalink)  
Old 05-13-2008
muser muser is offline
Registered User
  
 

Join Date: May 2008
Posts: 6
Quote:
Originally Posted by shamrock View Post
Code:
for file in "$(echo dat*)"
do
   xlsdate=$(echo $i | awk '{split($2,a,"\.xls");print a[1]}')
   fmtdate=$(echo $xlsdate | awk -F"-" '{for(i=NF;i>=1;i--) s=s""$i;print "20"s}')
   echo $xlsdate
   echo $fmtdate
   echo $file
done
Thanks for this code just prints a "20" and every file name on the same line.
  #7 (permalink)  
Old 05-13-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
Uh! I wrote "x" variable instead of "xlsFile"


Code:
for xlsFile in dat*.xls
do
   d=${xlsFile:4:2}
   m=${xlsFile:7:2}
   y=${xlsFile:10:2}

   xlsDate="$d-$m-$y"
   formattedDate="20$y$m$d"

   echo $xlsDate
   echo $formattedDate
   echo $xlsFile
done

Also, this works only with bash. You didn't specified which shell are you using. If not using bash, you may extract the same values through "cut" commands or something similar.
Closed Thread

Bookmarks

Tags
sendmail

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 07:04 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