Ok, this was probably a poorly worded title, but I wasn't quite sure how to write it. What I'm trying to do is go through a large list of files (without extensions) and determine what they are, then add an appropriate extension.
I discovered a really handy utility for linux called 'file' to help out with this. I know python syntax much better than bash & awk, but I had a lot of issues getting the 3rd party "python-magic" module working on my machine so instead of wasting time with that I spent my time trying to write it in bash/awk.
I believe I'm really close, but something is still off with my syntax and i'm not sure what.
Here is the code:
for i in *;
do file $i | awk '{
switch ($2) {
case $2 == 'TIFF':
mv $i "$i.tif"
case $2 == 'PDF':
mv $i "$i.pdf"
case $2 == 'ASCII':
mv $i "$i.txt"
case $2 == 'Rich':
mv $i "$i.rtf"
case $2 == 'gzip':
mv $i "$i.gz"
}
}';
done
The syntax errors i'm getting on some test txt files are:
awk: cmd. line:3: case $2 == TIFF:
awk: cmd. line:3: ^ syntax error
awk: cmd. line:3: case $2 == TIFF:
awk: cmd. line:3: ^ syntax error
awk: cmd. line:3: case $2 == TIFF:
awk: cmd. line:3: ^ syntax error
awk: cmd. line:3: case $2 == TIFF:
awk: cmd. line:3: ^ syntax error
awk: cmd. line:3: case $2 == TIFF:
awk: cmd. line:3: ^ syntax error
awk: cmd. line:3: case $2 == TIFF:
awk: cmd. line:3: ^ syntax error
awk: cmd. line:3: case $2 == TIFF:
awk: cmd. line:3: ^ syntax error
awk: cmd. line:3: case $2 == TIFF:
awk: cmd. line:3: ^ syntax error
awk: cmd. line:3: case $2 == TIFF:
awk: cmd. line:3: ^ syntax error
awk: cmd. line:3: case $2 == TIFF:
awk: cmd. line:3: ^ syntax error
I saw some other interesting methods of doing file renames with awk and sed, but at least in my mind, using in combination with 'file', this seems like the best approach for my skill level.
If anyone could help me troubleshoot these awk syntax errors, that would be awesome. Or, in a more generic help sense, if you know of a better method of accomplishing this task, please share :)
EDIT:
I made the changes suggested (fixed quotes, removed comparison operator from each case, added breaks). Now the script runs without error, but none of the files are changed. They are all still extensionless. My test files are all ASCII and so should be renamed to have '.txt' appended to the end.
To test and verify i'm pulling the right field with awk I ran this little test:
$ file test2 | awk '{printf $2}'
ASCII
So $2 is the file type. Any ideas on why it's not working?
No need for awk here:
for i in *; do
filetype=$(file $i)
case $filetype in
*TIFF*) ext="tif"
;;
*PDF*) ext="pdf"
;;
*ASCII*) ext="txt"
;;
*Rich*) ext="rtf"
;;
*gzip*) ext="gz"
;;
esac
echo mv "$i" "$i.$ext"
done
When the output looks like a safe set of commands to run, you can remove the echo to actually execute the mv commands.
Since your awk script is enclosed in single quotes, use double quotes inside awk for literal strings.
So
case $2 == 'TIFF'
should be replaced with:
case "TIFF"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With