Nginx Version + AWK Not Working - awk

Today I've noticed that I can't use "awk" on "nginx -v".
I've tried running this command: nginx -v | awk -F/ '{print $2}'
This should of been output like this: nginx/1.4.3
But instead it gives me nginx version: nginx/1.4.3
Any idea why it would behave this way !?
Also you can't output it to file by running: nginx -v > file.txt

nginx must be writing that message to standard error, not standard output. If you want to pipe it, you have to redirect stderr to stdout:
nginx -v 2>&1 | awk -F/ '{print $2}'

Related

AWKing or GREPing brackets [ ]?

I've searched all over and couldn't find a solution.
How would I awk or grep the following:
$ mbimcli -d /dev/cdc-wdm0 -p --query-ip-configuration
[/dev/cdc-wdm0] IPv4 configuration available: 'address, gateway, dns'
IP [0]: '11.22.333.44/55'
Gateway: '14.13.198.4'
DNS [0]: '172.17.1.101'
DNS [1]: '172.17.1.102'
DNS [2]: '172.17.1.101'
DNS [3]: '172.17.1.102'
So that I end up with:
11.22.33.44/55
I've tried a bunch of different combinations with both grep and awk and couldn't find a solution.
Using cat file as I don't have mbimcli -d /dev/cdc-wdm0 -p --query-ip-configuration:
$ cat file | awk -F"'" '/IP \[/{print $2}'
11.22.333.44/55
$ cat file | awk -F"'" '/Gateway/{print $2}'
14.13.198.4
or maybe this is all you need if the output of that command always looks like the example you posted:
$ cat file | awk -v RS= -F"'" '{print $5}'
11.22.333.44/55
$ cat file | awk -v RS= -F"'" '{print $8}'
14.13.198.4
You can do this in a single awk:
mbimcli -d /dev/cdc-wdm0 -p --query-ip-configuration |
awk '$1 == "IP" {gsub(/\047/, "", $NF); print $NF}'
11.22.333.44/55
something like this:
grep '[0-9]' file_with_text | awk '{print $NF}
grep [0-9] only lines with numbers and pipe the output into awk.
The $NF will return the last element.
If you want only the line that has the /, just add it to grep [0-9]/.
Also, for a complete answer, you can pipe the output of the command into grep:
mbimcli -d /dev/cdc-wdm0 -p --query-ip-configuration | grep '[0-9]/' | awk '{print $NF}
I would harness tr for preprocessing and GNU AWK for processing, let file.txt content be
IP [0]: '11.22.333.44/55'
Gateway: '14.13.198.4'
DNS [0]: '172.17.1.101'
DNS [1]: '172.17.1.102'
DNS [2]: '172.17.1.101'
DNS [3]: '172.17.1.102'
then
cat file.txt | tr -d "'" | awk '/IP/{print $NF}'
output
11.22.333.44/55
Explanation: use tr to delete ' then awk to print last column ($NF) if row contain IP.
(tested in tr (GNU coreutils) 8.30 and GNU Awk 5.0.1)

How to insert argument in awk script?

I'm writing a shell script which shut down some services and trying to get its pid by using the following awk script.
However, this awk script can't get pid. What's wrong with that?
ps -ef | awk -v port_no=10080 '/[m]ilk.*port=port_no/{print $2}'
The result of ps -ef is like this:
username 13155 27705 0 16:06 pts/2 00:00:00 /home/username/.rbenv/versions/2.3.6/bin/ruby /home/username/.rbenv/versions/2.3.6/bin/milk web --no-browser --host=example.com --port=10080
This process is working with a different port argument as well, so I want to kill the process only working on port=10080.
The awk script below works fine, but when I specify the port no using awk -v like the above, it doesn't work well.
ps -ef | awk '/[m]ilk.*port=10080/{print $2}'
awk version: GNU Awk 4.0.2
The syntax for pattern matching with /../ does not work with variables in the regular expression. You need to use the ~ syntax for it.
awk -v port_no=10080 '$0 ~ "[m]ilk.*port="port_no{print $2}'
If you notice the regex carefully, the regex string on the r.h.s of ~ is under the double-quotes ".." except the variable name holding the port number which shouldn't be under quotes, for the expansion to happen.
This task is easily accomplished using pgrep:
$ pgrep -f '[m]ilk.*port=10080'
Have a look at man pgrep for details.

Grep / awk, match exact string

I need to find the ID of some container docker, but some containers have similar names:
$ docker images
REPOSITORY TAG IMAGE ID
app-node latest 620350b79c5a
app-node-temp latest 461c5143a985
If I run:
$ docker images | grep -w app-node-temp | awk -e '{print $3}'
461c5143a985
If I run instead:
$ docker images | grep -w app-node | awk -e '{print $3}'
620350b79c5a
461c5143a985
How can I match the exact name?
I'd say just use awk with exact string matching:
docker images | awk '$1 == "app-node" { print $3 }'
Dashes are considered non-word characters, so grep -w won't work when the difference is marked by a dash.
In context, grep '^app-node[[:space:]]' would work. It looks for the required name followed by a space.
Of course, grep | awk is an anti-pattern most of the time; it would be better to use:
docker images | awk '/^app-node[[:space:]]/ { print $3 }'
Or, an easier solution with awk again uses equality — as suggested by Tom Fenech in his answer:
for server in app-node app-node-temp
do
docker images | awk -v server="$server" '$1 == server { print $3 }'
…
done
If running docker images is too expensive, you can run it once and capture the output in a file and then scan the file. This shows how to pass a shell variable into the awk script.
The chances are the pipeline would be run to capture the container's image ID information:
image_id=$(docker images | awk -v server="$server" '$1 == server { print $3 }')
docker images -q is good for your case

Pipe continuous stream to another command

I wrote a program to analyze the log files in real time. I need to feed it with the IPs. It works fine with the command:
cat /var/log/apache2/access.log | awk '{print $1}' | ./my_program
Also, I can get the IPs in real time with the command:
tail -f /var/log/apache2/access.log | awk '{print $1}'
When I pipe it to my program, my program does not receive anything:
tail -f /var/log/apache2/access.log | awk '{print $1}' | ./my_program
It seems like a matter of buffering. Is there way of piping continuous stream to my program?
I found the ultimate solution to my buffering problem here.
The problem is that stdio is being buffered, ...

piping to awk hangs

I am trying to pipe tshark output to awk. The tshark command works fine on its own, and when piped to other programs such as cat, it works fine (real time printing of output). However, when piped to awk, it hangs and nothing happens.
sudo tshark -i eth0 -l -f "tcp" -R 'http.request.method=="GET"' -T fields -e ip.src -e ip.dst -e
tcp.srcport -e tcp.dstport -e tcp.seq -e tcp.ack | awk '{printf("mz -A %s -B %s -tcp \"s=%s sp=%s
dp=%s\"\n", $2, $1, $5, $4, $3)}'
Here is a simplier version:
sudo tshark -i eth0 -f "tcp" -R 'http.request.method=="GET"' | awk '{print $0}'
And to compare, the following works fine (although is not very useful):
sudo tshark -i eth0 -f "tcp" -R 'http.request.method=="GET"' | cat
Thanks in advance.
I had the same problem.
I have found some partial "solutions" that are not completely portable.
Some of them point to use the fflush() or flush() awk functions or -W interactive option
http://mywiki.wooledge.org/BashFAQ/009
I tried both and none works. So awk is not the appropriate command at all.
A few of them suggest to use gawk but it neither does the trick for me.
cut command has the same problem.
My solution: In my case I just needed to put --line-buffered in GREP and not touching awk command but in your case I would try:
sed -u
with the proper regular expression. For example:
sed -u 's_\(.*\) \(.*\) \(.*\) DIFF: \(.*\)_\3 \4_'
This expression gives you the 3rd and 4th columns separate by TAB (written with ctrl+v and TAB combination). With -u option you get unbuffered output and also you have -l option that gives you line buffered output.
I hope you find this answer useful although is late
Per our previous messages in comments, maybe it will work to force closing the input and emitting a linefeed.
sudo tshark -i eth0 -f "tcp" -R 'http.request.method=="GET"' ...... \
| {
awk '{print $0}'
printf "\n"
}
Note, no pipe between awk and printf.
I hope this helps.
I found the solution here https://superuser.com/questions/742238/piping-tail-f-into-awk (by John1024).
It says:
"You don't see it in real time because, for purposes of efficiency, pipes are buffered. tail -f has to fill up the buffer, typically 4 kB, before the output is passed to awk."
The proposed solutions is to use "unbuffer" or "stdbuf -o0" commands to disable buffering. It worked for me like this:
stdbuf -o0 tshark -i ens192 -f "ip" | awk '{print $0}'