1

I am trying to grep with all elements of array but it is breaking because of spaces.

Below is the value of my array:

server:/home/a-hkat # echo "${hitesharray2[@]}"    
ida0481.abc.xyz.net:/J 'ida0481.abc.xyz.net [/J]'

server:/home/a-hkat # declare -p hitesharray2
declare -a hitesharray2='([0]="ida0481.abt.xyz.net:/J" [1]="'\''ida0481.abc.xyz.net [/J]'\''")'

Below is the error:

server:/home/a-hkat # omnidb -winfs | grep "${hitesharray2[@]}"    
grep: 'ida0481.abc.xyz.net [/J]': No such file or directory

Desired result should be as below but using array:

server:/home/a-hkataria # omnidb -winfs | grep ida0481.abc.xyz.net:/J    
ida0481.abc.xyz.net:/J 'ida0481.abc.xyz.net [/J]' 

In short, I want to grep the below lines using an array or some other alternative.

server:/home/a-hkat # echo "${hitesharray2[@]}"
amerfs0039.abc.xyz.net:/F 'amerfs0039.abc.xyz.net [/F]'

server:/home/a-hkat # omnidb -winfs | grep amerfs0039.abc.xyz.net:/F
amerfs0039.abc.xyz.net:/F 'amerfs0039.abc.xyz.net [/F]' WinFS
cxw
  • 16,685
  • 2
  • 45
  • 81
  • 1
    Hi i have updated the question and also include the output of declare -p – Hitesh Kataria Apr 20 '17 at 13:22
  • 1
    Please clarify, since the question is changing constantly: Do you want to grep with each array value separately? (i.e. `array = (aaa bbb)` and want to `omnidb -winfs | grep aaa` and `omnidb -winfs | grep bbb` ) Or do you want to grep with a string composed with all array values put together? (i.e. `omnidb -winfs | grep "aaa bbb"`) – MarcM Apr 21 '17 at 11:07

1 Answers1

1

OP's solution

The OP posted in a comment, and I memorialize here for posterity:

I have solved this problem by using fgrep and then removing all the spaces from output of command omnidb -winfs and array so that it can match perfectly.

fgrep, or grep -F, treats things like [] as literals, where they would normally be considered to be grep metacharacters. See this answer for more. Since the OP's text included brackets, using fgrep permitted matching that text literally.

Original Answer

Your ${hitesharray2[@]} expands to two words, one for each element of the array. What you are getting is the same as if you had said

omnidb -winfs | grep "${hitesharray2[0]}" "${hitesharray2[1]}"

When grep is given more than one argument, it treats the arguments after the first as filenames. Here, the second element (with [/J]) is treated as a filename.

Edit Per your comment, it appears you want to jam all the elements of your array together as a single space-separated word. You can use:

omnidb -winfs | grep "${hitesharray2[*]}"
#        note the asterisk subscript ^

Or, if the amount of whitespace varies, you might need to do it the long way:

omnidb -winfs | grep "${hitesharray2[0]}\s*${hitesharray2[1]}"

See the bash-hackers wiki for more about ways of indexing arrays.

Community
  • 1
  • 1
cxw
  • 16,685
  • 2
  • 45
  • 81
  • I want to filer using ida0481.bt.bombardier.net:/J 'ida0481.bt.bombardier.net [/J]' – Hitesh Kataria Apr 20 '17 at 13:48
  • @HiteshKataria Did you try `"${hitesharray2[*]}"`? It looks like that will do what you want, if I understand you correctly. – cxw Apr 20 '17 at 16:01
  • Yes i tried but did not work. I have added more details of mu requirement in my question. Please check if you can help me. – Hitesh Kataria Apr 21 '17 at 10:00
  • @HiteshKataria Try `grep -F` with any of the options above per [this answer](http://stackoverflow.com/a/11856117/2877364). That will prevent `grep` from interpreting the square brackets in your text as part of a pattern. – cxw Apr 21 '17 at 15:31
  • Hi Thanks for input. I have solved this problem by using fgrep and then removing all the spaces from output of command omnidb -winfs and array so that it can match perfectly. Thanks again. – Hitesh Kataria Apr 24 '17 at 11:43