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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-13-2008
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,
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: May 2008
Posts: 6
Just re-bouncing this post. I would appreciate any help.
Reply With Quote
  #3 (permalink)  
Old 05-13-2008
robotronic's Avatar
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.
Reply With Quote
  #4 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 542
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
Reply With Quote
  #5 (permalink)  
Old 05-13-2008
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.
Reply With Quote
  #6 (permalink)  
Old 05-13-2008
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.
Reply With Quote
  #7 (permalink)  
Old 05-13-2008
robotronic's Avatar
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
sendmail

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0