LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 12-03-2010, 08:27 PM   #1
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,505
Blog Entries: 35

Rep: Reputation: 383Reputation: 383Reputation: 383Reputation: 383
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...
Code:
; 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:
Code:
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:
Code:
mv oldfilename.ext oldfilename[hash].ext
Any help would be appreciated.

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

Rep: Reputation: 4
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, 08:57 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
To execute an external command in awk, you can simply print it down to a pipeline and pass it to the shell, example:
Code:
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
Code:
close("/bin/bash")
More details in the GNU awk manual: http://www.gnu.org/manual/gawk/gawk.html.
 
  


Reply


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 11:52 AM
[SOLVED] Batch processing images. JamesGT Slackware 12 09-13-2010 08:16 PM
script help for batch processing mma8x Linux - General 5 12-08-2006 02:31 AM
Image batch processing cvzyl Linux - Software 3 03-20-2006 03:45 AM
how to do batch processing with time out? Franziss Programming 4 11-30-2005 11:17 PM


All times are GMT -5. The time now is 10:24 AM.

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