LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash/korn Script Rename files to Same name one at a time (https://www.linuxquestions.org/questions/programming-9/bash-korn-script-rename-files-to-same-name-one-at-a-time-735070/)

kernelzack 06-23-2009 03:05 PM

Bash/korn Script Rename files to Same name one at a time
 
Hello all,

I have a question I hope someone can help me with, I have scowered the internet for the answer to this one. I need a script to rename multiple files to the same exact name, run a program on the file then do the same for the next file. We have a unix backend system that is expecting to load the file with the filename of cards.in
So I will have files named card.2009xxx, like i said i will have around 4 or five of those. I want the script to rename card.2009xxx to cards.in, run our unix program on cards.in which inturn changes the file name and once complete i want the script to rename the next cards.2009 to "cards.in"
Until there are no more left in the directory and the unix program has processed all the files. All of this is occurring in the same directory. I have written some scripts but they fail by moving for example cards.200901 to cards.in then immediately moving cards.200902 to cards.in and that is not good because it is overwriting valuable data. Any Help at all with this one is GREATLY appreciated. Thanks in advance.

Tha

GrapefruiTgirl 06-23-2009 03:39 PM

Quote:

All of this is occurring in the same directory. I have written some scripts but they fail by moving for example cards.200901 to cards.in then immediately moving cards.200902 to cards.in and that is not good because it is overwriting valuable data.
Exactly.

Please clarify about this: if all of these files ARE in the same directory, then each successive recursion of this process will overwrite the results of the last recursion.

EDIT:

I get it: the UNIX program will change the name from cards.in to some other name; THEN the process repeats.

what you need is basically:

1 ) rename the file to cards.in
2 ) call the UNIX program to edit the file and rename it something else.
3 ) Find the next file, repeat step 1.

right?


Sasha

kernelzack 06-23-2009 03:46 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 3583774)
Exactly.

Please clarify about this: if all of these files ARE in the same directory, then each successive recursion of this process will overwrite the results of the last recursion.

May I suggest, you perhaps want to `cat` or otherwise grab the contents of all these xxx2009xxx files, and MERGE their contents into one file?

Thanks for any clarification.

Sasha

First Thank you for your response. Cating the files is unfortunatley not an option because as the unix program runs it references the header date in each file and needs to record it so if we cat them it will only pick up one header. Here is a example of what the directory will look like
kernelzack$ ls
cards.200901
cards.200902
cards.200903
What I want to do is have my script change cards.200901 into cards.in then call the unix program. Once the unix program is called it will process the contents of cards.in and change the file name too cards.in200901 As soon as that process completes i would like the script to move cards.200902 to cards.in and call the unix program, and so on until the unix program has been run on all three files. So is there a way to do that with a loop of some sort? Thanks again.

kernelzack 06-23-2009 03:50 PM

You got it!

GrapefruiTgirl 06-23-2009 04:49 PM

Yep, a loop, and a simple one at that.

There are surely lots of ways to go about this; I'll point you towards one way:

Code:


#!/bin/bash

filenames=`ls`

for filename in "`echo -e "$filenames" | gawk '/^cards.[:digit:]+*/{print}'`"; do

mv -f $filename cards.in

<call your UNIX thing here>

done;

NOTE: No warranty comes with this code. You may want to test it on some phony files first because it doesn't do any really extreme sanity checks, AND it keeps re-writing the cards.in file with each loop. But it works over on my end here.

ALWAYS have a backup!

Cheers,
Sasha

GrapefruiTgirl 06-23-2009 04:55 PM

update - it isn't working right.. play with it a bit. when/if I fix it, I'll post a fix.

kernelzack 06-24-2009 12:37 AM

Quote:

Originally Posted by GrapefruiTgirl (Post 3583838)
update - it isn't working right.. play with it a bit. when/if I fix it, I'll post a fix.


No worries, I really appreciate you working on this with me. I will keep playing around with the code and if I can get it to work I will post what was done. Thanks!

H_TeXMeX_H 06-24-2009 03:59 AM

Possibly better option, adapted from GrapefruiTgirl's script:

Code:

#!/bin/sh

for filename in cards.??????
do

mv -f $filename cards.in

<call your UNIX thing here>

done


GrapefruiTgirl 06-24-2009 07:29 AM

@ H_TeXMeX_H,

THANK YOU!

For whatever reason, I kept picking up the newline in the list of filenames and was having a ridiculous time with that simple Gawk.

@ the OP - has this modification helped, e.i. is it working now?

Cheers,
Sasha

kernelzack 06-24-2009 09:35 AM

Thank you both!!, I really appreciate all the help. During my first round of tests (calling a test program) it seems to work great. I will run our actual program and let you know how it goes, after testing I see no reason why it wouldn't work. I will update you either way, thanks again!!

H_TeXMeX_H 06-24-2009 09:48 AM

Also, depending on your circumstances you can change:

for filename in cards.??????

to

for filename in cards.2009*

The '?' matches any one character, the '*' matches any number of characters, so you decide what is appropriate.


All times are GMT -5. The time now is 12:26 AM.