Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 12-03-2010, 07:27 PM   #1
Senior Member
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,849
Blog Entries: 36

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
batch processing with awk

I want to use awk to rename files to include their sfv hash.

For instance I have a file named hello.wmv and its sfv hash is 01234567. I want to automatically rename the file to hello[01234567].wmv.

I have many media files so manually doing it would be a pain. I could do this in Bash easily enough but I want to learn more about awk. Here's what I have so far.

Contents of myfile.sfv...
; Generated by WIN-SFV32 v1.1a (pure-sfv v0.3) on 2010-12-03 at 20:16.55
;      5174231  08:32.20 2009-04-01 2594917.mp3
;      7283974  08:22.44 2009-04-01 ending.avi
;     12397720  08:33.41 2009-04-01 ending.mkv
;      6786396  11:44.51 2009-04-01 gimp_quick-mask[H.264_MP3][1360x768].avi
;     66445728  13:35.35 2009-05-03 gimp_render-creation[H.264_MP3][1360x768].avi
;     63556699  14:00.27 2009-08-05 gimp_render-creation[H.264_MP3][1360x768].zip
;     11344014  14:19.25 2009-04-01 ichigo_sig_creation[H.264][1360x768].avi
;     10734810  14:00.39 2009-08-05 ichigo_sig_creation[H.264][1360x768].zip
2594917.mp3 076BF90A
ending.avi E9F3F6E4
ending.mkv 6FCD0331
gimp_quick-mask[H.264_MP3][1360x768].avi 8D38A177
gimp_render-creation[H.264_MP3][1360x768].avi 6FFA7FBE
gimp_render-creation[H.264_MP3][1360x768].zip 9085E8B7
ichigo_sig_creation[H.264][1360x768].avi BF19F4BC
ichigo_sig_creation[H.264][1360x768].zip 24C63B2E
So far I am parsing the file like this:
cat myfile.sfv | grep -v '^;' | awk '{print $1}'
So the file names successfully get printed. I want to substitute it using a regular expression (e.g. 's/(\.[a-z]{3})$/\[$2\]\1/' where $2 is the hash from awk).

I looked up "regex awk substitute" and found the sub() command in awk. But I can't seem to make it work.

I want awk to be able to execute:
mv oldfilename.ext oldfilename[hash].ext
Any help would be appreciated.

Last edited by sag47; 12-03-2010 at 07:29 PM.
Old 12-04-2010, 07:29 AM   #2
Registered: Feb 2007
Location: VA, USA
Distribution: CentOS, Fedora
Posts: 53

Rep: Reputation: 5
The way I would do this is to use awk to generate the new filename and then just use the mv command to rename the files. If you post what code you have maybe we can see what is happening with the sub() command.
Old 12-04-2010, 07:57 AM   #3
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
To execute an external command in awk, you can simply print it down to a pipeline and pass it to the shell, example:
print "some command" | "/bin/bash"
in this way the printed command is passed to the shell and executed. It is a good habit to close the pipe after that, using
More details in the GNU awk manual:


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch Processing ankan Linux - Software 3 10-11-2013 10:52 AM
[SOLVED] Batch processing images. JamesGT Slackware 12 09-13-2010 07:16 PM
script help for batch processing mma8x Linux - General 5 12-08-2006 01:31 AM
Image batch processing cvzyl Linux - Software 3 03-20-2006 02:45 AM
how to do batch processing with time out? Franziss Programming 4 11-30-2005 10:17 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:30 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration