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

Recommended Answers

All 3 Replies

Member Avatar for nileshgr

Option #1:

Remove space from a.txt

Option #2:

Use this code -

sed 's/\ //gi' a.txt | grep "$STR"
Member Avatar for nileshgr

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.