Results 1 to 5 of 5
Ok here is a problem that stumps me scripting with sed
I get we can have sed search ad replace for example i have a file called test_add1.cpp
and so ...
- 10-20-2009 #1Just Joined!
- Join Date
- Sep 2007
- Location
- Ann Arbor, MI
- Posts
- 22
using sed for changing file names
Ok here is a problem that stumps me scripting with sed
I get we can have sed search ad replace for example i have a file called test_add1.cpp
and so on with several add with a number followed by the .cpp
if i wanted to change test_ to finish i wouldand this would change all instances.Code:sed s/test_\(.*\)/final\1/'
How about if i need to write a script that could change delete a portion of the file name by passing two arguments from the command line
like if the script was called dropend
and the result would be the file called myfile and you can always ussume there will always be a period.Code:$./dropend myfile.bak .bak
im thinking something like using aand thenCode:newfilename=$ (echo $1| sed with the back quote technique)
doing something likebut i have no idea where to use $2 which is the second argument passed in to the script from the command line. I am stumped here.Code:mv $1 $newfilename
Please if some one can tell me where im going wrong?
Thanks,
Joe
- 10-21-2009 #2
I'll give you a start and let you work from it:
CheersCode:echo myfile.bak | sed "s/\.bak//"
- 10-21-2009 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
no need external commands in bash
Code:a="hello.bak" echo ${a%.bak}
- 10-22-2009 #4Just Joined!
- Join Date
- Sep 2007
- Location
- Ann Arbor, MI
- Posts
- 22
hey bird man thank you. I am using a script that works where you give it a file name and then the second argment giving is what you waant deleted off the file. It will not always start at the period example
[CODE] $dropend example.exe.tar .tar [CODE]
the script needs to pass the two arguments and make the change to the file name. so i cant hard code the files name or the part needing deleted. i need something that used $1 and $2 only im not getting how i can pass this to sed ???
- 10-22-2009 #5Just Joined!
- Join Date
- Sep 2007
- Location
- Ann Arbor, MI
- Posts
- 22
hey thanks birdman you got me thinking on the right path
her is the new script
works like a charm thank you so much#script that drops desired parts off #filenames
newfile=`echo $1 | sed "s/$2//"
mv $1 $newfile


Reply With Quote