LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-23-2009, 03:05 PM   #1
kernelzack
LQ Newbie
 
Registered: Jun 2009
Distribution: Centos
Posts: 25

Rep: Reputation: 15
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
 
Old 06-23-2009, 03:39 PM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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

Last edited by GrapefruiTgirl; 06-23-2009 at 03:42 PM. Reason: Oops, now I get it.
 
Old 06-23-2009, 03:46 PM   #3
kernelzack
LQ Newbie
 
Registered: Jun 2009
Distribution: Centos
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by GrapefruiTgirl View Post
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.
 
Old 06-23-2009, 03:50 PM   #4
kernelzack
LQ Newbie
 
Registered: Jun 2009
Distribution: Centos
Posts: 25

Original Poster
Rep: Reputation: 15
You got it!
 
Old 06-23-2009, 04:49 PM   #5
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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
 
Old 06-23-2009, 04:55 PM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

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

Last edited by GrapefruiTgirl; 06-23-2009 at 05:03 PM.
 
Old 06-24-2009, 12:37 AM   #7
kernelzack
LQ Newbie
 
Registered: Jun 2009
Distribution: Centos
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by GrapefruiTgirl View Post
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!
 
Old 06-24-2009, 03:59 AM   #8
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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
 
Old 06-24-2009, 07:29 AM   #9
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
@ 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
 
Old 06-24-2009, 09:35 AM   #10
kernelzack
LQ Newbie
 
Registered: Jun 2009
Distribution: Centos
Posts: 25

Original Poster
Rep: Reputation: 15
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!!
 
Old 06-24-2009, 09:48 AM   #11
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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.
 
  


Reply

Tags
scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script? how to rename files in random way? lefty.crupps Linux - Software 15 06-08-2014 03:47 PM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
bash script to rename files removing one character Byenary Linux - Newbie 2 04-08-2008 10:12 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM


All times are GMT -5. The time now is 03:24 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration