LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-07-2011, 08:03 PM   #1
mim747
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
Question Using grep search with array containing strings and spaces


Hi Everyone,

I want to be able to create an array that contains both strings and spaces within each element and use it as the pattern that grep will search for within a file.

I know that I can do it if I explicitly type the string within the grep command.

grep -ciw 'Hello world' file.txt

But it doesn't seem to work when I place the string with spaces in an array.

array[1]="'Hello'"
array[2]="'Hello world'"

grep -ciw "${array[1]} file.txt
grep -ciw "${array[2]} file.txt

grep for array[1] works, but the value for array[2] does not.

I tried all the possible premutation with regular expression to get it to work, with no luck.

Ultimately, I want to know if you can place single qoutes and or regular expressions within an array and have grep use it as a regular expression and not a literal value.

Any help would be appreciated...Thanks!!!
 
Old 10-07-2011, 08:13 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi,

try without the single-quotes:
array[2]="Hello world"

Invoke it with double-quotes:
grep -ciw "${array[2]}" file.txt
 
Old 10-07-2011, 10:41 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
In scripting it's vital to understand how the shell handles arguments and whitespace. Read these three pages carefully:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Short form: double-quoting variables protects the contents from being word-split during expansion.
 
Old 10-17-2011, 08:45 PM   #4
mim747
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Problem solved. Thanks for the help! THe wiki site on double quotes were extremely helpful!
 
  


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
[SOLVED] Recursive search for strings in files with a certain date: find -name or grep -R? wolverene13 Linux - Newbie 6 10-01-2011 06:05 PM
Display Array of Strings with White Spaces coffee arabia Linux - Newbie 3 10-02-2010 01:48 AM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 10:38 PM
grep two strings Hondro Linux - General 3 09-08-2008 10:55 PM
Grep search & replace strings with backslashes? ziphem Linux - Newbie 4 10-21-2007 05:36 PM


All times are GMT -5. The time now is 11:10 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration