Help with file rename script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with file rename script
# 1  
Old 06-20-2014
RedHat Help with file rename script

Hello,

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:

Code:
[Fixed description string]-[Fixed numeric code ]-[year][month][day].txt

The year, month and day has to be fetched from the first line of the file content, here is an example:

Code:
M16540035421004411262012N 11        SAMPLE WORD SAMPLE WORD

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:

Code:
11262012

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:

Code:
 
Fixed description-Fixed numeric code-20121126.txt

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.
# 2  
Old 06-20-2014
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...
Code:
Last login: Fri Jun 20 18:11:46 on console
AMIGA:barrywalker~> echo 'M16540035421004411262012N 11        SAMPLE WORD SAMPLE WORD' > /tmp/sample
AMIGA:barrywalker~> cat < /tmp/sample
M16540035421004411262012N 11        SAMPLE WORD SAMPLE WORD
AMIGA:barrywalker~> text=$(cat < /tmp/sample)
AMIGA:barrywalker~> echo "$text"
M16540035421004411262012N 11        SAMPLE WORD SAMPLE WORD
AMIGA:barrywalker~> text="${text:20:4}${text:16:4}"
AMIGA:barrywalker~> echo "$text"
20121126
AMIGA:barrywalker~> # REMEMBER TO BACKUP YOUR ORIGINAL FILE.
AMIGA:barrywalker~> mv /tmp/sample /tmp/"Fixed description-Fixed numeric code-$text.txt"
AMIGA:barrywalker~> ls -l /tmp/
total 24
-rw-r-----  1 root         _lp    1193 20 Jun 18:11 0001653a4f82c
-rw-r-----  1 root         _lp    1234 20 Jun 18:11 0001653a7018c
-rw-r--r--  1 barrywalker  wheel    60 20 Jun 20:29 Fixed description-Fixed numeric code-20121126.txt
drwx------  3 barrywalker  wheel   102 20 Jun 18:11 launch-B5kUmu
drwx------  3 barrywalker  wheel   102 20 Jun 18:11 launch-UEgXYb
drwx------  3 barrywalker  wheel   102 20 Jun 18:11 launch-ce6iKr
drwx------  3 barrywalker  wheel   102 20 Jun 18:11 launch-gfvxa4
drwx------  3 barrywalker  wheel   102 20 Jun 18:11 launchd-130.7TVN3n
AMIGA:barrywalker~> cat < /tmp/'Fixed description-Fixed numeric code-20121126.txt'
M16540035421004411262012N 11        SAMPLE WORD SAMPLE WORD
AMIGA:barrywalker~> _

EDIT:
Forgot about the file locking bit, research:-
Code:
man fuser


Last edited by wisecracker; 06-20-2014 at 04:51 PM..
# 3  
Old 06-20-2014
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:
Code:
read -r text < pathname

rather than:
Code:
text=$(cat < pathname
   or equivalently
text=$(cat pathname)

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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help required with a file rename shell script

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)
Discussion started by: tayyabq8
14 Replies

2. Shell Programming and Scripting

Command / script to partially rename file

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)
Discussion started by: Grueben
6 Replies

3. Shell Programming and Scripting

Batch Script To Unzip and Rename File

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)
Discussion started by: rajlakshmi
6 Replies

4. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

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)
Discussion started by: madhudeva
4 Replies

5. Shell Programming and Scripting

write shell script to rename file

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)
Discussion started by: shubhig15
7 Replies

6. Shell Programming and Scripting

Perl script to rename file,error

#!/usr/bin/perl $FL="ch.txt"; $CFL="mytext.txt"; print "This script will rename textfiles."; mv $FL $CFL; print "Done."; perl file.pl Error: Can't locate object method "mv" via package "ch.txt" (perhaps you forgot to load "ch.txt"?) at file.pl line 7. (5 Replies)
Discussion started by: cola
5 Replies

7. Shell Programming and Scripting

Script to rename zip-files with name of file

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)
Discussion started by: mark_a17
7 Replies

8. UNIX for Advanced & Expert Users

Rename a file to a file_current datetime in a shell script

Hi all, Could anyone suggest me on Renaming a file to a file_current datetime in a shell script. (3 Replies)
Discussion started by: Nithin
3 Replies

9. Shell Programming and Scripting

A script that will move a file to a directory with the same name and then rename that file

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)
Discussion started by: marcozd
7 Replies

10. Windows & DOS: Issues & Discussions

DOS script to grab the first file in a dir and rename it

: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)
Discussion started by: raghav525
2 Replies
Login or Register to Ask a Question