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 07-08-2008, 10:38 PM   #1
jhansonxi
LQ Newbie
 
Registered: Jan 2007
Posts: 10

Rep: Reputation: 0
Need help with bash array usage


I'm trying to find files with a specific filename extension, containing a specific string, in a directory tree where directory and file names may contain spaces, then put it into an array with one complete path per element.

With the following command I can obtain a list with quoted paths and load it into the array but the assignment escapes the quotes and each element gets a space-delimited substring. I think it's a word-splitting problem but am not sure how to fix it.
Code:
matching_files=($(find . -type f -iname "*.txt" -exec grep -l "MATCH" '{}' \;| sed -n 's/\(.*\)$/\"\1\"/p'|tr "\n" " "))
 
Old 07-08-2008, 11:20 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you are doing this one time, arrays are not needed. Just pipe your find output to process straight away.
 
Old 07-08-2008, 11:35 PM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
This seems a little clumsy. What do you have to do with the file list ?
 
Old 07-09-2008, 02:11 AM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Let me suggest you use the positional parameters and set. It is much simpler conceptually.

Code:
#!/bin/bash

IFS=$'\012'

echo No printx
set $(find . -type f -name "[abcd]*" -print)
echo -e "1: $1\n2: $2\n3: $3";

echo With printx
set $(find . -type f -name "[abcd]*" -printx)
echo -e "1: $1\n2: $2\n3: $3";

$ ./params
No printx
1: ./c
2: ./d
3: ./a b
With printx
1: ./c
2: ./d
3: ./a\ b
Save IFS if you need it later.
 
Old 07-09-2008, 01:29 PM   #5
jhansonxi
LQ Newbie
 
Registered: Jan 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
if you are doing this one time, arrays are not needed. Just pipe your find output to process straight away.
The data will be used several times in a script I'm writing.
 
Old 07-09-2008, 01:32 PM   #6
jhansonxi
LQ Newbie
 
Registered: Jan 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Mr. C. View Post
This seems a little clumsy. What do you have to do with the file list ?
I need to parse the found files several times in a script I'm writing. The command line is ugly but it should work with most distros as the tools are standard. I suspect the entire script could be done in a few lines of Perl but I don't know the language.
 
Old 07-09-2008, 01:36 PM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
What was clumsy is trying to handle quoting through layers of subshells. It is bad enough to deal with expansion order, but when child and grandchild shells get involved, it is a nightmare to be avoided.

Your solution is better handled as I showed in the example. You can assign the positional parameters to an array for later use.
 
Old 07-09-2008, 06:44 PM   #8
jhansonxi
LQ Newbie
 
Registered: Jan 2007
Posts: 10

Original Poster
Rep: Reputation: 0
I was able to get a solution from my friends at MDLUG. The answer:
Code:
IFS="$(echo -e "\n\r")"
matching_files=($(find . -type f -iname "*.txt" -exec grep -l "MATCH" '{}' \;| sed -n 's/\(.*\)$/\1/p'))
unset IFS
 
Old 07-09-2008, 07:33 PM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Excellent!
 
  


Reply


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
array in bash ramesh_manu Linux - Newbie 1 02-24-2007 11:19 AM
BASH - Array Variables Micro420 Programming 5 12-15-2006 05:49 PM
Array Help in BASH! ?*%$ johnnybhoy67 Linux - Software 2 02-22-2006 10:39 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
array in bin/bash how to x2000koh Programming 12 05-09-2003 04:51 PM

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

All times are GMT -5. The time now is 03:15 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