LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-20-2011, 11:09 AM   #1
TommySprat
LQ Newbie
 
Registered: May 2009
Posts: 7

Rep: Reputation: 0
Renaming files with sed


Today i am trying to learn how to use sed. I set up a testing folder with the following files:
AAb.lol
AAc.lol
AAx.lol
test.sh

My goal is to create a script (test.sh) which renames all the files to their original name without AA. I want to end up with this:
b.lol
c.lol
x.lol
test.sh

sed seemed to be the perfect tool so i went ahead and created a script which i think should clear the job.
Code:
#!/bin/bash
for i in $( ls ); do
    NewName='sed' 's/AA//g' '$i'
    mv '$i' $NewName
done
The output however is a lot of times this:
Code:
./test.sh: line 3: s/AA//g: No such file or directory
mv: missing destination file operand after `$i'
From that 2nd line i can tell that $NewName is just empty. I also read something about sed needing the -e option for scripting purposes but i just don't understand it.

Last edited by TommySprat; 03-21-2011 at 01:56 PM. Reason: Solved
 
Old 03-20-2011, 11:23 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,561
Blog Entries: 1

Rep: Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023
Try the following:
Code:
#!/bin/bash

for i in *.lol
do
        mv $i $(echo $i | sed "s/AA//")

done
 
Old 03-20-2011, 11:56 AM   #3
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
Hi TommySprat,

Try this!!
Code:
for file in AA*; do
  mv "$file" "${file#AA}"
done

Last edited by anishkumarv; 03-20-2011 at 11:57 AM. Reason: forget to put code tag
 
Old 03-20-2011, 01:34 PM   #4
TommySprat
LQ Newbie
 
Registered: May 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks for the replies. I tried both methods and they both worked. The problem is solved but i still have some questions.
First about sycamorex's method. I thought that a dollar sign in front of a word was the instruction for bash to substitute it with the value of the variable. I see you use it on something that's not a variable, can anyone explain the need and function of the dollar sign there?
A related question i have is what the function of the parentheses is.

My first question for anishkumarv is the same as my first question because he also uses the dollar sign on a place i don't understand. Secondly i would like to know what the curly brackets do as well as the pound sign. I tried looking this all up myself of course but it's either not in there or i don't have enough basic knowledge about bash to grasp it. Although the problem is "solved" i would appreciate more answers because I would love to be able to come up with this myself next time.
 
Old 03-20-2011, 03:10 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,561
Blog Entries: 1

Rep: Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023
From the Bash manual:
Quote:
Command Substitution
Command substitution allows the output of a command to replace the command name. There are two forms:

$(command)
or
`command`

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the
command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word
splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed
by $, `, or \. The first backquote not preceded by a backslash terminates the command substitution. When using the
$(command) form, all characters between the parentheses make up the command; none are treated specially.

Command substitutions may be nested. To nest when using the backquoted form, escape the inner backquotes with back‐
slashes.

If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results

The answer to the second question can also be found in the bash manual under the parameter expansion heading. It makes a very interesting read.

Last edited by sycamorex; 03-20-2011 at 03:11 PM.
 
Old 03-20-2011, 03:18 PM   #6
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
And for the simplest rename:

Code:
rename AA "" *
 
Old 03-20-2011, 07:54 PM   #7
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
If you have the choices, you can pick up a programming language. Eg Ruby
Code:
$ ruby -e 'Dir["*"].each {|x| File.file?(x) && File.rename(x, x.gsub(/^AA/,""))  }'
Any of the others like Perl,Python etc does similar as well.
 
Old 03-20-2011, 08:04 PM   #8
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
Hi Tommy

Why is $(...) preferred over `...` (backticks)?

http://mywiki.wooledge.org/BashFAQ/082

Check this link It really useful to u, Mainly it is useful for to avoid unnecessary mistakes in script.
 
  


Reply

Tags
bash scripting, sed


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Renaming Files..Help npeddle Linux - Newbie 9 12-20-2010 02:17 PM
Renaming files mashcaster Linux - General 3 05-08-2009 03:14 PM
Renaming files Viablade Linux - Newbie 11 10-10-2008 01:54 PM
mass file renaming sed/awk/mv asalford Linux - General 2 01-16-2008 05:53 PM
renaming files starwarsfan982 Linux - Software 7 10-30-2006 02:06 PM


All times are GMT -5. The time now is 03:15 PM.

Main Menu
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