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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
-
: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

[Index of Archives]     [Linux Newbie]     [Audio]     [Hams]     [Kernel Newbies]     [Util Linux NG]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Device Drivers]     [Samba]     [Video 4 Linux]     [Git]     [Fedora Users]

  Powered by Linux