shell script question:How 2 assign several fields from a file to a variable?

shell script question:How 2 assign several fields from a file to a variable?

am 14.09.2005 07:35:31 von vick Julius

Hello everybody

I want to rename files with the new names from a text file, names.txt:

Brazilian coffee
Canadian maple
Korean car



i want to use the following script

for i in *.doc
do

for file in `cat names.txt`
do
mv $i $file....



I want to renmae all files (in *.doc) with the new names read from names.txt
file.

The problem is the names in the file names.txt have spaces...


Any idea?

Thanks

Vick

____________________________________________________________ _____
Express yourself instantly with MSN Messenger! Download today it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/ 01/

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: shell script question:How 2 assign several fields from a fileto a variable?

am 14.09.2005 08:08:50 von Rick von Richter

I'm not sure you have the right logic in your script. As I understand
it, you want to take each *.doc file and rename it to a filename from
names.txt. For example; you have a.doc, b.doc, and c.doc. The way your
script is written, a.doc will be renamed (moved) to "Korean car", then
b.doc will be renamed to "Korean.car", and so will c.doc. Your script
has two 'for' loops. The outer loop runs thru each file of *.doc, and
the inner loop cycles thru all of the entries in names.txt for EACH
*.doc file. So, for each *.doc file (outer loop) it will completely run
thru the inner loop effective renaming each file to every file name you
have in names.txt eventually ending up with "Korean car" as the last
rename before it moves to the next *.doc file. The effect of this will
be that every *.doc file will be renamed to "Korean car and the only
file you will have left in the directory will be the last *.doc file and
it will be called "Korean car". Hmmm.

Anyways, to answer your question... you need to use double quotes
around your input and output variables and commands. Here is and example:

for i in "`cat names.txt`"; do
printf "$i\n"
done

You could also use the 'echo' command instead or 'printf' but 'echo' is
outdated. Use printf.

HTH,
Rick

vick Julius wrote:

> Hello everybody
>
> I want to rename files with the new names from a text file, names.txt:
>
> Brazilian coffee
> Canadian maple
> Korean car
>
>
>
> i want to use the following script
>
> for i in *.doc
> do
>
> for file in `cat names.txt`
> do
> mv $i $file....
>
>
>
> I want to renmae all files (in *.doc) with the new names read from
> names.txt file.
>
> The problem is the names in the file names.txt have spaces...
>
>
> Any idea?
>
> Thanks
>
> Vick
>
> ____________________________________________________________ _____
> Express yourself instantly with MSN Messenger! Download today it's
> FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/ 01/
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-admin" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: shell script question:How 2 assign several fields from a file to a variable?

am 14.09.2005 08:14:08 von Jeff Woods

At 9/14/2005 05:35 +0000, vick Julius wrote:
>I want to rename files with the new names from a text file, names.txt:

>I want to renmae all files (in *.doc) with the new names read from
>names.txt file.
>The problem is the names in the file names.txt have spaces...

I created a small test environment:

$ ls -1
a.doc
b.doc
c.doc
names.txt
$ cat names.txt
first file
second file
third file
$ for pair in $(ls -1 *.doc |paste -d: - names.txt |tr ' ' '~')
> do cp -v "${pair%%:*}" "$(echo ${pair#*:} |tr '~' ' ')"
> done
`a.doc' -> `first file'
`b.doc' -> `second file'
`c.doc' -> `third file'
$ ls -1
a.doc
b.doc
c.doc
first file
names.txt
second file
third file
$

Change "cp" to "mv" to rename the files. Note that I've assumed none
of the files have ":" or "~"; if this isn't true, substitute other characters.

--
Jeff Woods

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: shell script question:How 2 assign several fields from a fileto a variable?

am 14.09.2005 08:41:04 von drupix

Hint: Use sed to remove spaces from names.txt first.

--Adrian.

vick Julius wrote:

> Hello everybody
>
> I want to rename files with the new names from a text file, names.txt:
>
> Brazilian coffee
> Canadian maple
> Korean car
>
>
>
> i want to use the following script
>
> for i in *.doc
> do
>
> for file in `cat names.txt`
> do
> mv $i $file....
>
>
>
> I want to renmae all files (in *.doc) with the new names read from
> names.txt file.
>
> The problem is the names in the file names.txt have spaces...
>
>
> Any idea?
>
> Thanks
>
> Vick
>
> ____________________________________________________________ _____
> Express yourself instantly with MSN Messenger! Download today it's
> FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/ 01/
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-admin" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: shell script question:How 2 assign several fields from a file to a variable?

am 14.09.2005 16:58:55 von Scott Taylor

vick Julius wrote:

> Hello everybody
>
> I want to rename files with the new names from a text file, names.txt:
>
> Brazilian coffee
> Canadian maple
> Korean car
>
> i want to use the following script

As Rick mentioned, you will end up with only the one file.

> I want to renmae all files (in *.doc) with the new names read from
> names.txt file.
>
> The problem is the names in the file names.txt have spaces...

The only way I can think of is you will need to use an array to hold the
file names, see 'man bash' search for Arrays. I think Perl would be more
like you friend here, or C if you can.

Another thing, spaces in *nix files just cause havoc; you would save
yourself a lot of future grief if you replaced the spaces with an
underscore "blah_blah" or something.

Maybe you can do something to this effect: while looping through the
names.txt file, do a 'ls *.doc' and mv the first file to newname.blah,
ignoring the rest. I don't know how you could do that, off hand, just a
thought, I still think an array would be easier. =P

--
Scott
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: shell script question:How 2 assign several fields from a file to a variable?

am 14.09.2005 17:36:30 von Freddie

here's my entry for "ugliest bash script that would do the job":
counter=1
ls -1A > file_list
while read line
do
newname=`sed -n $counter\p newnames.txt`
mv $line \"$newname\"
counter=$((counter++))
done < file_list
rm file_list

note: i didnt test this script - if youre gonna try it test it on some
dummy files first.
i would definitely also recommend stripping out the spaces. it just makes
life much easier. you can do this pretty easily by putting something like
tr \ _ (notice there are two spaces after the \) into the above script
when you create newname.



At 17:58 14.9.2005, you wrote:

>vick Julius wrote:
>
> > Hello everybody
> >
> > I want to rename files with the new names from a text file, names.txt:
> >
> > Brazilian coffee
> > Canadian maple
> > Korean car
> >
> > i want to use the following script
>
>As Rick mentioned, you will end up with only the one file.
>
> > I want to renmae all files (in *.doc) with the new names read from
> > names.txt file.
> >
> > The problem is the names in the file names.txt have spaces...
>
>The only way I can think of is you will need to use an array to hold the
>file names, see 'man bash' search for Arrays. I think Perl would be more
>like you friend here, or C if you can.
>
>Another thing, spaces in *nix files just cause havoc; you would save
>yourself a lot of future grief if you replaced the spaces with an
>underscore "blah_blah" or something.
>
>Maybe you can do something to this effect: while looping through the
>names.txt file, do a 'ls *.doc' and mv the first file to newname.blah,
>ignoring the rest. I don't know how you could do that, off hand, just a
>thought, I still think an array would be easier. =P
>
>--
>Scott
>-
>To unsubscribe from this list: send the line "unsubscribe linux-admin" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at http://vger.kernel.org/majordomo-info.html

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html