1

I am still a newbie to shell scripting and would like to make some backups of billing files I receive and then upload these to an FTP server directory, and upon completion archive the file into a new directory called archive. Here is what is tripping me up:

The billing files are sent with a random sub string included (ex. 020001.030002.XXXXX.01.2 where XXXXXX is changed daily). What I need to do is use something like awk to find the substring in the filename, copy it to a variable so I can use it to push the filename to the FTP server, and then archive the file. I then need to place the variable in something that resembles FILE=020001.030002.($VARIABLE).01.2.

I located this post on stackoverflow which is similar in nature, however since I have a random input file each time I am having difficulty figuring out how to accomplish this task. If this is not something which can be done through shell scripting is there another way (Python, etc) to accomplish this task?


Sample filenames - again they are randomly created by my billing server: (note there is not an extension on the end of these files)

  • 020001.030002.00320.01.2
  • 020001.030002.07243.01.2
  • 020001.030002.12048.01.2

Required format should leave this file name intact to be placed in a FTP script for upload such as:

#/bin/bash
FTPU="user" # ftp login name
FTPP="passwd" # ftp password
FTPS="ftp.server.com" # remote ftp server
FTPF="/home/backup/" # remote ftp server directory for $FTPU & $FTPP
LOCALFILE="020001.030002.($INSERT_VARIABLE_HERE).01.2"
ncftpput -m -u $FTPU -p $FTPP $FTPS  $FTPF $LOCALFILE
Community
  • 1
  • 1
psiphix
  • 23
  • 3
  • It's not clear (to me) how the original filename, with XXXXX is any different from FILE=... ${VARIABLE}... when VARIABLE=XXXXX. Both have 5 '.' delimited sections with numbers in each section. Can you edit your question to include 2-3 sample filenames, and the required format for the changed version? Note the formatting 'tool-bar' at the top of the edit-box. {} will format so your lines do not get merged together. Good luck. – shellter Mar 21 '12 at 19:26
  • Getting there! Good first post.. Still don't understand how `LOCALFILE="020001.030002.($INSERT_VARIABLE_HERE).01.2"` is differnt from `020001.030002.00320.01.2` or `020001.030002.07243.01.2` or .... Please edit once more to show what you think `${INSERT_VARIABLE_HERE}` will look like. Or are you saying that the `020001.030002.,.01.2` parts actually change all the time, and you want to reduce what is saved as a new filename to a std part 1,2, 4, 5 and only capture what is part 3 to change the filename? Good luck. – shellter Mar 21 '12 at 20:13

1 Answers1

0
f=020001.030002.00320.01.2
id=${f:14:5}
echo $id

result:

00320

With

id=${f:14:5}

you extract from position 14 5 characters.


Alternative approach:

f=020001.030002.00320.01.2
end=${f/020001.030002./}
echo $end
00320.01.2
core=${end/.01.2/}
echo $core
00320
user unknown
  • 35,537
  • 11
  • 75
  • 121
  • Yet since I don't know what f is going to be on any given day is there a way to automatically determine f from a directory path to further automate this so I could run the script from cron? – psiphix Mar 21 '12 at 20:25
  • @psiphix: Where do you receive the file? How do you now it is the new file? Is it the only file in a directory? – user unknown Mar 21 '12 at 20:42
  • /home/username/frombilling after I push the file to ftp directory I will archive the file and append the date to it like so (again I am missing the variable here as well: `date_formatted=$(date +%m%d%y%H%M) cp -r $1 $2.$date_formatted` – psiphix Mar 21 '12 at 21:21
  • What is $1 and $2? The date format leads to something like 0321122326. Your examples look different. If the file is the only file in /home/username/frombilling, you get it as `f=/home/username/frombilling`. If you know it is the only file from today, you get it as `f=/home/username/frombilling/*.*.*.*.*$(date +%m%d%y)*` for example. Maybe you make it another question. – user unknown Mar 21 '12 at 22:33