1

I am using array to tackle with spaces in line of my file. But when i am using grep to filter with value of array it is breaking because of spaces.

For example my line is as per below

bbbh.cone.abc.com:/home 'bbbh.cone.abc.com 

As it has spaces i am using array as per below.

object1=$(echo "$line" | awk '{print $1}' )
object2=$(echo "$line" | awk '{print $2}' )
object3=$(echo "$line" | awk '{print $3}' )
object4=$(echo "$line" | awk '{print $4}' )
hiteshcharry=("$object1" "$object2" "$object3" "$object4")
grep  "${hiteshcharry[@]}" <filename>

It give me error because of spaces.

Below is the example.

I have below line in my file.

st.cone.abc.com:/platform/sun4v/lib/sparcv9/libc_psr.so.1 space 'st.cone.abc.com space [/platform/sun4v/lib/sparcv9/libc_psr.so.1]'

So i have 2 spaces in my above line. I have written my script in such way so that it can handle a line with maximum 4 spaces.

When i am running below command

omnidb -session "$sessionid" -detail | grep "${hiteshcharry[@]}"

it give me error because of spaces. However when i print the value of array it show me correct value.

Example : -

one of line from my file is as below( it has 2 spaces)

st.cone.abc.com:/platform/sun4v/lib/sparcv9/libc_psr.so.1 space 'st.cone.abc.com space [/platform/sun4v/lib/sparcv9/libc_psr.so.1]'

I am putting this value in my array named as hiteshcharry. when i am running below command

omnidb -session "$sessionid" -detail | grep "${hiteshcharry[@]}"

It is giving me error because of spaces in value of array. In output it should filter the line having value equal to array named hiteshcharry.

I hope this is clear now.

Output of omnidb command is in picture. So i want to grep the lines having

"st.cone.abc.com:/platform/sun4v/lib/sparcv9/libc_psr.so.1 space 'st.cone.abc.com space [/platform/sun4v/lib/sparcv9/libc_psr.so.1]'" from

output of omnidb command which is in picture

enter image description here

Thanks. i have added declare -p hiteshcharry and it start printing the each elements of array. But i am error shown in picture .

enter image description here

6
  • 1
    You're not passing an array to grep, you're passing a series of words as separate arguments, which is not what the tool expects. Edit your question to show us a minimal reproducible example with a sample of your file and the corresponding desired output. Commented Mar 20, 2017 at 15:54
  • Thanks Tom for response , i have updated my question. Commented Mar 20, 2017 at 16:50
  • Hi Tom can you please help in this. Commented Mar 21, 2017 at 13:24
  • it is still not clear, at least for me... like Tom commented, add a sample input (ex: few lines of output from omnidb -session "$sessionid" -detail) and expected output for that sample Commented Mar 21, 2017 at 16:18
  • Cross-posted on UL: unix.stackexchange.com/q/352860/203203 Commented Mar 21, 2017 at 16:27

1 Answer 1

2

When you pass your array to grep through "${array[@]}", grep will see each array element as a separate argument. So, the first element would become the pattern to search for, and the second element onwards would become the file names to be searched on. Obviously, that's not what you want.

You can use process substitution to make grep match the strings contained in your array, like this:

omnidb -session "$sessionid" -detail | grep -Fxf <(printf '%s\n' "${hiteshcharry[@]}")
  • printf will print your array elements one line per element
  • grep -Fxf treats the about output as a file containing strings to be searched (-F option treats them as strings, not patterns, -x matches the whole line of omnidb output, preventing any partial matches)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks , i have tried but it is giving me blank output. So please suggest further.
Include the output of omnidb command as well as declare -p hiteshcharry in your question.
Thanks i have updated my question with output of omnidb command. But can you clarify where to declare the hiteshcharry? In my script?
'declare -p' prints the content of your array. So, do it right after initializing the array.
Thanks. i have added declare -p hiteshcharry and it start printing the each elements of array. But i am getting error .I have added a picture of my error in question. Thanks in advance.

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.