0

hope you are doing fine

i want to get a substring of my string which has a format of : 18d6m2s and so.. all i'm interested in is getting only the number of days and if the string contains only minutes/seconds i want to get a value of null/zero ..

examples:

age=12d23m2s ... the needed output is 12
age=21m... the needed output is 0
age=22s... the needed output is 0

my solution was to use cut (i can't have a separate bash script and this would be the input for another command..)..

my solution:

 awk -F "d" '{print $1}' $6 

but this is only valid for strings with the d characters.. i want to set the strings which contains m/s only to zero/null..

how can i do that ?

doksha
  • 47
  • 1
  • 2
  • 8
  • What is the expected output for the sample data? Please, don't post it as comments or images but edit it to the original question. Thanks. – James Brown Jun 07 '21 at 12:58
  • 1
    i already provided the needed output in the first section of my question :) – doksha Jun 07 '21 at 12:59

4 Answers4

0

You can use parameter expansion:

for age in 12d23m2s 12d 12d1s 21m1s 22s ; do
    days=${age%d*}  # Remove everything starting from "d".
    if [[ $days == *[ms]* ]] ; then
        days=0
    fi
    echo $days
done
choroba
  • 231,213
  • 25
  • 204
  • 289
0

We can use the NF var to check if the delimiter (d) has been found, and print accordingly:

#!/bin/bash

awk -F "d" '{print (NF > 1) ? $1 : 0}' <<< '12d23m2s'
awk -F "d" '{print (NF > 1) ? $1 : 0}' <<< '21m'
awk -F "d" '{print (NF > 1) ? $1 : 0}' <<< '22'
12
0
0

Try it online!

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • this works fine but how can i assign this to a value ? here is the command and the command below should be assigned to the age here.. can you assist ? ` kubectl get pods -A | awk {'printf "%s %s %s %s\n", $1,$2,$4,$6'} | grep -E "Evicted" | while read line; do ns=$(echo $line | cut -d , -f 1); pod=$(echo $line | cut -d , -f 2); ag e=$(echo $line | cut -d, -f 6); if [[$age -gt 30]]; then kubectl delete pod --dry-run='server' -n $ns $pod; fi done ` – doksha Jun 07 '21 at 19:17
  • [Assign the result to a variable](https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash) `age="$(your-command)"`, then pass that variable to awk: `awk -F "d" '{print (NF > 1) ? $1 : 0}' <<< "$age"` – 0stone0 Jun 07 '21 at 19:20
  • would this be correct ? ` kubectl get pods -A | awk {'printf "%s %s %s %s\n", $1,$2,$4,$6'} | grep -E "Evicted" | while read line; do ns=$(echo $line | cut -d , -f 1); pod=$(echo $line | cut -d , -f 2); age=$(echo $line | cut -d, -f 6) | final_age=awk -F "d" '{print (NF > 1) ? $1 : 0}' <<< "$age"; if [[$final_age -gt 30]]; then kubectl delete pod --dry-run='server' -n $ns $pod; fi done ` – doksha Jun 07 '21 at 19:32
  • Not sure m8, please add the code to the original post, unreadable through comment. – 0stone0 Jun 07 '21 at 19:37
  • created another question, can you have alook please ? https://stackoverflow.com/questions/67877807/is-this-command-valid-for-deleting-pods – doksha Jun 07 '21 at 19:50
0

You could use a regular expression:

if [[ $age =~ ([0-9]+)d ]]; then
  echo "${BASH_REMATCH[1]}"
fi

Encapsulated into a function:

get_days() {
  [[ $1 =~ ([0-9]+)d ]] && echo "${BASH_REMATCH[1]}" || echo 0
}

for age in  12d23m2s  21m  22s; do
  days=$(get_days "$age")
  echo "days in $age => $days"
done
days in 12d23m2s => 12
days in 21m => 0
days in 22s => 0
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

Using perl one-liner, with positive lookaround to dig between = and d:

$ perl -ne '(/(?<==)(\d+)(?=d)/ && print "$1\n") || print "0\n";' file

Output:

12
0
0
James Brown
  • 36,089
  • 7
  • 43
  • 59