egrepping from a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting egrepping from a variable
# 1  
Old 04-29-2012
egrepping from a variable

i stored a lot of lines in a variable. and i want to be able to scan that variable quickly. sounds simple enough. but really, it isnt. scanning a variable containing a lot of lines actually is very slow.

i'd rather not have to write the contents of the variable into a file.

but here's an example of what i'm doing that is taking too long:

Code:
echo "$VarLines" | egrep "error"

I tried something like the following but its not working:

Code:
egrep "error" "$VarLines"

Any ideas?
# 2  
Old 04-29-2012
Perhaps this?
Code:
egrep "error" << EOF
$VarLines
EOF

--
How many lines is "lots of lines" ?
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-29-2012
Quote:
Originally Posted by Scrutinizer
Perhaps this?
Code:
egrep "error" << EOF
$VarLines
EOF

--
How many lines is "lots of lines" ?

it could be 200,000 lines or more.

what problems can you envision this causing? and by the way, i tested your command. it works, but i get this error:
Code:
line 7: warning: here-document at line 5 delimited by end-of-file (wanted `EOF')


Last edited by SkySmart; 04-29-2012 at 03:37 PM..
# 4  
Old 04-29-2012
@SkySmart
Quote:
egrep "error" "$VarLines"
This command is a complete error unless $VarLines is the name of a file.

Please post what Operating System you are running and what Shell you are using. If you do not know how to do this, please ask.

Environment Variables are not files. IMHO Any Script which stores more than one line in an Environment Variable is faulty.

Please post the script you are testing complete with sample input, expected output and actual output. If you have timing issues, please post real time values.

If you do not understand these basic requirements, please post in "Unix for Dummies" and ask lots of relevant questions.
# 5  
Old 04-29-2012
Quote:
Originally Posted by methyl
@SkySmart

This command is a complete error unless $VarLines is the name of a file.

Please post what Operating System you are running and what Shell you are using. If you do not know how to do this, please ask.

Environment Variables are not files. IMHO Any Script which stores more than one line in an Environment Variable is faulty.

Please post the script you are testing complete with sample input, expected output and actual output. If you have timing issues, please post real time values.

If you do not understand these basic requirements, please post in "Unix for Dummies" and ask lots of relevant questions.
The operating system I intend to run this own will be linux (any variant of it) and sun solaris.

the shell is always bash.

sample of the script is:

Code:
#!/bin/sh

CONTENT=$(tail -200000 /var/logs/catalina.out)

egrep "Accepted order" << EOF | tail -1 $CONTENT


The problem is that the catalina.out file is a huge file. over 1 GB. i have it seen it go up to 3 or 4 GB. I have no control over that.

I'm sure by looking at the sample script i provided, you can maybe come up with much more smarter way of doing this...without having to write to a file.

as far as the output of the log file. lets just imagine that each line of the log file i'm interested in must contain "Accepted Order".

thank you so much
# 6  
Old 04-29-2012
Quote:
#!/bin/sh

CONTENT=$(tail -200000 /var/logs/catalina.out)

egrep "Accepted order" << EOF | tail -1 $CONTENT
This script fragment is syntactically incorrect, contains erroneous assumptions about the "tail" command and quite honestly does not make sense. You cannot store multiple lines in one variable sensibly (let alone 200,000 lines !).
The script contains too many fundamental script errors to guess the intention. What (in words) are you try to achive?



Quote:
the shell is always bash.
#!/bin/sh
Make you mind up ! Under Solaris the Shell /bin/sh is the basic Bourne Shell and definitely not bash. The bash Shell may not be available unless installed as a non-standard add-on to the Solaris system.



Footnote: Bit of a guess based on the sketchy information posted, but did you mean?:
Code:
grep "Accepted order" /var/logs/catalina.out | tail -1


Last edited by methyl; 04-29-2012 at 06:15 PM.. Reason: typo
# 7  
Old 04-29-2012
Quote:
Originally Posted by methyl
This script fragment is syntactically incorrect, contains erroneous assumptions about the "tail" command and quite honestly does not make sense. You cannot store ultiple lines in one variable sensibly (let alone 200,000 lines !).
The script contains too many fundamental script errors to guess the intention. What (in words) are you try to achive?




Make you mind up ! Under Solaris the Shell /bin/sh is the basic Bourne Shell and definitely not bash. The bash Shell may not be available unless installed as a non-standard add-on to the Solaris system.



Footnote: Bit of a guess based on the sketchy information posted, but did you mean?:
Code:
grep "Accepted order" /var/logs/catalina.out | tail -1

so here's the problem.

as i pointed out in my previous post, i have a file that can be as huge as 4GB is size.

grepping out anything from a file that big is bound to take forever. which means a command like the one below is no good:
Code:
grep "Accepted order" /var/logs/catalina.out | tail -1

so, this script is meant to choose the number of lines to grep from instead of having to grep through a file that is huge. this will pose the least resource usage on the system.

in other words, what the script is doing is something to the effect of:

if the file is too big, just pull out the last 200000 lines from it and work on those lines.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

5. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

6. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

7. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question