Extract date from filename and create a new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract date from filename and create a new file
# 1  
Old 03-03-2011
Extract date from filename and create a new file

Hi, i have a filename CRED20102009.txt in a server

20102009 is the date of the file ddmmaaaa format

the complete route is
/dprod/informatica/Fuentes/CRED20102009.csv

i want to extract the date to create a new file named Parameters.txt

I need to create Parameters.txt with this content in the same /dprod/informatica/Fuentes route

[Comercial.AACPWM001]
$$Date = 20091020 aaaammdd format

how i can do that in unix in a shell, i'm beginner

Thanks
# 2  
Old 03-03-2011
Code:
FILENAME="/dprod/informatica/Fuentes/CRED20102009.csv"
DIRNAME=$(dirname $FILENAME)
echo $FILENAME | sed 's/^.*\(..\)\(..\)\(....\)\....$/[Comercial.AACPWM001]\n$$Date = \3\2\1 aaammdd format/' > $DIR/Parameters.txt

The string in your parameters.txt file is just about long enough to fetch the date into another shell var and then cat it out (this also makes it easier to update the parameters format at a later date):

Code:
FILENAME="/dprod/informatica/Fuentes/CRED20102009.csv"
DIRNAME=$(dirname $FILENAME)
DATESTR=$(echo $FILENAME | sed 's/^.*\(..\)\(..\)\(....\)\....$/\3\2\1/')
 
cat <<EOF > $DIR/Parameters.txt
[Comercial.AACPWM001]
\$\$Date = $DATESTR aaammdd format
EOF


Last edited by Chubler_XL; 03-03-2011 at 07:33 PM.. Reason: Fix some typos
# 3  
Old 03-03-2011
extracting the date from filename

Try this ..
Code:
CompletePath="/dprod/informatica/Fuentes/CRED20102009.csv"
JustFileName=`basename $CompletePath`
JustDate=`echo $JustFileName|awk -F"." '{print $1}'|awk '{print substr($1,length($1)-7, length($1))}'`


Once you get the date alone, you can do the remainder

Hope this helps
Vj

Last edited by Franklin52; 03-04-2011 at 03:03 AM.. Reason: Please use code tags, thank you
# 4  
Old 03-03-2011
mvijayv OP wanted to swap the date around (see that the year is in the front).
# 5  
Old 03-03-2011
thanks a lot guys, after several intents i can realize it.
Code:
dia=`ls /dprod/informatica/fuentes/apolo/Ejem????????.txt|cut -c44-45`
mes=`ls /dprod/informatica/fuentes/apolo/Ejem????????.txt|cut -c42-43`
ano=`ls /dprod/informatica/fuentes/apolo/Ejem????????.txt|cut -c38-41`
fecha="$ano$mes$dia"
var1='[Comercial.AACPWM001]'
var2='$$Date='
var3="$var2$fecha"
echo $var1 > /dprod/informatica/fuentes/apolo/Parameters.txt
echo $var3 >>  /dprod/informatica/fuentes/apolo/Parameters.txt

thanks a lot guys

Last edited by Franklin52; 03-04-2011 at 03:04 AM.. Reason: Please use code tags
# 6  
Old 03-03-2011
Great to see your progressing, just be carefull of using cut that way as the numbers 44, 38, etc are very dependant on the length of filename and directory pathes. The sed command I posted earlier cuts the date from the end of the string avoiding these issues.
# 7  
Old 03-03-2011
Code:
JustDate=`echo $JustFileName|awk -F"." '{print $1}'|awk '{print substr($1,length($1)-3,length($1))substr($1,length($1)-5,length($1)-10)substr($1,length($1)-7,length
($1)-10)}'`

This should give the date in the format required.
Overlooked that in the beginning. Thx Chubler!

Last edited by Franklin52; 03-04-2011 at 03:04 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract date and time part from filename

Hi, I am facing one scenario in which I need to extract exact position of date and time from the name of the files. For example, Below is the record in which I need to extract position of YYYYMMDD,HHMISS and YYMMDD. Date and time variables can come more than once. I need to use these position... (13 Replies)
Discussion started by: Prathmesh
13 Replies

2. UNIX for Beginners Questions & Answers

How to extract when filename contains file seperator..?

Hi, I want to extract part of filename, for eg: File="010020004_S-TOR-Sort-CASAP_20170519_121504_0007.TXT" here i need first 5 words of file i.e. FilePart="$(echo "${File%"${File#******}"}")" Echo $FilePart 010020004_S-TOR-Sort-CASAP But what if i get filename like below: ... (3 Replies)
Discussion started by: gnnsprapa
3 Replies

3. UNIX for Beginners Questions & Answers

How to extract date and time from filename?

Hi, I'm totally new in sell script and working with a shell code. I want to extract the date and time from the filenames. The filenames are different but all of them begins with WI_ SCOPE_: WI_SCOPE_DATA_CHANGE_2017-09-12_15-30-40.txt WI_SCOPE_BACK_COMPLETE_QUEUE_2017-09-12_15-31-40.txt... (5 Replies)
Discussion started by: Home
5 Replies

4. Shell Programming and Scripting

Extract data according to keys from filename mentioned in file

Hello experts, I want to join a file with files whosE names are mentioned in one of the columns of the same file. File 1 t1,a,b,file number 1 t1,a,c,file number 1 t2,c,d,file number 2 t2,c,e,file number 2 t2,c,f,file number 2 t2,c,g,file number 2 t3,e,f,file number 3 file number 1... (3 Replies)
Discussion started by: ritakadm
3 Replies

5. UNIX for Dummies Questions & Answers

Extract Date part from the filename

Hi All, I have incoming source files abcmmyy.txt I need to extract the mmyy part from the filename and pass that to a variable . I really appreciate your quick response on this. Thanks raj (7 Replies)
Discussion started by: rajeevm
7 Replies

6. UNIX for Dummies Questions & Answers

Create file with last month in filename

Hi, I have a bash script which outputs a file with current year and month in the filename. Last line is: cat result_* > output.$(date +%y%m) So for Feb 2013 this gives me output.1302 Now I am requested to run the script every first day of the month, but with the previous month in... (4 Replies)
Discussion started by: viscacha
4 Replies

7. Shell Programming and Scripting

Extract date from filename and set timestamp

I have lots of files in this format: dvgrab-2003.06.29_15-30-24.mpg The numbers represents the date and time (YYYY.MM.DD_HH-MM-SS) How can I extract the dates from the filenames, and use the dates in the file timestamp? I guess this can be done by using "find", "sed" and "touch"? Can... (6 Replies)
Discussion started by: qwerty1234
6 Replies

8. UNIX for Dummies Questions & Answers

shell scripts - create a filename with the date appended

I am looking to do something where if I created a file named backup,or whatever it would print a name like “backup_Apr_11_2011”. Thanks citizencro (1 Reply)
Discussion started by: citizencro
1 Replies

9. UNIX for Dummies Questions & Answers

Extract first line of a file and use as filename

I am trying to find a way to create a script which will extract the first line of a file and then rename the file (or create a new file with the same content as the old file) using the first line as the name. The first line being a single word, that is. I am hopeless at programming, if anyone can... (5 Replies)
Discussion started by: s.plumb
5 Replies

10. Shell Programming and Scripting

create filename with 'DD/MM/YYYY' date format

Hi, I can use the following command to create a file with some name then underscore and then date appended to it in the format 'DD-MM-YYYY': touch "newfile_`date '+%d-%m-%Y'`" But it gives me error when I try with the similar command to create a file with the date format 'DD/MM/YYYY'. I... (4 Replies)
Discussion started by: royalibrahim
4 Replies
Login or Register to Ask a Question