[SOLVED] [BASH] Find + sed --> can you pipe variables?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am writing a little script and i use this to list some files in the working directory:
Code:
find $img_dir -maxdepth 1 -type f
well the issue is that the list contains the path + the name of the file... I wanted to just get the filenames...
I thought of something like:
Code:
find $img_dir -maxdepth 1 -type f | sed "s/$img_dir//"
it didnt work it gives me errors like:
Code:
sed: -e expression #1, char 9: unknown option to `s'
but if I use an environment variable like $PWD sed works fine... the question is is it possible to pipe a normal variable? or is there any other way to do what im looking for? (well this is linux there ARE tons of ways of doing EVERYTHING ... )
In sed the slash as a delimiter for the substitute command is not mandatory. You can use any other character: the first one after the s command will be used as delimiter. These all are valid:
Code:
sed "s%$img_dir%%"
sed "s@$img_dir@@"
and so on. I prefer the solution by jschiwal, anyway.
In sed the slash as a delimiter for the substitute command is not mandatory. You can use any other character: the first one after the s command will be used as delimiter. These all are valid:
Code:
sed "s%$img_dir%%"
sed "s@$img_dir@@"
and so on. I prefer the solution by jschiwal, anyway.
Interesting... I will be diving in to sed and awk soon, but that is cool to know since a lot of scripts that i have seen use the @ delimiter and i was confused about it...
if i use sed s%$img_dir%% it works perfectly.
and yes: find . -printf "%f\n"
the best solution here.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.