combine two greps


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers combine two greps
# 1  
Old 08-19-2011
Question combine two greps

Hi,

Syntax 1:
Code:
/usr/bin/svcs -a |grep apache22 | wc -l
2

Syntax 2:
Code:
 ps -ef | grep wls[a]dmin | grep -v "grep" | wc -l
3

I need to combine both the above syntax and get 2+3 i.e. 5 as the resultant output.

What's the simplest way to achieve this ?
# 2  
Old 08-19-2011
Code:
a=`/usr/bin/svcs -a |grep apache22 | wc -l`
b=`ps -ef | grep wls[a]dmin | grep -v "grep" | wc -l`
echo $(($a+$b))

In bash you can use just $((a+b)). And of course you do not need to use variables but they improve readability.
# 3  
Old 08-19-2011
Error

Quote:
Originally Posted by yazu
Code:
a=`/usr/bin/svcs -a |grep apache22 | wc -l`
b=`ps -ef | grep wls[a]dmin | grep -v "grep" | wc -l`
echo $(($a+$b))

In bash you can use just $((a+b)). And of course you do not need to use variables but they improve readability.
Can i embed this in the profile as I

Code:
su - wlsuser

it should automatically set variables a and b so that i just need to echo $(($a+$b)) to get output.

As i am naive would like to know where and how can this be set.

Also, I do not have root access.
# 4  
Old 08-19-2011
Well... Yes. But it's better use some more descriptive names (usually in uppercase) and export them. Add something like this in your profile:
Code:
APACHE_N=`...`
export APACHE_N

But these values will be constant. Maybe it's better to use aliases:
Code:
alias ca='/usr/bin/svcs -a |grep apache22 | wc -l'
alias pw='ps -ef | grep wls[a]dmin | grep -v "grep" | wc -l'

Then:
Code:
echo $((`ca`+`pw`))

It will work only on the command line not in scripts.
This User Gave Thanks to yazu For This Post:
# 5  
Old 08-19-2011
unrelated to the orginal question but here is a better way to write the first statement. avoids invoking the wc program.

original:
Quote:
/usr/bin/svcs -a |grep apache22 | wc -l
new
Quote:
/usr/bin/svcs -a |grep -c apache22
---------- Post updated at 08:14 ---------- Previous update was at 08:13 ----------

Quote:
Originally Posted by shifahim
Can i embed this in the profile as I

Code:
su - wlsuser

it should automatically set variables a and b so that i just need to echo $(($a+$b)) to get output.

As i am naive would like to know where and how can this be set.

Also, I do not have root access.
no - you can not use su without a password unless your root. For what your are doing root is not required. If it was you could use sudo.
# 6  
Old 08-19-2011
Question

As i said i do not know much about this.

Below is what i did.

Does not look like i have privileges or do i ?
Code:
cd

pwd
/home/wlsuser

ls -ltr .profile
-rw-------   1 wlsuser root          25 Oct 20  2009 .profile

id
uid=870(wlsuser) gid=641(wlsgrp)

groups wlsuser
wlsgrp webgrp webgroup ndm wlsuser httpd

Can you please help me with the detailed steps ?

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

Quote:
no - you can not use su without a password unless your root. For what your are doing root is not required. If it was you could use sudo.
Thats not a problem. I am providing the password as well. Setting this automatically is my concern.

---------- Post updated at 09:00 AM ---------- Previous update was at 08:19 AM ----------

Quote:
Originally Posted by yazu
Well... Yes. But it's better use some more descriptive names (usually in uppercase) and export them. Add something like this in your profile:
Code:
APACHE_N=`...`
export APACHE_N

But these values will be constant. Maybe it's better to use aliases:
Code:
alias ca='/usr/bin/svcs -a |grep apache22 | wc -l'
alias pw='ps -ef | grep wls[a]dmin | grep -v "grep" | wc -l'

Then:
Code:
echo $((`ca`+`pw`))

It will work only on the command line not in scripts.
echo $pw
return blank

while

Code:
 echo $((`ca`+`pw`))
bash: ./1: Permission denied
bash: 51: command not found
bash: +: syntax error: operand expected (error token is "+")

I am on bash and

Code:
uname -a
SunOS mymachine 5.10 Generic_144488-07 sun4v sparc SUNW,SPARC-Enterprise-T5220

Yes, I need the value to change dynamically and not be constant as some processes might get killed post su - wlsuser
# 7  
Old 08-19-2011
Save a process and some overhead by eliminating the "grep -v grep". Use this construct instead:
Code:
 
ps -ef|grep [s]earchstring

This matches "searchstring" but not itself.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Combining multiple greps

I'm trying to learn about regular expressions. Let's say I want to list all the files in /usr/bin beginning with "p", ending with "x", and containing an "a". I know this works:ls | grep ^p | grep x$ | grep abut I'm thinking there must be a way to do it without typing grep three times. Some of my... (9 Replies)
Discussion started by: Xubuntu56
9 Replies

2. Shell Programming and Scripting

Bash Scipting (New); Run multiple greps > multiple files

Hi everyone, I'm new to the forums, as you can probably tell... I'm also pretty new to scripting and writing any type of code. I needed to know exactly how I can grep for multiple strings, in files located in one directory, but I need each string to output to a separate file. So I'd... (19 Replies)
Discussion started by: LDHB2012
19 Replies

3. Shell Programming and Scripting

Script Optimization - large delimited file, for loop with many greps

Since there are approximately 75K gsfiles and hundreds of stfiles per gsfile, this script can take hours. How can I rewrite this script, so that it's much faster? I'm not as familiar with perl but I'm open to all suggestions. ls file.list>$split for gsfile in `cat $split`; do csplit... (17 Replies)
Discussion started by: verge
17 Replies

4. UNIX for Dummies Questions & Answers

Struggling to combine two Greps statements

Greetings! I have been tasked to create a report off files we receive from our hardware suppliers. I need to grep these files for two fields 'Test_Version' and 'Model-Manufacturer' ; for each field, I need to capture their corresponding values. When running each statement separately, I get... (4 Replies)
Discussion started by: alan
4 Replies

5. Shell Programming and Scripting

help with a shell script that greps an error from the logs

Hello everyone. I wrote the following script but the second part is not excecuting. It is not sending the notification by email if the error occurs. the send mail is working so i think the errorr should be in the if statement LOGDIR=/logs/out LOG=`date "+%Y%m%d"`.LOG-FILE.out #the log file ... (11 Replies)
Discussion started by: adak2010
11 Replies

6. Shell Programming and Scripting

combine

Dear all i am having text file like xxx|yyy|1|2| zzz|rrr|3|4| www|xxx|>< 5|6|>< jjj|kkk|>< 8|9>< i want to join two lines which are having ' >< ' by taking only two lines at a stretch ...using awk command the result output should be xxx|yyy|1|2| zzz|rrr|3|4| www|xxx|5|6|... (2 Replies)
Discussion started by: suryanarayana
2 Replies

7. Shell Programming and Scripting

combine

Hi I am having text file like this 001|ramu|hno221|>< sheshadripuram|delhi|560061>< 002|krishna|hno225|>< newdelhimain|delhi|560061>< i want to combine every two lines as single...line... i.e 001|ramu|hno221|sheshadripuram|delhi|560061 can u pls help me (3 Replies)
Discussion started by: suryanarayana
3 Replies

8. UNIX for Dummies Questions & Answers

Command that creates file and also greps that file?

I have a command that does something and then creates a log file (importlog.xml). I then want to grep that newly created log (importlog.xml) file for a certain word (success). I then want to write that grep result to a new file (success.log). So far I can run the command which creates the... (2 Replies)
Discussion started by: Sepia
2 Replies

9. UNIX for Dummies Questions & Answers

Combine commands

Hi, i tried to combine grep with find and it didnt work grep 'find dirname filename" i also would like that the file will be sorted in the way. thanks a lot. (2 Replies)
Discussion started by: Spoiler
2 Replies

10. Shell Programming and Scripting

Combine new files

Hi All , Any one help me to combine two files in shell scripting .Below is my requrement i have 2 files as follows filea newyork America Texas America london Engalad Fileb abc def xyz i have to combine this file as follows newyork America abc Texas ... (1 Reply)
Discussion started by: ajmalc
1 Replies
Login or Register to Ask a Question