Adding an extension to a group of filenames | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Adding an extension to a group of filenames

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-09-2004
pepintheshort pepintheshort is offline
Registered User
 
Join Date: Jul 2003
Last Activity: 12 April 2004, 9:09 AM EDT
Location: Seattle
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Adding an extension to a group of filenames

Hi -
I'm stuck. I have a group of text files created using the split command. My files have the names "projectaa", "projectab", "projectac", etc. What I want to do is add the extension ".txt" to each file. I think I've got part of a sed command together, but I'm stuck on my regex - I keep getting an error "Illegal variable name".

Here's the regex I created:

ls -d * | sed "s/^project[a-z][a-z]$/mv '&' \.txt/" | sh -v

Can anybody help me fix this? Or, offer an alternative way of renaming the files? I don't want to use the mv command by itself because each time I use split on a new text file, I won't know how many new files will be created.

Thanks!
Sponsored Links
    #2  
Old 04-09-2004
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
 
Join Date: May 2001
Last Activity: 24 March 2008, 9:43 PM EDT
Location: Chicago IL, USA
Posts: 1,006
Thanks: 0
Thanked 2 Times in 1 Post
useing the ksh shell i would use a for loop.

for x in blah; do
cool stuff
done
Sponsored Links
    #3  
Old 04-09-2004
pepintheshort pepintheshort is offline
Registered User
 
Join Date: Jul 2003
Last Activity: 12 April 2004, 9:09 AM EDT
Location: Seattle
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
I'm using csh, primarily and the shell script I'm working on is just in sh.

Can you be more descriptive about the "cool stuff" line? That's really the core of my problem here.
    #4  
Old 04-09-2004
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
 
Join Date: May 2001
Last Activity: 24 March 2008, 9:43 PM EDT
Location: Chicago IL, USA
Posts: 1,006
Thanks: 0
Thanked 2 Times in 1 Post
cool stuff = useing the mv/cp command to move your file to a new name.
Sponsored Links
    #5  
Old 04-09-2004
pepintheshort pepintheshort is offline
Registered User
 
Join Date: Jul 2003
Last Activity: 12 April 2004, 9:09 AM EDT
Location: Seattle
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Optimus,

I appreciate the insight, but I think you're missing my point. My problem is with the regular expression I posted above. I need help matching the metacharacters.

If you had read my initial post, you'll also see that I am hesitant to use mv or cp to rename the files because (without the "for" loop) it will only mv one file at a time. Since I am a relative newcomer to creating a shell script, I need more information than just one brief phrase - details are extremely critical. So, if you think I should use a "for" loop, why not post a sample of the code - all of the code, not "do cool stuff here" - so that it helps fix my problem. Other new people might read this post too and possibly learn from it.

If you don't know the answer to my question, I would appreciate you not wasting my time, and letting someone else answer - even if its to point me to an FAQ where this type of question may have already been answered. (I did search the BBS briefly before posting and did not match what I was looking for.)
Sponsored Links
    #6  
Old 04-09-2004
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
 
Join Date: May 2001
Last Activity: 24 March 2008, 9:43 PM EDT
Location: Chicago IL, USA
Posts: 1,006
Thanks: 0
Thanked 2 Times in 1 Post
Quote:
Originally posted by pepintheshort
Optimus,

I appreciate the insight, but I think you're missing my point. My problem is with the regular expression I posted above. I need help matching the metacharacters.

If you had read my initial post, you'll also see that I am hesitant to use mv or cp to rename the files because (without the "for" loop) it will only mv one file at a time. Since I am a relative newcomer to creating a shell script, I need more information than just one brief phrase - details are extremely critical. So, if you think I should use a "for" loop, why not post a sample of the code - all of the code, not "do cool stuff here" - so that it helps fix my problem. Other new people might read this post too and possibly learn from it.

If you don't know the answer to my question, I would appreciate you not wasting my time, and letting someone else answer - even if its to point me to an FAQ where this type of question may have already been answered. (I did search the BBS briefly before posting and did not match what I was looking for.)

snip.. My files have the names "projectaa", "projectab", "projectac", etc. What I want to do is add the extension ".txt" to each file.../snip
These forums are to help you think of the answer. Not to do the work for you. WE DO NOT FIX YOUR PROBLEMS. WE HELP YOU FIX YOUR PROBLEMS. we are here to help you help yourself and in doing so you become a better coder/admin/hobbiest/l33t d00d

The concept of a forloop it to continue the loop for the # of arugments it is passed.

I am very capable of supplying this answer. I am chooseing to not supply the full answer but instead challenge you to come up with it on your own with some supportive outside assistance.

your sed command is completely unneeded for this request.
If you wrote the sed command why dont you explane to me exactly what you are doing with it.

if all you wanted to know was how to escape metachar then why didnt you just say that to begin with.
To escape metachar use a backslash.

also note in your orig post you did not say you are hesitent to use the move or copy command.

i gave you the framework. you need to work a bit harder at completeing the missing puzzle pieces instead of comming here to complane about not getting what you want when you wanted it.

Last edited by Optimus_P; 04-09-2004 at 03:38 PM..
Sponsored Links
    #7  
Old 04-09-2004
pepintheshort pepintheshort is offline
Registered User
 
Join Date: Jul 2003
Last Activity: 12 April 2004, 9:09 AM EDT
Location: Seattle
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Optimus -
I expected more maturity than this from people who use this board. The name of the forum I'm posting in is "Unix Newbies". I am new to Unix, and I have figured out a fair amount on my own. I am not asking you to fix my problems or do my work for me - if I do not find the answer on this board, there are plenty of other sites on the net I can use, as well as - shock! - books I can read. The answer will appear eventually. I thought I was being very confident and trusting by posting the code I had already figured out without anyone else's help. Sort of a meet you halfway there proposition.

But let me just say that both your posts are relatively useless to me. You tell me something about Korn shell - I counter and tell you I'm using C Shell. Being new, I can't distinguish yet between those two shells and what differences there are. I tell you I don't want to use the mv command by itself, and you proceed to give me the definition of the mv command. Well, of course I know that it renames files as well as moves them - that's why I included a variation of it in my regex. The regex I've built is supposed to do the same thing your forloop does. (There's more than one way to do things - Larry Wall, right?) But neither of your posts point out where my error is - you just tell me I don't know how to think yet. I would have to counter and say you don't know the answer, don't care to answer and are in general a lazy poster who thinks he's too cool to forego his ego for a minute and genuinely help. Are you some kind of whiz kid who knew everything from the start and never had to rely on help from anyone else ever? Should I nominate you for sainthood, because that would be a miracle. I for one know I need help, and that is why I wanted to ask someone else. A little push in the right direction will do wonders.

Alright, enough of this ranting. I will try to explain this one more time, and see what happens.

I have one large text file with, say, 8000 lines. I have split this file into 8 new text files with 1000 lines in each. The text files, generated from this split command:

split -1000 thelist.txt project

result in files named "projectaa", "projectab", etc. I want their names to be "projectaa.txt", "projectab.txt", etc. Why I want to make this change is not important to you. Accept that this is what I want and move on. So, knowing that, I have decided that I could use filename expansion and try to add the extension on the name using sed and a substitute command "s/". I know that a simplified substitution looks like s/oldtext/newtext/. I also know that by adding the mv command here: s/oldtext/mv newtext/ almost works. It works better when I add the '&' to it, which automatically uses the pattern I've tried to match in the beginning. Hence: s/oldtext/mv '&' / Finally, I try adding my text to be changed, by escaping the period in the command, because it also has special metacharacter meanings. Now my sed command looks like s/oldtext/mv '&' \.txt/

So, in a nutshell, that's what I'm thinking. I added the ls -d* command first to list all files in the folder and pipe those to this sed command. I then piped that result to "sh" so that the shell would execute the command for every item in the folder. This is the equivalent of your forloop. The -v on the end just lets me see a verbose method so I can verify its working correctly.

Given all this information, I might not know if I placed a space incorrectly in the sed command, or misquoted or didn't escape something properly. That's what I wanted help with. Some way to make a regex that correctly identifies all files with the name "project" and two miscellaneous characters afterwards. I thought that /^project[a-z][a-z]$/ pretty well isolated it. But since its not working, I'm not sure what to try.

I'm not complaining that this isn't working. I wouldn't spend all day working out variations and doing research so I could complain here. I've got better things to do with my time and so do you.

So be the bigger man and post a relevant clue about my specific sed command and benefit all the other newbies who want an example of how to do the same thing. If you know the answer, put up or shut up.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
change filenames but not extension erinbot Shell Programming and Scripting 2 02-06-2012 05:07 AM
Adding a Volume Group to an HACMP Resource Group? aixromeo AIX 6 06-30-2011 08:29 AM
Help with adding to tar filenames Swinton Shell Programming and Scripting 2 12-16-2009 11:48 AM
removing the extension from all filenames in a folder johnmcclintock UNIX for Dummies Questions & Answers 5 05-21-2008 08:23 AM
Reading filenames with extension .xml bhalotias Shell Programming and Scripting 3 01-21-2008 11:02 AM



All times are GMT -4. The time now is 09:33 PM.