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
2
  • 1
    Hi i have updated the question and also include the output of declare -p Commented Apr 20, 2017 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") Commented Apr 21, 2017 at 11:07

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I want to filer using ida0481.bt.bombardier.net:/J 'ida0481.bt.bombardier.net [/J]'
@HiteshKataria Did you try "${hitesharray2[*]}"? It looks like that will do what you want, if I understand you correctly.
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.
@HiteshKataria Try grep -F with any of the options above per this answer. That will prevent grep from interpreting the square brackets in your text as part of a pattern.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.