Unix questions - renaming files based on first line

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > January 2006 > renaming files based on first line





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author renaming files based on first line
bmdavll@gmail.com

2006-01-07, 7:49 am

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.

Ed Morton

2006-01-07, 5: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.
javibarroso@gmail.com

2006-01-08, 8:51 pm

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

bmdavll@gmail.com

2006-01-09, 2: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

bmdavll@gmail.com

2006-01-09, 2: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

Ed Morton

2006-01-09, 6: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.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com