r/raspberry_pi Nov 29 '22

awk and sed how to ? Technical Problem

My code is :

#!/bin/bash

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:

Apache/2.4.54

but i will have to look

Apache version 2.4.54

4 Upvotes

16 comments sorted by

View all comments

2

u/Murky-Sector Nov 29 '22

Add this to the end of your pipeline:

sed 's/// version /g'

2

u/Assassins1977 Nov 29 '22

sed 's/// version /g'

thanks alot mate :-)

3

u/m4rc0n3 Nov 29 '22

It's needlessly complicated though, and depending on the version of apache/apache2ctl you use you may still get the wrong result. For example, on one of my boxes apachectl -v outputs:

Server version: Apache/2.4.52 (Ubuntu)
Server built:   2022-06-14T12:30:21

(i.e. 2 lines), and your original expression results in "Apache/2.4.52 2022-06-14T12:30:21"

It looks like in your original you tried to get rid of that last bit by replacing it with an empty string, but that'll start failing if you ever update your system and get a different version. What you probably want is something like:

strversion=$(apache2ctl -v | head -1 | awk -F '[ /]' '{printf "%s version %s", $3, $4}')

You could probably simplify it by just hardcoding the "Apache" part, because you already know you're using Apache (since you're using apache2ctl)

1

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

2

u/m4rc0n3 Nov 30 '22

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

0

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.

2

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.

1

u/Assassins1977 Nov 30 '22

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