Whitespace breaking my egrep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Whitespace breaking my egrep command
# 1  
Old 10-05-2011
Whitespace breaking my egrep command

Hi,

I am writing a bash script which will dynamically generate an egrep command searching for a number of times within a file.

The problem is that I am putting a space in a variable called timestr. When I echo the variable the space comes out normally:
Code:
'(2011-10-05 12:|2011-10-05 13)'

When I try and use it in an egrep command it puts single quotes there and messes up the command:
Code:
+ grep TEST /var/tmp/TEST.log
+ egrep ''\''(2011-10-05' '12:|2011-10-05' '13)'\'''
egrep: Unmatched ( or \(

Below is my code:
Code:
todayf=$(date +%Y-%m-%d)
todayf="${todayf} "
timestr="'("
#Calc hr range
for (( i=${starthr}; $i <= ${endhr}; i++ ))
do
        if [ ${i} -eq ${starthr} ]
        then
                timestr="${timestr}${todayf}${i}:"
        else
                timestr="${timestr}|${todayf}${i}:"
        fi
done
timestr="${timestr})'"
echo ${timestr}
grep =${searchstr} $logpath/$logfile | egrep ${timestr}

Any ideas where I am going wrong?
# 2  
Old 10-05-2011
If you properly quote your variables like "${VARIABLE}" that should properly preserve the spaces in it.

That said, I don't understand your strategy here, particularly in putting literal brackets and single quotes into your search string since these don't appear to be what you're looking for.

Also this: grep =${searchstr} $logpath/$logfile | egrep ${timestr} would seem to imply you're looking a literal = character right before your search string? Is that correct?

Could you explain what you're trying to do, not just the manner you wish to accomplish it?
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-05-2011
Thanks for the response, that's done the trick. Can't believe it was staring me in the face all that time!

I was building the search string to create the same command I would if I was manually issuing it. I always use
Code:
egrep '(TERM_A|TERM_B|TERM_C)'

I believe the brackets are superfluous but I guess it's a habit. I guess I could also have done -e but then I'd still of had my whitespace issue.

As you have noticed, I am actually looking for a literal = character in front of my search string, that is intentional.

The script is designed so that I will pass in arguments specifying the search string, start and end times and search for all occurrences within that time frame. There is more to the script obviously, but I just pasted the relevant part.

Anyway thanks for your help.
# 4  
Old 10-05-2011
There may be easier ways to assemble a list of things than that. You can set $1,$2,$3 with set --, and control what $* prints with IFS:
Code:
$ set -- TERM_A TERM_B TERM_C # Set $1=TERM_A, $2=TERM_B, ...
$ OLDIFS="$IFS"
$ IFS="|"
$ echo "($*)"
(TERM_A|TERM_B|TERM_C)
$ echo \($*\)
(TERM_A TERM_B TERM_C) # This happens because the shell splits on | now!  echo gets 3 args
$ IFS="$OLDIFS" # Set splitting back to normal!

---------- Post updated at 02:31 PM ---------- Previous update was at 02:26 PM ----------

Or, you could just put some of that stuff into the expression instead of grep:

Code:
egrep "= (${STRING})" ...

to make it more obvious what you're trying to do.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-05-2011
Thanks for the tip. I'm not quite sure how it makes it easier, as I still need my for loop to generate the search string (because i will not specify each individual hour to be searched for, just the start and end like starthr=18 and endhr=20 would search from 6pm till 8pm), but I guess it would make the code cleaner.

Anyway sadly it seems I spoke too soon. Although I don't get an error the egrep isn't working still.

Code:
+ grep TEST /var/tmp/TEST.log
+ egrep ''\''(2011-10-05 12:|2011-10-05 13)'\'''

it doesn't return any results because it is literally issuing that command as opposed to the
Code:
egrep '(2011-10-05 12:|2011-10-05 13)'

that I want.

This is what I updated my code to:
Code:
grep =${searchstr} $logpath/$logfile | egrep "${timestr}"

(Only this line was editted)

Clearly I am doing something wrong with the single quotes/brackets in the timestr variable. I can probably get around this by using egrep -e instead of the single quotes and brackets to form the command, however I'm interested to know what I am doing wrong.

Any ideas? Sorry if it's something stupid, as you can tell I'm not a scripter.

Last edited by remixed; 10-05-2011 at 06:06 PM..
# 6  
Old 10-05-2011
It helps because you can build a list and print it out in one whack without worrying about where to add | chars... If I knew what your shell was I might be able to tell you even easier ways.

Anyway, you've got ' quote chars in your search string. I did ask if the literal quotes in
Code:
timestr="'("
timestr="${timestr})'"

were supposed to be there, because they have no special meaning to the shell when inside double-quotes. grep is hunting for literal 's.

The shell never evaluates what's inside variables for quote characters anyway, so adding them is doubly redundant.

---------- Post updated at 03:19 PM ---------- Previous update was at 03:15 PM ----------

I think you could replace those with

Code:
timestr=""
...
timestr="(${timestr})"

...again, pairing the brackets there to make it more obvious what you're actually doing.

---------- Post updated at 03:20 PM ---------- Previous update was at 03:19 PM ----------

Quote:
Originally Posted by remixed
This is what I updated my code to:
Code:
grep =${searchstr} $logpath/$logfile | egrep "${timestr}"

(Only this line was editted)
You forgot the quotes, again. It's splitting on spaces, again.


Code:
grep "=${searchstr}" $logpath/$logfile | egrep "${timestr}"

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 10-05-2011
Thanks, sounds like I have some reading to do. The searchstr variable that you mention doesn't have any whitespaces in it, but for the sake of being robust I will add it. (it's the timestr variable where it's all going wrong. Anyway I think I have enough food for thought there
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Egrep command is not working for me

my file is below REREGISTER is something to Failed to create the request Failed to create the request in not easy I know how REREGISTERcommand i run is egrep 'REREGISTER|Failed|to|create|the|request' test1 expected output REREGISTER is something to Failed to create the request i should... (2 Replies)
Discussion started by: mirwasim
2 Replies

2. Shell Programming and Scripting

help in egrep command in file

Hi Guys, I need hepl on egrep commnad I have one file and i need grep only specific lines.. EX: 2012-04-01 02:15:14 w 2012-04-01 02:15:14 w 2012-04-01 02:15:14 w 2012-10-26 02:15:14 w 2012-04-01 02:15:14 w 2012-10-26 02:15:14 w 2012-10-26 02:15:14 w 2012-10-26 03:18:56 M... (1 Reply)
Discussion started by: asavaliya
1 Replies

3. Shell Programming and Scripting

Help with egrep command

Hello folks, Here's how my current egrep command works: egrep "NY|DC|LA|VA|MD" state_data.txt I am planning to use a file to enter all allowable state values like say a new state_names.lookup with the following data: NY DC LA VA MD egrep "`cat state_names.lookup`"... (6 Replies)
Discussion started by: calredd
6 Replies

4. Shell Programming and Scripting

Help with egrep command

cat /tmp/inventory.csv|grep AARP|egrep -v "T11|12.4\(7\)" how do i exclude in addition to above 12.4\(3\) I have tried adding this in i.e -v "T11|12.4\(7\)|12.4\(3\)" but it did not work (3 Replies)
Discussion started by: slashbash
3 Replies

5. Shell Programming and Scripting

egrep command and order

Hi All, Here is my question. I have two files, file1.txt and file2.txt. I need the line number (index number) of file2.txt where the words in file1.txt appear. But they have to be in the same order as file1.txt. In example, file1.txt Z K A ... T file2.txt W A Q R (6 Replies)
Discussion started by: senayasma
6 Replies

6. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

7. Shell Programming and Scripting

Query regarding egrep command

Hi All, I am having a query regarding the usage of egrep command. i am having two unix environmanets in environment when i am using "egrep -f" it is working fine and other unix environment i am getting a syntax error. Please let me know if i need to set any environmane variables. ... (12 Replies)
Discussion started by: Sriram.Vedula53
12 Replies

8. Shell Programming and Scripting

Help with egrep command

Hi All, I am using egrep command to search one pattern. Following is the command i am using egrep -i "ACL*" filename but its also giving me the records which do not contain ACL. any help would be appreciated. Regards, Sam (3 Replies)
Discussion started by: sam25
3 Replies

9. Shell Programming and Scripting

sed command breaking because of /

hey im having a problem with my script replace="dir/another_dir" sed s/something/$replace/g is there a way i can get around this problem without using the escape string character "\" (2 Replies)
Discussion started by: nookie
2 Replies

10. Shell Programming and Scripting

egrep command

I'd like to grep a pattern of a version number as *_number.number.number number should be digit my grep is |egrep '^*++\.+' It works for V_3.2.1 or V _5.3.2 but not with V_43.6.543 !!!!! How can I specify any repetition of digit in the ? thanks, (4 Replies)
Discussion started by: annelisa
4 Replies
Login or Register to Ask a Question