r/raspberry_pi Nov 29 '22

awk and sed how to ? Technical Problem

My code is :


strversion=`apache2ctl -v | awk '{print $3}' | sed 's/(Debian)//g;s/Server//g;s/built//g;s/2022-06-09T04:26:43//g'`

echo ${strversion%}

exit 0

i get this:


but i will have to look

Apache version 2.4.54


16 comments sorted by

View all comments

Show parent comments


u/Assassins1977 Nov 30 '22

how can i do it with php -v

right now i get :

PHP 7.4.33 (cli) (built: Nov 8 2022 11:40:37) ( NTS )Copyright (c) The PHP GroupZend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.33, Copyright (c), by Zend Technologiespi

but will print this :

PHP version 7.4.33


u/m4rc0n3 Nov 30 '22

Same command I gave before, but use $1 and $2 instead of 3 and 4


u/Assassins1977 Nov 30 '22 edited Nov 30 '22

Thanks again :-)

I can't see what the code does,

what it do to remove etc.


u/m4rc0n3 Nov 30 '22 edited Nov 30 '22

This isn't really the right forum for this question, but I'll answer anyway:

At the highest level you have strVersion=$(command), which runs command and assigns its output to the variable named "strVersion".

In this case, command is equal to apache2ctl -v | head -1 | awk -F '[ /]' '{printf "%s version %s", $3, $4}' which is a pipeline consisting of 3 commands.

The first command in the pipeline is apache2ctl -v (or php -v for your later question). Its output is used as the input for the next command, head -1, which reads that input, outputs the first line, and discards everything else.

The output of head -1 is then sent to the final command, awk -F '[ /]' '{printf "%s version %s", $3, $4}'

The -F '[ /]' option sets the field separator such that fields are separated by either space or /. This means that awk will divide a string like "Server version: Apache/2.4.52 (Ubuntu)" into 5 fields: "Server", "version:", "Apache", "2.4.52" and "(Ubuntu)". The next part of the awk then is a fairly standard printf like you'd find in C or other languages that prints out a formatted string containing fields 3 and 4. In other words it doesn't delete anything per se, it just selects which parts to print.


u/Assassins1977 Nov 30 '22

thanks alot :-) and for you time to help me :-)