LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-21-2011, 05:17 PM   #16
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Original Poster
Rep: Reputation: 147Reputation: 147
Thumbs up Tested and works


Quote:
Originally Posted by grail View Post
Code:
while read -r FILE
do
    find dir2 -type f -name "$FILE"
done< <(find dir1 -type f -printf "%f\n" | sed 's/[^[:alnum:]]/\\&/g')
It seems to work and *bonus* even works if files under dir1 are buried in a completely different hierarchy than files under dir2. It looks a little over-engineered though and I'm not sure all that complexity is really doing anything useful.

You're escaping all non-alphanumeric characters. I cannot think of a scenario where this would not work, but it goes further than necessary for my scenario.

Is it really necessary to use process substitution for gathering the file names from dir1? If so, then why?


I tried piping the dir1 file names into the loop and it seemed to work perfectly. When might piping not work?

Code:
foo$ find dir1 -type f -printf "%f\n" | sed 's/[^[:alnum:]]/\\&/g' |
> while read -r FILE
> do
> find dir2 -type f -name "$FILE"
> done
dir2/subdir2/file2
dir2/subdir2/file[a1]
dir2/subdir2/deepdir/messed[[up]name
dir2/subdir1/file1
Thanks very much for this example, although I still prefer
Quote:
Originally Posted by http://www.linuxquestions.org/questions/programming-9/bash-find-how-to-avoid-%5B-%5D-pattern-matching-in-file-names-expanded-from-%24var-875990/#post4331213
konsolebox's solution
because IMHO it is more graceful and easier to type.

Last edited by Telengard; 04-22-2011 at 05:06 PM.
 
Old 04-22-2011, 12:53 AM   #17
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,654

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
The main reason I prefer substitution over piping is that should you also require data from within the loop, ie. maybe you add a counter, it
will not be accessible from outside the loop due to the pipe creating a subshell, but in the simplest of cases it works just fine

Not sure I would agree with easier to type ... but I do like keeping it mostly all bash
 
Old 04-22-2011, 05:15 PM   #18
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Original Poster
Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by grail View Post
should you also require data from within the loop, ie. maybe you add a counter, it will not be accessible from outside the loop due to the pipe creating a subshell,
Couldn't that be solved by marking the counter variable for export before creating the subshell? I guess maybe not because variable normally export parent to child (downstream) and not child to parent (upstream).

Code:
tmp$ export cmts=0
tmp$ awk '/^#/' ~/.bashrc |
> while read
> do
>   cmts=$((cmts + 1))
>   echo -n "$cmts "
> done ; echo -e "\n$cmts"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
0
tmp$
Quote:
but I do like keeping it mostly all bash
Bash is a general purpose interpreter, so it probably is slower at specialized tasks than a dedicated program would be. For example, Bash can do what tr can do, but I'd be willing to bet that tr does it faster. At some point though, the expense of invoking additional processes within a loop will likely outweigh the benefit of using a specialized program.

Last edited by Telengard; 04-22-2011 at 05:53 PM. Reason: add code
 
Old 04-23-2011, 02:27 AM   #19
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,654

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
Quote:
For example, Bash can do what tr can do, but I'd be willing to bet that tr does it faster.
Not to be too nit picky, but this is probably not a good example as you need to first call bash builtin echo and bash now supports parameter substitution.
Code:
x="this is a test"

echo $( echo "$x" | tr ' ' '_' )

echo "${x// /_}"
But I would agree when looking at something like sed or awk in comparison to bash for getting data from a file efficiently.
 
Old 04-23-2011, 03:36 AM   #20
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Original Poster
Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by grail View Post
Not to be too nit picky, but this is probably not a good example as you need to first call bash builtin echo and bash now supports parameter substitution.
Code:
x="this is a test"

echo $( echo "$x" | tr ' ' '_' )

echo "${x// /_}"
I don't want to go too far afield here, but I can't understand why you're making it so complex.

No need for parameter substitution. No need for echo.

Code:
foo$ x='this is a test'
foo$ tr ' ' '_' <<< "$x"
this_is_a_test
foo$
Beyond that, I'm pretty sure tr can be invoked without involving Bash whatsoever. There are other shells, and other command interpreters. I don't think program1 even needs Bash (or any other shell) to invoke program2 (although I may be wrong about that.)

Lately I've been thinking of invoking programs within AWK. I manipulate the output of programs with other programs. If I'm using AWK anyway, do I really need so many other programs?

I'm sure an AWK solution to the problem in this thread is possible.
 
  


Reply

Tags
bash, filename expansion, find, pattern, variable


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
BASH:find out if volume "foo"/folder "goo" can be written to SilversleevesX Programming 2 08-28-2010 11:03 AM
Using "OR" type matching pattern with grep command riteshanand40 Linux - Newbie 3 05-06-2009 05:48 AM
"searching for a matching pattern" sharath.bv Programming 6 02-02-2008 12:24 PM
unclean shutdown causes "Couldn't find matching filesystem: label=" error irnerdette Red Hat 3 06-02-2006 02:44 PM
Pattern matching with a "case" statement in BASH King V Programming 8 04-27-2006 05:19 PM


All times are GMT -5. The time now is 07:12 AM.

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