Monday, April 10, 2017

Bash - Scripting - part 2- replace part of string


  1 #!/bin/bash
  2 #
  3 # Change file extensions
  4 #
  5 FILES=/homeb/d54712/sandbox/scripts/data.d/tmp/*
  6
  7 # ${file/%.pcx/.jpg}
  8
  9 CONVERT="$(ls $FILES)"

 10 TEMP=$(mktemp)

 11 echo "$CONVERT" > $TEMP
 12
 13
 14 while read file
 15 do
 16         echo ${file/%.pcx/.jpg}

 17 done < $TEMP
 18
 19

Results:

# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135724.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135731.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135732.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135733.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135734.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135735.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135736.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135737.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135739.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135752.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135753.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135754.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135755.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135756.pcx

# Convert extensions in temp file ($TEMP)


$ ./file_convert1.sh
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135724.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135731.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135732.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135733.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135734.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135735.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135736.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135737.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135739.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135752.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135753.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135754.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135755.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135756.jpg

No comments:

Post a Comment