pipe'ing grep output to awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pipe'ing grep output to awk
# 1  
Old 12-11-2009
pipe'ing grep output to awk

This script is supposed to find out if tomcat is running or not.

Code:
#!/bin/sh
if netstat -a | grep `grep ${1}:  /tomcat/bases | awk -F: '{print $3}'` > /dev/null
then
        echo Tomcat for $1 running
else
        echo Tomcat for $1 NOT running
fi

the /tomcat/bases is a file that contains a list of all the tomcat instances running and which port number it is running
Code:
user1:yes:8080
user2:no:8081
user3:yes:8082

I know it uses netstat to check if any of the port numbers in the "bases" file is listening but i dont quite understand how this is achieved by that command shown above.

Thanks
# 2  
Old 12-13-2009
I believe the test here simply says that the port number is listed by netstat(1) or not, so the port could be in various states, not just listening.
There is a slight flaw in that if a process was using port 80801 for instance (can they go that high?) then a test for user1 would give a false positive result. Making the first grep a "grep -w" would avoid this.
A "netstat -an" would be quicker because it would not translate any IP addresses to hostnames.
# 3  
Old 12-14-2009
Try:


Code:
#!/bin/sh
if netstat -an | grep -q  "8080 .*LISTEN"
then
        echo Tomcat for $1 running
else
        echo Tomcat for $1 NOT running
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pipe or combine output of three awk commands

What is the correct syntax to pipe or run three awk commands? Basically, using the output of the first awk as input in the second. Then using the output of the second awk in the third. Thank you :). awk 'FNR==NR {E; next }$3 in E {print $3, $5}' panel_genes.txt RefSeqGene.txt > update.txt |... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Use less pipe for grep or awk sed to print the line not include xx yy zz

cat file |grep -v "xx" | grep -v "yy" |grep -v "zz" (3 Replies)
Discussion started by: yanglei_fage
3 Replies

3. Shell Programming and Scripting

Grep:pipe delimited output

Hi, I am trying to search for a string in a file and print all the matched lines as pipe delimited format. My command is cat m_gid_trans.XML|grep -i '<TABLEATTRIBUTE NAME ="Lookup cache directory name"' The output I am getting is <TABLEATTRIBUTE NAME ="Lookup cache directory name"... (4 Replies)
Discussion started by: sampoorna
4 Replies

4. Shell Programming and Scripting

Pipe awk's output to sed for deletion

Hi Friends, I am using a command that prints certain lines from a file. For ex: cat input abc chr1 456 def chr1 789 ghi chr1 999 jjj chr1 777 jhk chr7 914 My command awk '{if($2=="chr1" && $3>=456 && $3<=999) {print $0}}' OFS="\t" input Output being printed is abc chr1 456 (7 Replies)
Discussion started by: jacobs.smith
7 Replies

5. UNIX for Dummies Questions & Answers

Use awk to pipe output from one file into multiple files

Hi All. Thanks for your help in advance. I have a requirement to examine the number of delimiters in each record of a file. If the record has the expected number of delimiters it should be passed into a 'good' file. If it does not, the record should be passed into a 'bad' file. I have been able... (8 Replies)
Discussion started by: codestar1
8 Replies

6. UNIX for Dummies Questions & Answers

grep'ing a variable - why not working

Hi all, Am writing a ksh script where I am looking for processes that has gone defunct and all of which has the same PPID PID is the variable that I need to match as this is the process ID of the processes that has gone defunct Am just curious how come the following DOES NOT work? ps... (6 Replies)
Discussion started by: newbie_01
6 Replies

7. Shell Programming and Scripting

pipe output of grep to sed?

Is there a way I can do this: search for text and replace line containing matched text with a different line? For example: "I want to replace text" I want to search for replace and then change the line to I am perplexed. Hope that makes sense. Thanks in advance. (4 Replies)
Discussion started by: arsh
4 Replies

8. Shell Programming and Scripting

grep'ing and sed'ing chunks in bash... need help on speeding up a log parser.

I have a file that is 20 - 80+ MB in size that is a certain type of log file. It logs one of our processes and this process is multi-threaded. Therefore the log file is kind of a mess. Here's an example: The logfile looks like: "DATE TIME - THREAD ID - Details", and a new file is created... (4 Replies)
Discussion started by: elinenbe
4 Replies

9. Shell Programming and Scripting

grep'ing for specific directories, and using the output to move files

Hello, this is probably another really simple tasks for most of you gurus, however I am trying to make a script which takes an input, greps a specific file for that input, prints back to screen the results (which are directory names) and then be able to use the directory names to move files.... (1 Reply)
Discussion started by: JayC89
1 Replies

10. Shell Programming and Scripting

Is it better to grep and pipe to awk, or to seach with awk itself

This may just be a lack of experience talking, but I always assumed that when possible it was better to use a commands built in abilities rather than to pipe to a bunch of commands. I wrote a (very simple) script a while back that was meant to pull out a certain error code, and report back what... (4 Replies)
Discussion started by: DeCoTwc
4 Replies
Login or Register to Ask a Question