Hi Inch
Well this turned out to be more interesting than I originally thought it would be: -
When I got your file I found it would not behave with awk or sed or any of the usual utilities so I took a look at its internal structure by doing an octal dump of the contents.
Here is the part containing your start string with the individual letters highlighted in bold: -
TX5XN:/home/brad/forum/inch>od -c FP00000.SHD | pg
0003440 H & 375 032 001 002 \0 \0 b \0 v \0 i \0 w \0
0003460 a \0 c \0 s \0 \0 \0 b \0 v \0 i \0 w \0
0003500 a \0 c \0 s \0 \0 \0 s \0 m \0 u \0 A \0
As you can see each letter is delimited by a number 0 or null, so anything like awk or sed will fail as they look for string based files, not null delimited chars.
This is why your original paste of the file contained so many unprintable characters and spaces between each letter of your target string.
Having established the problem it is now a simple fix, just remove the nulls prior to manipulating the strings: -
TX5XN:/home/brad/forum/inch>tr -d "\000" < FP00000.SHD | strings
Adobe PDF
PRIV
EBDA
Standard
bviwacsbviwacssmuAB462691_200908148 19 27 296 - BerichtAdobe PDFAdobe PDF ConverterWinPrintNT EMF 1.008\\PC267IWACS
So adding the stripping of the nulls to the tool gives us the ability to correctly process the string we want to turn into a file name: -
TX5XN:/home/brad/forum/inch>inch
TX5XN:/home/brad/forum/inch>ls -l
total 168
-rw-r--r-- 1 brad root 2080 2009-08-14 20:10 FP00000.SHD
-rw-r--r-- 1 brad root 75368 2009-10-08 11:46 FP00000.SPL
-rwxrwxrwx 1 brad root 696 2009-10-09 17:48 inch
-rw-r--r-- 1 brad root 75368 2009-10-09 17:48 smuAB462691_2009081481927296-Berich.SPL
Here is the modified code: -
ls *.SHD | while read FILE_NAME
do
if [[ -w ${FILE_NAME%.*}.SPL ]] ## Ignore any file with no .SPL file
then
NEW_FILE_NAME=$( tr -d "\000" < $FILE_NAME | strings | nawk ' BEGIN {
## Spaces in target strings ????
first = "viwacsbviwacs"
last = "Adobe"
}
( $0 ~ first ) && ( $0 ~ last ) {
startm = index( $0, first ) + length( first )
endm = index( $0, last ) - 1
file_name = substr( $0, startm, endm - startm )
} END {
## Remove spaces from new file name
gsub( / /, "", file_name )
print file_name
} ' ).SPL
## Copy. change cp to mv if required
cp ${FILE_NAME%.*}.SPL $NEW_FILE_NAME
else
echo "File ${FILE_NAME} has no matching .SPL file"
fi
done
Note there is no real error checking for existing files etc, I will leave you to add that yourself.
You should also note it needs to be run in the target directory so you will need to modify it if you want to handle multiple directories etc.
Until you add this kind of validation I would copy the files into a work directory and process them there first to avoid any unfortunate mishaps or lost data.
Hope it is usefull.........