renaming files based on first line
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix questions > renaming files based on first line




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    renaming files based on first line  
bmdavll@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-07-06 12:49 PM

Hi, I have a set of indiscriminately named files (but they all fit a
pattern, say xxx*), and I'm looking for a series of commands that will
look at the first lines from those files (say they all start with the
line -------some-string) and rename each of those files to
some-string.txt. I know how piping and redirection work but I'm new to
Unix commands and utils so any help would be great.






[ Post a follow-up to this message ]



    Re: renaming files based on first line  
Ed Morton


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-07-06 10:57 PM

bmdavll@gmail.com wrote:
> Hi, I have a set of indiscriminately named files (but they all fit a
> pattern, say xxx*), and I'm looking for a series of commands that will
> look at the first lines from those files (say they all start with the
> line -------some-string) and rename each of those files to
> some-string.txt. I know how piping and redirection work but I'm new to
> Unix commands and utils so any help would be great.
>

If there's no more than one consecutive "-" in your file names, and
always 2 or more in the preceeding text:

read name < file && mv file "${name##*--}.txt"

Regards,

Ed.





[ Post a follow-up to this message ]



    Re: renaming files based on first line  
javibarroso@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-09-06 01:51 AM

Only you have to find the pipe for get your name

name=$(head -1 "$f" | .... } where ... is a good grep or sed or awk
command for you

# for f in *
do
name=$(head -1 "$f" | sed 's/^-----//')
mv "$f" $name.txt
done






[ Post a follow-up to this message ]



    Re: renaming files based on first line  
bmdavll@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-09-06 07:56 AM

That works great for a single file, but how do I do it for all the
files matching a pattern, say xxx*? And what does "##*--" do to the
name variable?

Thanks,
David






[ Post a follow-up to this message ]



    Re: renaming files based on first line  
bmdavll@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-09-06 07:56 AM

That works great for a single file, but how do I do it for all files
matching a pattern. Do I need a for loop? And what does "##*--" do
exactly to the name variable to remove the leading dashes?

Thanks,
David






[ Post a follow-up to this message ]



    Re: renaming files based on first line  
Ed Morton


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-09-06 11:02 PM

bmdavll@gmail.com wrote:
> That works great for a single file, but how do I do it for all files
> matching a pattern. Do I need a for loop? And what does "##*--" do
> exactly to the name variable to remove the leading dashes?
>
> Thanks,
> David
>

Please read http://cfaj.freeshell.org/google before posting again as
you're falling prey to google.

Wrt your questions. If you're responding to this posting of mine:

> If there's no more than one consecutive "-" in your file names, and always
 2 or more in the preceeding text:
>
> read name < file && mv file "${name##*--}.txt"

then, yes, you need to put it in a loop for multiple files, e.g.

for file in *
do
read name < "$file" && mv "$file" "${name##*--}.txt"
done

and the "##*--" removes every character in $name up to and including the
last 2 consectutive dashes. Look for "parameter substitution" in your
shells man page.

Ed.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:12 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register