0

i have a file a.txt which contains
"ysm.set.ac4-qa.sds.audit
add yinst setting yti. YTI_
HOST_OVERRIDE =qa-sav-midas-002.ysm.ac4.yahoo.com"

I have variable STR which is dynamically assigned the value
"HOST_OVERRIDE=qa-sav-midas-002.ysm.ac4.yahoo.com"

I need to grep $STR on the file a.txt as `grep "$STR" a.txt `
But because of the spaces before =, the line is not getting printed. But my program definetely needs it to be printed ignoring the spaces. Is there any way to achieve it..?
Please reply any one.

Thanks
Srujana

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by issue9
0

Option #1:

Remove space from a.txt

Option #2:

Use this code -

sed 's/\ //gi' a.txt | grep "$STR"
0

One more method I found is to use sed to trim infinite white spaces. The code I posted previosuly works only for single space.

Here's the code for infinite spaces -

sed -r 's/[ ]+//gi' a.txt | grep $STR
0

You might want to try

awk 'BEGIN{ str="'"${STR}"'" }{ buf=$0; gsub(/ /, "", buf) } buf ~ str { print }' a.txt

which should print the "matched" line untouched.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.