![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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, |
|
||||
|
Quote:
|
|
||||
|
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.
|
|
|||||
|
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. |
![]() |
| Bookmarks |
| Tags |
| sendmail |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|