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

Re: Editing Problem with bash script and sed



Hi,

i see at least one problem in Trim_Line.sh :
It uses "$1" as shell parser input and as sed regular expression.

With "S.*-" as "$1", the meaning differs in both contexts.
The shell parser input of

  for filename in *$1*

will look for files with a text snippet "S.".
The expression in 

  s/$1//

will not insist in seeing the "." after "S". It rather understands the "."
as instruction to match any character.

Looking at File.txt, i'd say "St.*-" could match the "St. John's College
Choir, Cambridge -" files in both interpretations.


In general, sed goes better with grep than with the shell parser.
Try

  ls -d * | grep "$1" | while read filename
  do
     ...
                  n=$(echo "$fname" | sed -e s/"$1"//)

Regrettably i found no way to make this safe against newlines in filenames.
I'd do it in C language if such names were to be expected.


Have a nice day :)

Thomas


Reply to: