Converting value to MB [bash] [closed] - bash

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have this code:
VAL1=`ps auxf | grep httpd | grep ^apache | grep -v grep | wc -l`
VAL2=`ps auxf | grep httpd | grep ^apache | grep -v grep | awk '{s+=$6} END {print s}'`
VAL3=`expr $VAL2 / $VAL1`
echo "servers.value $VAL3"
and then I have values like servers.value 63908. Tell me please, how can i get in in MB?

Divide the KiB by 1024 and you get MB:
VAL3=`expr $VAL2 / $VAL1 / 1024`
echo "servers.value $VAL3 MB"

To get RSS from VAL2 in MB just use:
VAL2=`ps auxf | grep httpd | grep ^apache | grep -v grep | awk '{s+=$6} END {print s/1014}'`

Related

From awk output, how to cut or trim characters in columns

At the moment
I want to trim .fmbi1a5nn9sp5o4qy3eyazeq5.eddvrl9sa8t448pb38vibj8ef: and .ilwio0k43fgqt4jqzyfadx19v: so the output take less space :)
First step:
docker ps --format "{{.Names}}: {{.Status}}" | sort -k1 | column -t
mon_node-exporter.fmbi1a5nn9sp5o4qy3eyazeq5.eddvrl9sa8t448pb38vibj8ef: Up 7 days
mon_prometheus.1.ilwio0k43fgqt4jqzyfadx19v: Up 7 days
I know
I can do something like:
docker ps --format "{{.Names}}: {{.Status}}" | sort -k1 | rev | cut -d"." -f2- | rev
mon_node-exporter.fmbi1a5nn9sp5o4qy3eyazeq5
mon_prometheus.1
The issue
is that I'm losing the other columns :-/
Idea
It would sound logical to do something like this (with awk) but it does not work. Any ideas?
docker ps --format "{{.Names}} : {{.Status}}" | sort -k1 | awk '{(print $1 | rev | cut -d"." -f2- | rev),$2,$3,$4,$5,$6}' | column -t
Thank you in advance!
P
to cut the last dot extension
$ docker ... | sort | awk '{sub(/\.[^.]*$/,"",$1)}1' file | column -t
mon_node-exporter.fmbi1a5nn9sp5o4qy3eyazeq5 Up 7 days
mon_prometheus.1 Up 7 days
or, delete anything longer than 20 chars after a dot.
$ ... | sed -e 's/\(\.[a-z0-9:]\{20,\}\)* / /' | column -t
mon_node-exporter Up 7 days
mon_prometheus.1 Up 7 days
Works! This trick will make my life so much easier.
(I removed file)
docker ps --format "{{.Names}}: {{.Status}}" | sort -k1 | awk '{sub(/\.[^.]*$/,"",$1)}1' | column -t;
mon_grafana.1 Up 24 hours
mon_node-exporter.fmbi1a5nn9sp5o4qy3eyazeq5 Up 23 hours
Question #2:
Now how would you proceed to cut the characters after the first dot?
Cheers!

Use SED in order to filter a file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I would like to use SED in order to filter a file and only get the id which is constituted of 3 numbers and the Domain (e.g.: google.com).
Original File:
451 [04/Jan/1997:03:35:55 +0100] http://www.netvibes.com
448 [04/Jan/1997:03:36:30 +0100] www.google.com:443
450 [04/Jan/1997:03:36:48 +0100] http://84.55.151.142:8080
452 [04/Jan/1997:03:36:51 +0100] http://127.0.0.1:9010
451 [04/Jan/1997:03:36:55 +0100] http://www.netvibes.com
453 [04/Jan/1997:03:37:10 +0100] api.del.icio.us:443
453 [04/Jan/1997:03:37:33 +0100] api.del.icio.us:443
448 [04/Jan/1997:03:37:34 +0100] www.google.com:443
Used SED commands : sed -e 's/\[[^]]*\]//g' -e 's/http:\/\///g' -e 's/www.//g' -e 's/^.com//g' -e 's/:[0-9]*//g'
Current Output:
451 netvibes.com
448 google.com
450 84.55.151.142
452 127.0.0.1
451 netvibes.com
453 api.del.icio.us
453 api.del.icio.us
448 google.com
Wished Output:
451 netvibes.com
448 google.com
451 netvibes.com
448 google.com
using grep
sed ... | grep -F '.com'
or
sed ... | grep '\.com$'
or with sed -n, using p to print match
sed -ne 's/\[[^]]*\]//g;s/http:\/\///g;s/www.//g;s/:[0-9]*//g;/.com$/p'
Expected you've lost api.del.icio.us in your wish output so:
cat testfile | awk '{print $1" "$NF}' | sed -r 's/http\:\/\/*//g;s/www\.//g' | awk -F: '{print $1}' | sed -r 's/([0-9]{1,3}) [0-9].*/\1 /g' | sed -r 's/[0-9]{3} $//g' | grep -v '^$' | uniq
If you needs only *.com domains, get it:
cat testfile | awk '{print $1" "$NF}' | sed -r 's/http://*//g;s/www.//g' | awk -F: '{print $1}' | sed -r 's/([0-9]{1,3}) [0-9].*/\1 /g' | sed -r 's/[0-9]{3} $//g' | grep -v '^$' | grep com | uniq
Here's one in awk:
$ awk 'match($NF,/[^\.]+\.[a-z]+($|:)/) {
print $1,substr($NF,RSTART,RLENGTH-($NF~/:[0-9]+/?1:0))
}' file
451 netvibes.com
448 google.com
451 netvibes.com
453 icio.us
453 icio.us
448 google.com
If you want just the .coms, replace [a-z]+ in the match regex with com.

Compute the total memory used by Docker containers in Bash

How to compute in one Bash command line the total memory used by Docker containers running on the local Docker engine?
I use the following command to compute the total memory used in MB.
docker stats --no-stream --format 'table {{.MemUsage}}' | sed 's/[A-Za-z]*//g' | awk '{sum += $1} END {print sum "MB"}'
or if any are larger than 1GiB
docker stats --no-stream --format 'table {{.MemUsage}}' | sed 's/\.\([0-9]*\)GiB/\1MiB/g' | sed 's/[A-Za-z]*//g' | awk '{sum += $1} END {print sum "MB"}'
tl;dr
docker stats --no-stream --format '{{.MemUsage}}' | awk '{print $1}' | sed 's/GiB/ * 1024/;s/MiB//;s/KiB/ \/ 1024/' | bc -l | awk '{s+=$1} END {print s}'
Breaking this down:
docker stats --no-stream --format '{{.MemUsage}}' - Get only the memory usage
awk '{print $1}' - Strip the total memory from each line
sed 's/GiB/ * 1024/;s/MiB//;s/KiB/ \/ 1024/' - Normalize values into MiB
bc -l - Run calculations
awk '{s+=$1} END {print s}' - Sum all lines
To get total memory regardless of container size --KiB, MiB, or GiB
docker stats --no-stream --format 'table {{.MemUsage}}' | sed -n '1!p' | cut -d '/' -f1 | sed 's/GiB/ * 1024 MiB/;s/MiB/ * 1024 KiB/;s/KiB/ * 1024/; s/$/ +\\/; $a0' | bc | numfmt --to=iec-i --suffix=B "$#"

How do i ordanate a column without ordanating a line? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
can someone explain me how do i do this?
I have a file that has three columns, and a lot of lines, and i want that the second column to be sorted in ascending order (it contains only numbers).
sample file;
7.31937 736 /tmp/ref13
7.3223 5373 /tmp/ref13
7.32816 768 /tmp/ref13
7.32955 5370 /tmp/ref10
I want to;
7.31937 736 /tmp/ref13
7.3223 768 /tmp/ref13
7.32816 5370 /tmp/ref13
7.32955 5373 /tmp/ref10
Thanks!!
you can try this ;
paste -d' ' <(awk '{print $1}' yourFile) <(awk '{print $2}' yourFile | sort -n) <(awk '{print $3}' yourFile)
or
awk '{print $2}' yourFile | sort -n | paste yourFile - | awk '{print $1"\t"$4"\t"$3}'
Eg:
user#host:/tmp$ cat t1
7.31937 736 /tmp/ref13
7.3223 5373 /tmp/ref13
7.32816 768 /tmp/ref13
7.32955 5370 /tmp/ref10
user#host:/tmp$ paste -d' ' <(awk '{print $1}' t1) <(awk '{print $2}' t1 | sort -n) <(awk '{print $3}' t1) | column -t
7.31937 736 /tmp/ref13
7.3223 768 /tmp/ref13
7.32816 5370 /tmp/ref13
7.32955 5373 /tmp/ref10

shell script to extract the name and IP address

Is there a way to use shell script to get only the name and net from the result as below:
Result
6cb7f14e-6466-4211-9a09-2b8e7ad92703 | name-erkoev4ja3rv | 2e3900ff36574cf9937d88223403da77 | ACTIVE | Running | net0=10.1.1.2; ing-net=10.1.1.3; net=10.1.1.4;
Expected Result
name-erkoev4ja3rv: 10.1.1.4
$ input="6cb7f14e-6466-4211-9a09-2b8e7ad92703 | name-erkoev4ja3rv | 2e3900ff36574cf9937d88223403da77 | ACTIVE | Running | net0=10.1.1.2; ing-net=10.1.1.3; net=10.1.1.4;"
$ echo "$input" | sed -E 's,^[^|]+ \| ([^ ]+).* net=([0-9.]+).*$,\1: \2,g'
name-erkoev4ja3rv: 10.1.1.4
echo "6cb7f14e-6466-4211-9a09-2b8e7ad92703 | name-erkoev4ja3rv | 2e3900ff36574cf9937d88223403da77 | ACTIVE | Running | net0=10.1.1.2; ing-net=10.1.1.3; net=10.1.1.4;" | awk -F ' ' '{print $3}{print $13}'
Does this satisfy your case?

Resources