Register
It is currently Mon Dec 22, 2014 4:12 am

Pipes in Backticks not working


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Tue Apr 26, 2011 6:59 am   

Joined: Tue Apr 26, 2011 6:52 am
Posts: 4
I want to rename a list of files which have the name something_accepted.nt to something.nt.
Now my idea was:
Code:
find -iname *accepted.nt | xargs -I @ mv @ $(echo @ | sed "s|_accepted||")

(I've read backticks are deprecated so I replaced them with $(), but I don't know the name of this so I kept the name :-))

Strangely this did not work and one researching I found that the following only prints blank lines:

Code:
echo "test"  | xargs -I @ echo $(echo @ | echo)


While without pipes it works:

Code:
echo "test"  | xargs -I @ echo $(echo @ | echo)


I did search google & this forum but I didn't find a solution so I would like to know a) how I can get this to work and b) if there is another approach that does not use pipes and backticks I would like to know how to use them together anyways.


Top
 Profile  
 PostPosted: Tue Apr 26, 2011 7:39 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
Hi,

find has -exec option, so no more need of xargs

bash has parameter expansion, so no need of sed

Code:
find -iname "*_accepted.nt" -exec bash -c 'mv "${1}" "${1//_accepted/}"' _ {} \;


you could also use rename.


Top
 Profile  
 PostPosted: Thu Apr 28, 2011 4:22 am   

Joined: Tue Apr 26, 2011 6:52 am
Posts: 4
Thank you very much this worked! However I would like to know why the thing that I did did not work (even if it's not necessary to do it that way) and also, what ""${1}" "${1//_accepted/}"' _ {} \;" means, is it some kind of regular expression?


Top
 Profile  
 PostPosted: Mon May 02, 2011 10:34 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
kirdie wrote:
Thank you very much this worked! However I would like to know why the thing that I did did not work (even if it's not necessary to do it that way) and also, what ""${1}" "${1//_accepted/}"' _ {} \;" means, is it some kind of regular expression?

Because the sed command was operating on @, not on the file name. You can easily test it with
Code:
find -iname \*accepted.nt | xargs -I @ echo mv @ $(echo @ | sed "s|\x40|at_sign|")

Note also the backslash before *: needed in case there are matching files in the current directory.


Top
 Profile  
 PostPosted: Sat May 07, 2011 3:24 am   

Joined: Tue Apr 26, 2011 6:52 am
Posts: 4
Yeah but how can I get sed to operate on the content of the @ instead of the @ itself? The problem is that I understand my approach as I already understand sed but not the other one and I have now a similar problem in that I want to replace spaces in folder names with underscores and I can't get it to work :-(

Edit: I now looked into http://www.network-theory.co.uk/docs/ba ... nsion.html and it says:

${parameter/pattern/string} The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string.

However with "${1//_accepted/}" this would be

parameter = "1"
pattern = ""
string =" _accepted/" as you have 3 slashes instead of the normal 2. or what is the meaning of the double slash?

Oh and what is the
Code:
_ {} \;
for?


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP