Help with grepping within variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with grepping within variables
# 1  
Old 04-30-2009
Help with grepping within variables

I've got a script at the moment that looks like this:

Code:
if [ $submenunum -eq 6 ]
        then
                echo "How many hours would you like users to have logged in for? (single digits)"
                read hours
                tim=
                echo "These are the users who were logged in for $hours hours:"
                echo "$tim"
        fi

For the 6th option in this particular menu script I'm trying to get the following command:

Code:
last | grep '(02:'

to fetch from the last file all logins of a certain amount of hours, but I am trying to provoke the user to type in the particular amount of hours they want to see. So where I have the variable tim, I expected this to work:

Code:
tim=$(last | grep '(0$hours:')

but it doesn't seem to want to comprehend having an open bracket inside the grep, without a closed one. It does work without the brackets, but then shows not only the login lengths matching the hours, but also the actual timestamps aswell, which I don't want.

How can I make the grep work inside the variable?

Cheers for any help in advance, and since this is my first query here and may have some more, is it better to create a new topic each time or shall I continue to use this thread?
# 2  
Old 04-30-2009
Code:
#!/bin/ksh

typeset -Z2 tim

tim=8
last |
grep '[(]'$tim:

oh. and new topic each new problem.

Last edited by quirkasaurus; 04-30-2009 at 03:40 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grepping in Perl

Hello Friends, I have a password file at /etc/password.bak in which the fields are present in the below format i.e. each field is seperated by ":" vbjr3:x:1007:1007:student users:/home/vbjr3:/bin/bash dbhyt2:x:1008:1008:student users:/home/dbhyt2:/bin/bash bbwe3:x:1009:1009:student... (1 Reply)
Discussion started by: Ravi Tej
1 Replies

2. Shell Programming and Scripting

Grepping and replacing

Hey Friends, Need your help again. I have input.temp file as follows $cat input.temp Lakme|Beauty Products|Lipstick L'Oreal|Hair Care|Conditioner Lakme|Beauty Products|Lip gloss L'Oreal|Hair Care|Mild Shampoo Gala|Beauty Products|Mehndi Cones Lakme|Beauty Products|Eye Shadow... (2 Replies)
Discussion started by: anushree.a
2 Replies

3. Shell Programming and Scripting

Grepping more than one word

Dear Experts, Need your help. Typically we use "grep" to search and display a pattern in a txt file. However, here what we want is, we want to grep a line which contains 4 words any where in a line. For example. File has 10,000,000 lines in it out of which there is a particular line which... (1 Reply)
Discussion started by: anushree.a
1 Replies

4. Shell Programming and Scripting

Please help on grepping

Hi All, I have a log file and I want to parse the logfile with a script.A sample text is shown below: I would grep on "return code " on this file. Any idea how the lines above and below the grep patterns could also be extracted. Thanks! nua7 The runLoggingInstall return code is 0... (3 Replies)
Discussion started by: nua7
3 Replies

5. UNIX for Dummies Questions & Answers

grepping columns

Hi All, I was recently helped out 'big time' with my last post on changing multiple file formats (thx, scott1256ca and bakunin)! My new question is about selecting and displaying columns in a file using (possibly) grep. Several of my data files are spreadsheet format (columns separated by... (8 Replies)
Discussion started by: ScKaSx
8 Replies

6. Shell Programming and Scripting

grepping around

Using shell scripts, I use grep to find the word “error” in a log file: grep error this.log. How can I print or get the line 3 lines below the line that word “error” is located? Thanks in advance for your response. (9 Replies)
Discussion started by: cbeauty
9 Replies

7. UNIX for Dummies Questions & Answers

Grepping for strings

Hello. I have a dir of 1500+ dir. In these dirs is a file host, with a tag <x_tag>. I need to : 1. grep for all dir that contain this host file that contain <x_tag> 2. print a list of these host files containing <x_tag> is this better to egrep this? (5 Replies)
Discussion started by: t4st33@mac.com
5 Replies

8. UNIX for Dummies Questions & Answers

grepping for numbers in between

Ok here's the situation I have a log like the following: (S0) Time: 124937.326033 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.389946 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.462494 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.551056 - ... (1 Reply)
Discussion started by: fusion99
1 Replies

9. UNIX for Dummies Questions & Answers

grepping for a sentence

Can you grep for a sentence. I have to search logs everyday at work and I was wondering if I could search for a string of words instead of just one. for example, if I had to find this sentence: "Received HTTP message type" How would I grep it (2 Replies)
Discussion started by: eloquent99
2 Replies

10. UNIX for Dummies Questions & Answers

grepping

Is there a way to grep for something and then print out 10 lines after it. for example if I want to grep for a word, then output the following 10 or whatever number of lines after the word. (5 Replies)
Discussion started by: eloquent99
5 Replies
Login or Register to Ask a Question