I'm new in this shell scripting subject, I´m looking forward for someone to give me a hint or advice as to how to tackle my requirement, which is as follows:
We have a Linux process that runs periodically every day, this process dumps a text file with always the same name overwriting the previous one (information is incrementally added). What I need to accomplish is to rename this file as soon as possible once is generated, following the following nomenclature:
The year, month and day has to be fetched from the first line of the file content, here is an example:
The year, date and month will always be found in a fixed position from this first line (position 17 to 24), which in this example is as follows:
This date is expressed in Month-Day-Year format, and my requirement needs it to be [year][month][day]
Another thing that I need to track is if original file is currently being used, since I dont want to rename the file in the middle of the generation process.
At the end, filename should be:
Kindly requesting your help with my requirement.
Many many thanks in advanced for any help you can provide.
Last edited by Don Cragun; 06-20-2014 at 05:50 PM..
Reason: Change FONT tags to CODE tags.
Longhand from the CLI only, OSX 10.7.5, default bash terminal...
This makes assumptions that the first field is constant length.
If not then a slightly more difficult approach is needed...
EDIT:
Forgot about the file locking bit, research:-
Last edited by wisecracker; 06-20-2014 at 04:51 PM..
Nothing has been said about the size of the input file, nor about how the process that updates this file is written.
If the file size is more than a few lines (and especially if it could be large), reading the entire contents of the file into a variable to get a few characters out of the 1st line is wasting time and space. I would strongly suggest using:
rather than:
If the process that is writing to this file opens the file once and writes data to the file using that single file descriptor, you don't need to worry about locking at all. Renaming the file (as long as you don't move it to a different file system) won't affect the file descriptor being used by the process that is adding data to the file. All of the data for an update will be appended to the file you're moving or to a new file after you've moved it.
But, if the program that is updating the file is opening the file multiple times, then you do need to make sure that you don't move the file while the updater is running. The fuser utility probably won't help you in the case where it matters. You'll need a way to synchronize file updates with file renames and that will require more information than you have provided. It is easy to create a lock file, but both the updater and the mover need to agree on the lock and cooperate with each other to make that work. If just one of the two parties tries to guess whether or not the other one is active; eventually something will go wrong.
Are you responsible for both the updater and this code to rename the file?
Hello everyone,
Posting here after a long time, been away from unix world lately and it seems I have forgotten my shell scripting completely. I have a requirement where a csv file contains following columns:
Full Registration VIN Stock... (14 Replies)
Hi
I have numerous files names
product_host_result_B1000842.txt
product_host_result_B1000847.txt
product_host_result_C1000842.txt
product_host_result_C1000848.txt
etc. I need them renamed so that the 'product_host_result' becomes 'output_product_host' but the rest of the filename is... (6 Replies)
Hello All,
I need help in writing a batch script.
I have 100 zip files in a folder. Each zip file has a unique name starting with XYZ_12345.zip Each zip file contains single csv file
I would like to batch extract the files and also rename the extracted csv as per the original zip name... (6 Replies)
HI,
I have a file serverlist in that all host names are placed.
i have written a small script
#./testping
#! /bin/bash
for i in `cat serverlist`
do
ping $i >> output.txt
done
so now it creates a file output.txt till here fine..
now each time i run this script the output file... (4 Replies)
hi,
I need some help in writing shell script in a bourne shell.I am trying to rename the file..
eg.
find /root/data -type f -name "text*) | while read FILES
do
newfile=${FILES/type_2.0_20101208_34.xml / tmp.xml}
mv "$FILES" "$newfile"
done
above written script is working...If the... (7 Replies)
Hi,
I'm desperately in search for a solution of the following problem:
I have a directory full of zip-files. All these zip-files contain a single file with a name that should be used for the name of the zip-container.
Anybody a good idea. I'm an absolute beginner in shell scripting - so please... (7 Replies)
Hello all.
I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).:b:
I have the following problem that I have not been able to figure out how to take care of and I was wondering if anyone could help me out.:confused:
I have all of my music stored in... (7 Replies)
:confused:
Hello,
Is there any way to use the dir command / some DOS Script to select only first file of similar pattern of files in a direcotory and rename it for example, one directory has 5 files
abc_1005.txt
abc_5256.txt
abc_2001.txt
abc_2003.txt
abc_3006.txt
by use script I would... (2 Replies)