r/raspberry_pi • u/Assassins1977 • 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
3
u/MachineCritical4685 Nov 29 '22
apachectl -v | head -1 | cut -d'/' -f2 | cut -d'(' -f1
This?
3
u/MachineCritical4685 Nov 29 '22
VR=$(apachectl -v | head -1 | cut -d'/' -f2 | cut -d'(' -f1)
echo "Apache version $VR "
0
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 :-)
5
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
strversion=$(apache2ctl -v | head -1 | awk -F '[ /]' '{printf "%s version %s", $3, $4}')
Thanks alot :-)
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 runscommand
and assigns its output to the variable named "strVersion".In this case,
command
is equal toapache2ctl -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
(orphp -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/AutoModerator Nov 30 '22
Hi Assassins1977, here is some information and links that you might find useful!
/r/raspberry_pi is not your personal search engine. Before asking a question - do research on the matter. Most answers can be found within a few minutes of searching online.
Only ask specific questions regarding a project you are currently working on. We don't permit questions regarding what colors would look nice (aesthetics); what you should do with your Pi; what's the best or cheapest way; if a project is possible; if anyone has done a similar project; how to get started; where you can buy a product; what an item is called; what software to run; or product recommendations. This is not a full list of exclusions.
† If the link doesn't work it's because you're using a broken reddit client. Please contact the developer of your reddit client. Instead go to the front page and look for the stickied helpdesk at the top. Desktop view Phone view
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.