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

Editing Problem with bash script and sed



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
Dragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite Carols$ ls
Edit_File_Names.sh
File.txt
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 01 - Ding, Dong! Merrily On High.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 02 - O Little Town Of Bethlehem.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 03 - Born On Earth.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 04 - The Twelve Days Of Christmas.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 05 - Up Good Christen Folk.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 06 - Silent Night.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 07 - Good King Wenceslas.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 08 - While Shepherds Watched.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 09 - God Rest You Merry, Gentlemen.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 10 - The Holly And The Ivy.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 11 - Away In A Manger.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 12 - Shepherd's Pipe Carol.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 13 - The First Nowell.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 14 - I Saw Three Ships.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 15 - Suo Gân.ogg
St. John's College Choir, Cambridge - [1988  421 022-2 10DC] Christmas Weekend (16 Favourite Carols) - 16 - Hark! The Herald Angels Sing.ogg
temp
test.sh
trial.sh
Trim_Line.sh
Dragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite Carols$ 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
Dragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite Carols$ 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







Trim_Line.sh (END)
Dragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite Carols$ 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
Dragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite Carols$ 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
Dragonette@tom:~/Music/Christmas/Carols/Christmas Weekend 16 Favorite Carols$ exit

Script done on Tue 14 Nov 2017 10:17:40 AM EST

Reply to: