Send grepped tail output to netcat - unix

I am trying to run the following command, and nothing is getting sent to netcat
tail -F file.txt | grep test | nc host 9999
If I remove the grep, the tail successfully is followed and sent to netcat.
If I just run the following, data comes back, so I know that data should be getting sent to the nc pipe:
tail -F file.txt | grep test
Any ideas?
UPDATE
I added the following to unbuffer the piped output and nothing goes through:
tail -F file.txt | stdbuf -o0 grep test | nc host 9999
When I turn on line buffering, output is cut off
tail -F file.txt | grep --line-buffered test | nc host 9999
Where
workid: ID:ITEST_HGR1-EMS12103.1A156BB6CEB1:10F76E5D
is sent as
workid: ID:ITEST_HGR1-EMS12103.1A156BB6CEB1:10F7

You need to change the default buffering behavior of grep. If you're on a GNU grep, you can use grep --line-buffered, or try unbuffer.

Related

How can pipes and grep and wc be combined to just give a count of the phrase “syntax ok”

How can pipes and grep and wc be combined to just give a count of the phrase “syntax ok”
Something like the following…
cd /usr/IBMIHS/bin/ |
apachectl -t -f /usr/IBMIHS/conf/AAA/httpd.conf |
apachectl -t -f /usr/IBMIHS/conf/AAA/siteAA.conf |
grep "^Syntax OK" | wc
Simply via grouping commands with curly brackets, and use grep -c:
{
apachectl -t -f /usr/IBMIHS/conf/AAA/httpd.conf
apachectl -t -f /usr/IBMIHS/conf/AAA/siteAA.conf
} |& grep -c "Syntax OK"
From man grep
-c, --count
Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option (see below), count non-matching lines.

Using awk command and comparing with a command output in another machine

I am looking to check if a random UID on one machine is present on another machine and printing if it exists. I am pretty new to awk and have hit a roadblock.Here is how I am approaching the problem:
Pick a random line in /etc/passwd, get the 3rd column which is the UID; ssh to another machine , get the /etc/passwd contents ,check if the reference UID from the first machine is present in the 3rd column of any line and print it.
I am only able to reach up until the point where I get the reference UID. How do I use this value, ssh into another machine and compare if it exists:
shuf -n 1 /etc/passwd | awk '{print $3}' <the reference UID> <ssh 10.0.0.0> cat /etc/passwd <compare if reference UID is present>
Here's one way of doing that:
awk -vuid=$(shuf -n 1 /etc/passwd | awk -F: '{print $3}') -F: '$3 == uid' <( ssh host cat /etc/passwd)
Breaking it down:
This part stores uid : -vuid=$(shuf -n 1 /etc/passwd | awk -F: '{print $3}')
This will print the line if 3rd field matches our uid: '$3 == uid'
here we're treating output of ssh command as a file: <( ssh host cat /etc/passwd)

fetch the value of number of active threads in a process

I am trying to fetch the number of threads of a process in a UNIX using command line. After going through the man page of unix command, I learnt that following command:
ps -o nlwp <pid>
returns the number of threads spawned in a process.
Whenever i executed above command in unix, it returned:
NLWP
7
Now, I want to neglect NLWP and a space before 7.
That is I am just interested in a value, as I will be using it in a script, that I am writing for unit testing?
Is it possible to fetch only value, and neglect everything(Title NLWP, space)?
You can always use the --no-headers option in ps to get rid of the headers.
In that case, use awk to just print the first value:
ps --no-headers -o nlwp <pid> | awk '{print $1}'
Or tr to remove the spaces:
ps --no-headers -o nlwp <pid> | tr -d ' '
If --no-headers is not supported in your ps version, either of these make it:
ps -o nlwp <pid> | awk 'END {print $1}'
ps -o nlwp <pid> | tail -1 | tr -d' '

Suppress find & grep "cannot open" output

I was given this syntax by user phi
find . | awk '!/((\.jpeg)|(\.jpg)|(\.png))$/ {print $0;}' | xargs grep "B206"
I would like to suppress the output of grep: can't open..... and find: cannot open lines from the results.sample output to be ignored:
grep: can't open ./cisc/.xdbhist
find: cannot open ./cisc/.ssh
Have you tried redirecting stderr to /dev/null ?
2>/dev/null
So the above redirects stream no.2 (which is stderr) to /dev/null. That's shell dependent, but the above should work for most. Because find and grep are different processes, you may have to do it for both, or (perhaps) execute in a subshell. e.g.
find ... 2>/dev/null | xargs grep ... 2>/dev/null
Here's a reference to some documentation on bash redirection. Unless you're using csh, this should work for most.
The option flag grep -s will suppress these messages for the grep command

Kill respawing server running on port 3000 created by `node . > /dev/null 2> /dev/null < /dev/null &`

I'm using AWS CodeDeploy in which server running on pm2 dose not work due to explanation given here in troubleShoot documentation.
I followed the documentation and in AfterInstall script used node . > /dev/null 2> /dev/null < /dev/null & to run the node server in the background.
I've tried following ways to kill the server
fuser -k 3000/tcp
lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9
kill -9 $(lsof -t -i:3000)
but each time a new process respwans with a different PID.
How can I kill this background process and add it to the ApplicationStop script for CodeDeploy?
One of the problems with finding a pid with grep is that the grep pid will also show up as a result and can kill itself before the target, so try;
ps ax | grep node | grep -v grep
if it looks reasonable, review this;
ps ax | grep node | grep -v grep | awk '{print $1}'
then run the kill;
ps ax | grep node | grep -v grep | awk '{print $1}' | xargs kill -9
pkill is a less flexible option (no regex filtering) but if you use that be sure to use the -I flag so you don't kill anything you did not intend to.
I was able to kill using pkill node command.

Resources