LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2009, 06:46 AM   #1
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
[BASH] Find + sed --> can you pipe variables?


Hi...

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 ... )

Thanks
 
Old 08-29-2009, 06:52 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Look in the find info manual for -printf and the directives it uses.

find $DIR -type f -printf '%f\n'

I usually need to go back to the info manual to remember which directive to use.
Using -printf, you have a lot of control over the output format.
 
Old 08-29-2009, 06:57 AM   #3
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
it's failing because there's probably a '/' in $img_dir.

how about this..

Code:
pushd $img_dir
find . -maxdepth 1 -type f | sed "s/\.\///"
popd
 
Old 08-29-2009, 06:58 AM   #4
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Quote:
Originally Posted by jschiwal View Post
find $DIR -type f -printf '%f\n'
Ohh i like that!
 
Old 08-29-2009, 07:01 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 08-29-2009, 07:18 AM   #6
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by colucix View Post
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.

Last edited by RaptorX; 08-29-2009 at 02:57 PM.
 
Old 08-29-2009, 07:21 AM   #7
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Quote:
Originally Posted by RaptorX View Post
sed s%$img_dir%% it works perfectly.
I had no idea sed allowed this. Always fun to learn something new like this.

BTW, it works perfectly up until $img_dir contains a %

jschiwal's printf for the win...
 
Old 08-29-2009, 08:00 AM   #8
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
yes, but i am yet to find the -printf's directives in "info find", I found very general information there.
 
Old 08-29-2009, 08:16 AM   #9
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
use 'man find' and search for printf...

Quote:
%b File’s size in 512-byte blocks (rounded up).

%c File’s last status change time in the format returned by the C ‘ctime’ function.

%Ck File’s last status change time in the format specified by k, which is the same as for %A.

%d File’s depth in the directory tree; 0 means the file is a command line argument.

%f File’s name with any leading directories removed (only the last element).

%F Type of the filesystem the file is on; this value can be used for -fstype.

%g File’s group name, or numeric group ID if the group has no name.

%G File’s numeric group ID.

%h Leading directories of file’s name (all but the last element).

%H Command line argument under which file was found.

%i File’s inode number (in decimal).

%k File’s size in 1K blocks (rounded up).

%l Object of symbolic link (empty string if file is not a symbolic link).

%m File’s permission bits (in octal).

%n Number of hard links to file.

%p File’s name.

%P File’s name with the name of the command line argument under which it was found removed.

%s File’s size in bytes.

%t File’s last modification time in the format returned by the C ‘ctime’ function.

%Tk File’s last modification time in the format specified by k, which is the same as for %A.

%u File’s user name, or numeric user ID if the user has no name.

%U File’s numeric user ID.

%Z (SELinux only) file’s security context.
 
Old 08-29-2009, 08:48 AM   #10
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
ah! I thought that i had to do "info find" instead of "man find"... thanks for the pointers!
 
  


Reply



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
sed and bash variables cashton2k Linux - Newbie 6 03-28-2007 11:09 AM
sed and bash variables cashton2k Programming 1 03-23-2007 04:43 PM
sed with bash variables MurrayL Linux - Software 2 03-21-2005 08:13 AM
using bash variables in sed jimieee Programming 6 02-04-2004 04:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration