Extracting logs using gunzip awk and gzip

Hi All
I am trying to use a hard coded script into shell scripting but I am unable to .
Kindly find the Script below along with the command Please help

 
 gunzip -c FilePath/FileName_*.gz | awk '$0 > "[04/26/2016][03:30:00.000]" &&  $0 < "[04/26/2016][05:29:59.999]"'|\
 gzip >> FilePath/Outputfile.log.gz

I Am trying to use this in shell script as it is working manually.

  
 gunzip -c $file*.gz | awk "\$0 > "[$date1][$time]" && \$0 < "[$date3][$time1]"" | \
gzip >> $location 

Can you please let me know where am I wrong?

There might be some double quotes missing. What exactly is not working?

I need to take input of Date and Time from the user . It is not picking the correct values.

I have shared the manual line of code which is working and giving me correct response but I am not able to use it in shell script.

I need the below command to be used in shell script with various parameter taken from user

 gunzip -c FilePath/FileName_*.gz | awk '$0 > "[04/26/2016][03:30:00.000]" &&  $0 < "[04/26/2016][05:29:59.999]"'|\
 gzip >> FilePath/Outputfile.log.gz

parameter Date and Time..

Thanks for the response

---------- Post updated at 08:15 AM ---------- Previous update was at 08:15 AM ----------

thnx kindly find my query above

And how do you want to "take" these parameters from the user?

  1. Will the user give them as command-line arguments?
  2. Do you want your script to prompt the user for all of these arguments?
  3. Are there default values that the user can override with command-line options?
  4. Something else???

What have you tried?

I am taking Input from the user

echo -n "Enter Start Date in Format YYYYMMDD:"
read date
echo "You Entered : $date"
gunzip -c "$file_"*.gz | awk '$0 > "$Date $Time" && $0 < "$Date $Time"' | gzip >> "$location" &"

but this is not working $0 is picking the name of the shell script I am running .
I tried using back slash but that is also not working though the value does not get updated.

 
 initial=[$date1][$time]
  
 final=[$date3][$time1]
  
 Duration="\$0 > \"$initial\" && \$0 < \"$final\"" 
  
 gunzip -c $file*.gz | awk \'$Duration\' | gzip >> $location 
 

Using this the Duration is in proper structure as in the hard coded one.
but while running it is giving error.

It is giving me the below error

==============
awk: '$0

awk: ^ invalid char ''' in expression

What am I doing wrong?

You should get the quotation (singles and doubles) in order, and you should use awk 's -v option to pass variables into the program.

In addition to what RudiC has already suggested, you also need to realize that a string in the format you are prompting for ( YYYYMMDD ) is not going to work well to compare against a string in the format MM/DD/YYYY which is what you said works with your existing pipeline.

And that if the pipeline matching against [04/26/2016][03:30:00.000] (note that there is no space in this pattern) works, then a command that puts a space between the expansion of the Date and Time variables (as in

gunzip -c "$file_"*.gz | awk '$0 > "$Date $Time" && $0 < "$Date $Time"' | gzip >> "$location" &"

can't work). And, furthermore, if you are reading a variable named date , you need to reference the contents of that variable using $date (NOT $Date ).

Plus, when doing a "string comparison", be aware that e.g. "05/26/2014" is greater than "04/26/2015" which might not be what you expect.

Hi Sir
The Variable are working properly and are taking what is being shared by the user in input (Tried echo its fetching correct values).
so Date and Time is not the issue here.

I agree ' " can be a issue but I have tried a lot but not able to figure out how to use it.

I have shared the awk command above can you create a UI based script I can try?

Hi Kindly find the code written below:

 
 #!/usr/ssh/bin
# My first script
 echo -n "Enter Start Date in Format YYYYMMDD:"
read date
echo "You Entered : $date"
 echo -n "Enter Start Date in Format MM/DD/YYYY:"
read date1
echo "You Entered : $date1"
 echo -n "Enter END Date in Format YYYYMMDD:"
read date2
echo "You Entered : $date2"
 echo -n "Enter END Date in Format MM/DD/YYYY:"
read date3
echo "You Entered : $date3"
 echo -n "Enter Start Time in Format HH:MM:SS.SSS eg. 03:30:00.000 :"
read time
echo "You Entered : $time"
 echo -n "Enter End Time in Format HH:MM:SS.SSS eg. 05:29:59.999 :"
read time1
echo "You Entered : $time1"
 echo -n "Enter Path Where you want to save the Logs :"
read Path
echo "You Entered : $Path"
 initial=[$date1][$time]
 echo $initial
 final=[$date3][$time1]
 echo $final
 
location=$Path/smtracedefault_$server.log.gz
 
#ssh <server1>  "nohup  gunzip -c $file*.gz | awk '$0 > "'$initial'" && $0 < "'$final'"' | gzip >> $location &"
 #ssh <server2>  "nohup  gunzip -c $file*.gz | awk '$0 > "'$initial'" && $0 < "'$final'"' | gzip >> $location &"
 #ssh <server3>  "nohup  gunzip -c $file*.gz | awk '$0 > "'$initial'" && $0 < "'$final'"' | gzip >> $location &"
 #nohup gunzip -c $file*.gz | awk '$0 > "'$initial'" && $0 < "'$final'"' | gzip >> $location & 
  
 

in this code

the last command works if I log into the server manually(not through script) where as I want to log in using script only.

#nohup gunzip -c $file*.gz | awk '$0 > "'$initial'" && $0 < "'$final'"' | gzip >> $location & 

I am not able to use expect send and interact.Public and private keys cannot be placed on all servers.Userid and password cannot be hardcoded. SED does not work on the servers.

I want to logging into server and run the awk command working individually through script on all the required servers.

Please help.