Need help with 2 similar commands with different outputs


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need help with 2 similar commands with different outputs
# 1  
Old 06-13-2012
Need help with 2 similar commands with different outputs

NEVERMIND.... "$TIME" did the trick... stupid me. Smilie


Hi,

I know that this must be something pretty dumb but I cant figure it out myself.

I have this:
Code:
#!/bin/bash
TIME="Time: 20120611"
FILE=file.log

grep $TIME $FILE

And I get the following output:
Code:
file.log:Time: 20120611
file.log:Time: 20120611
file.log:Time: 20120611
.
.
.

but when i run
Code:
grep "Time: 20120611" file.log

I get my expected output:

Code:
Time: 20120611
Time: 20120611
Time: 20120611
.
.
.

Why I get 2 different outputs when basically Im using the same command?

Thanks in advance

Last edited by RedSpyder; 06-13-2012 at 10:36 AM..
# 2  
Old 06-13-2012
I guess (and it is just a guess, since I know next to nothing about how shell works) is that
1. Since the variable $FILE may refer to more than one files, grep is showing you the exact filename which matches content.
2. The variable TIME is not quoted in the grep command. Doesn't it show you a "no file or directory found" message? What is the output of:

Code:
TIME="Time: 20120611";FILE=file.log; grep "$TIME" $FILE

If it does not show you the file name in output, then my guess is that the part after "Time:" of the TIME variable is being treated as one of the search files and hence the output tells you the name of the file in which the data is found (as is the norm for multiple file grep).
This User Gave Thanks to jawsnnn For This Post:
# 3  
Old 06-13-2012
Yeah the problem is that I didnt used "" around $TIME. Put them and it worked.

Thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Comparing wc outputs using array

Hi All, Im trying to compare the wc -l output with another set of rowcount outputs which returned from sql... For Eg : Im storing the first outputs as below <srccnt=`wc -l $HOME/*.csv | awk {'print $1'}` and comparing this with the another set of outputs. descnt=`seclect count(*)... (7 Replies)
Discussion started by: Deena1984
7 Replies

2. UNIX for Dummies Questions & Answers

AWK - Different outputs *Question*

I'm having a small issue with AWK: I run this in PUTTY: awk 'BEGIN{FS=","}NR==FNR{A=$1;next}{if (A==$1) print $0}' FILE1 FILE2 And it gives me the output that I expect. I wanted to create a file.awk file that i could just run instead of typing this out all the time. But its not giving my... (1 Reply)
Discussion started by: WongSifu
1 Replies

3. Shell Programming and Scripting

Storing outputs into variables

I need to know how to store output from one command so that it can initiate another command. chktraf -s | cut -c1-4 output would look like 321 142 256 342 123 Then if the value of the output = 0, then initiate next command. if then for xx in 01 02 03 04 05 06 07 08 09 10 do ... (4 Replies)
Discussion started by: Shaun74
4 Replies

4. Shell Programming and Scripting

create outputs from other command outputs

hi friends, The code: i=1 while do filename=`/usr/bin/ls -l| awk '{ print $9}'` echo $filename>>summary.csv #Gives the name of the file stored at column 9 count=`wc -l $filename | awk '{print $1}'` echo $count>>summary.csv #Gives just the count of lines of file "filename" i=`expr... (1 Reply)
Discussion started by: rajsharma
1 Replies

5. Programming

strcat outputs garbage

Anyone have any ideas why when using strcat function I would get some garbage at the beginning of the output string? what I'm doing is something like the following example. Code: char temp; char tempHolder; for(int i=0;i<something;i++){ sprintf(temp,"%u ", someVariable);... (2 Replies)
Discussion started by: airon23bball
2 Replies

6. UNIX for Dummies Questions & Answers

Multiple Substring Outputs

Hello, I am reading a file with millions of lines in it. Each line is big line containing several xml tags. I need to Output just the value of two tags in a seperate flat file. For eg- I need to output whats present in <ComponentName> something </ComponentName> and another tag is... (2 Replies)
Discussion started by: sunnybehl
2 Replies

7. Shell Programming and Scripting

compare 2 outputs

Hello, I have a shell script that is used as follows: ./script -s <Oracle_server_name> the script returns several lines in this format: parameter name required value Current Value comments --------------- ------------- -------------- --------- My objective now is to compare 2... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

8. Shell Programming and Scripting

LotusScript similar to shell commands - need help

I am running the following commands but only the second part executes. I can't get the file to exit if the first statement is true, anyone have an idea? I know it's not UNIX but this is the best crowd for knowledge in all fields... Sub Querysave(Source As Notesuidocument, Continue As Variant)... (0 Replies)
Discussion started by: bushey1
0 Replies

9. Shell Programming and Scripting

Printing outputs using awk.

I have a output of a command like this. the command is : bdf|sed '/^e/d'|awk '{print$2/1048576}' output : 0 0.515625 0.481979 2 2 2 7.8125 4 2 0.488281 7.8125 3.90625 4 1.95312 1 0.488281 (4 Replies)
Discussion started by: Krrishv
4 Replies

10. Solaris

I may have accidentally redefined one of the outputs...

I get unexpected results when doing ls -al | more and ls -al | grep. When I do not pipe the output, it comes to the screen just fine, and there is output! When I pipe to another command, I either get nothing, or else I get a strange dump. Which of the outputs have I accidentally affected?... (2 Replies)
Discussion started by: joang
2 Replies
Login or Register to Ask a Question