Sed or Awk usage to rename string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed or Awk usage to rename string
# 1  
Old 10-11-2010
Sed or Awk usage to rename string

I have gotten myself totally lost trying to sort this out and just need some help please..

I will have a multiple directories using the following naming convention with an undetermined number of files in each directory.

9780743582094_05of5_Accountable.wav

I need to batch rename all files grabbing the two digit number after the first underscore, and appending it to the end of the file name,with a preceding hyphen, before the extension.

The example: 9780743582094_05of5_Accountable.wav

Would become

9780743582094_05of5_Accountable-05.wav

I know sed or awk could work but I have not been using Linux/Unix for some time and really have burned out trying to remember this. Please show me the way with an explanation. I am using Ubuntu 10.04 and have the rename command available to me.

For those who are going to insist to see some effort on my part, sadly this is all I've got:

Code:
for i in `ls|grep -o '_[00-99].'|tr '_' '-'`; do echo $i;done

The reason i am doing this is to try and use a script that I found to batch convert wav files to .m4b audiobook format, but I first must convert to mp3. If anyone has a better suggestion to accomplish this, I would also love to hear about that. But first, I really need an answer to this sed issue because it is upsetting me.
# 2  
Old 10-11-2010
Using parameter expansion:
Code:
for i in *_*_*.wav
do
  t=${i#*_}
  mv "$i" "${i%.*}-${t%%of*}.${i##*.}"
done

# 3  
Old 10-11-2010
This works brilliantly, but please explain a bit.. Also why this is better than awk or sed..
# 4  
Old 10-11-2010
Hi it is faster than awk or sed since no external programs need to be called from the shell to do the parsing..

for i in *_*_*.wavfor every file in the directory with this pattern
t=${i#*_}remove the part before the first _ and put the remainder in variable t
mv "$i" "${i%.*}-${t%%of*}.${i##*.}"mv file "$i" to the part of $i upto the extension (the last dot), followed by a "-" and variable $t with the part "of" and further cut off (which leaves the sequence number), then a dot (.) and then the extension of variable $i (everything before the last dot cut off)
I hope this helps. It can look a bit cryptic at first, but it really isn't that complicated..

Since $i will always contain files ending in .wav, this is equivalent in this case:
Code:
for i in *_*_*.wav
do
  t=${i#*_}
  mv "$i" "${i%.wav}-${t%%of*}.wav"
done


Last edited by Scrutinizer; 10-18-2010 at 11:04 AM..
# 5  
Old 10-18-2010
I have begun reading about parameter expansion.. Thanks so much, this is incredibly helpful.
# 6  
Old 10-18-2010
Thanks! Good to hear. I noticed there was a mistake in the explanation and I made a correction..

S.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

sed awk to remove the , in a string

Dear All, Can anyone help to remove the , bewteen "" in a string by using sed or awk? e.g. input : 1,4,5,"abcdef","we,are,here",4,"help hep" output:1,4,5,"abcdef","wearehere",4,"help hep" Thanks, Mimi (5 Replies)
Discussion started by: mimilaw
5 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

Rename file using sed or awk

I have a filename like 1_DATE_3_4.5_888 and I want to modify the date field (ie the last 4 digits ) alone and remove the last field. Old filename:1_DATE_3_4.5_888 Given date (for eg):120606259532 modified date:120606259899 new filename:1_<modified date>_3.4.5 (14 Replies)
Discussion started by: sandy88
14 Replies

6. Shell Programming and Scripting

match string exactly with awk/sed

Hi all, I have a list that I would like to parse with awk/sed. The list is contains entries such as: JournalTitle: Biochemistry JournalTitle: Biochemistry and cell biology = Biochimie et biologie cellulaire JournalTitle: Biochemistry and experimental biology JournalTitle: Biochemistry and... (6 Replies)
Discussion started by: euval
6 Replies

7. Homework & Coursework Questions

String editing using sed? awk?

1. The problem statement, all variables and given/known data: Problem Statement for project: When an account is created on the CS Unix network, a public html directory is created in the account's home directory. A default web page is put into that directory. Some users replace or... (13 Replies)
Discussion started by: peage1475
13 Replies

8. Shell Programming and Scripting

usage...sed/awk/reg-exp ..in shell scripting

in shell scripting there is extensive usage of i> regular expression ii>sed iii>awk can anyone tell me the suitable contexts ...i mean which one is suitable for what kind of operation. like the reg-exp and sed seems to be doing the same job..i.e pattern matching (1 Reply)
Discussion started by: mobydick
1 Replies

9. Shell Programming and Scripting

awk/sed - getting string instead of number

Hi! I am writing a script handling downloading list of files and I have to check whether file is present locally and if not finished than continue downloading. To do so I have to compare sizes of remote file and local file. To check remote file size I have to parse something like this: ... (2 Replies)
Discussion started by: hrwath
2 Replies

10. UNIX for Dummies Questions & Answers

sed/awk String problem

I would appreciate it if any one can guide me in using awk perhaps sed in extracting some values from a long string. here is an example. .......some lines here........ ........ aaaa bbbb cccc ddddd eeeee fffff gggg (time:hhhh)........ ......some lines here also.......... How can I extract... (2 Replies)
Discussion started by: odogbolu98
2 Replies
Login or Register to Ask a Question