The problem is editing long file names to shorten them. An example group
of file names is attached.
The bash script copied from BashScripting is attached. This script works
perfectly with simple deletions, for example TrimLine.sh "College" ""
will remove College from each line in File.txt.
After experimenting with regular expressions with sed I found ls | sed
-e s/S.*-// reduced the file names in File.txt to just the names of the
Carols as shown in sed.txt. Used like this sed leaves the original file
unchanged.
Trim_Line.sh "S.*-" "" fails to actually change the file names. Assuming
Trim_Line.sh does not accept the regex as a pattern, I edited it to
replace the $1 in the sed command with the regex expression. This does
not work, the original file is left unchanged. I also tried replacing
$1 with ^.*- which also works with the test ls | sed -e s/^.*-// but
still the file names are unchanged.
I would appreciate any help or comments.
Tom George
Script started on Tue 14 Nov 2017 10:10:03 AM EST
[1;32mDragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite
Carols$[m ls
[0m[01;32mEdit_File_Names.sh[0m
File.txt
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 01 - Ding, Dong! Merrily On High.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 02 - O Little Town Of Bethlehem.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 03 - Born On Earth.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 04 - The Twelve Days Of Christmas.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 05 - Up Good Christen Folk.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 06 - Silent Night.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 07 - Good King Wenceslas.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 08 - While Shepherds Watched.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 09 - God Rest You Merry, Gentlemen.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 10 - The Holly And The Ivy.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 11 - Away In A Manger.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 12 - Shepherd's Pipe Carol.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 13 - The First Nowell.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 14 - I Saw Three Ships.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 15 - Suo Gân.ogg[0m
[00;36mSt. John's College Choir, Cambridge - [1988 421 022-2 10DC] Christmas
Weekend (16 Favourite Carols) - 16 - Hark! The Herald Angels Sing.ogg[0m
[01;34mtemp[0m
[01;32mtest.sh[0m
[01;32mtrial.sh[0m
[01;32mTrim_Line.sh[0m
[1;32mDragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite
Carols$[m exit
Script done on Tue 14 Nov 2017 10:10:14 AM EST
Script started on Tue 14 Nov 2017 10:14:04 AM EST
[1;32mDragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite
Carols$[m less Trim_Line.sh
#! /bin/bash
#
# This is a hacked version of Bashscripting Example A-2
# If the arguements contain spaces they should be in double quotes
# As written below the second arguement is not used, the first is replaced
# by nothing
ARGS=2
E_BADARGS=65
ONE=1
if [ $# -ne "$ARGS" ]
then
echo "Usage: `basename $0` old-pattern new-pattern"
exit $E_BADARGS
fi
number=0
for filename in *$1*
do
if [ -f "$filename" ] # Tests to be sure this is a
regular file
then
fname="$filename"
echo $fname
n=`echo $fname | sed -e s/$1//`
mv "$fname" "$n" # Note the `command` makes
the output of
let 'number += 1' # the command available to
be assigned
fi # to a variable.
done
if [ "$number" -eq "$ONE" ]
then
echo "$number file renamed"
else
echo "$number files renamed"
fi
exit
[7mTrim_Line.sh (END)[27m[K
[K[1;32mDragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite
Carols$[m exit
Script done on Tue 14 Nov 2017 10:14:34 AM EST
Script started on Tue 14 Nov 2017 10:16:26 AM EST
[1;32mDragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite
Carols$[m ls | sed -e s/S.*-//
Edit_File_Names.sh
File.txt
sed.txt
Ding, Dong! Merrily On High.ogg
O Little Town Of Bethlehem.ogg
Born On Earth.ogg
The Twelve Days Of Christmas.ogg
Up Good Christen Folk.ogg
Silent Night.ogg
Good King Wenceslas.ogg
While Shepherds Watched.ogg
God Rest You Merry, Gentlemen.ogg
The Holly And The Ivy.ogg
Away In A Manger.ogg
Shepherd's Pipe Carol.ogg
The First Nowell.ogg
I Saw Three Ships.ogg
Suo Gân.ogg
Hark! The Herald Angels Sing.ogg
temp
test.sh
trial.sh
Trim_Line.sh
Trim_Line.txt
[1;32mDragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite
Carols$[m exit
Script done on Tue 14 Nov 2017 10:17:40 AM EST