2

I need to print only the 900 in this line: auth required pam_faillock.so preauth silent deny=3 unlock_time=604800 fail_interval=900

However, this line will not always be in this order.

I need to find out how to print the value after the =. I will need to do this for unlock_time and fail_interval

I have been searching all night for something that will work exactly for me and cannot find it. I have been toying around with sed and awk and have not nailed this down yet.

Som3guy
  • 57
  • 1
  • 9

4 Answers4

7

Let's define your string:

s='auth required pam_faillock.so preauth silent deny=3 unlock_time=604800 fail_interval=900'

Using awk:

$ printf %s "$s" | awk -F= '$1=="fail_interval"{print $2}' RS=' '
900

Or:

$ printf %s "$s" | awk -F= '$1=="unlock_time"{print $2}' RS=' '
604800

How it works

Awk divides its input into records. We tell it to use a space as the record separator. Each record is divided into fields. We tell awk to use = as the field separator. In more detail:

  • printf %s "$s"

    This prints the string. printf is safer than echo in cases where the string might begin with -.

  • -F=

    This tells awk to use = as the field separator.

  • $1=="fail_interval" {print $2}

    If the first field is fail_interval, then we tell awk to print the second field.

  • RS=' '

    This tells awk to use a space as the record separator.

John1024
  • 109,961
  • 14
  • 137
  • 171
0

You may use sed for this

Command

echo "...stuff.... unlock_time=604800 fail_interval=900" | sed -E '
s/^.*unlock_time=([[:digit:]]*).*fail_interval=([[:digit:]]*).*$/\1 \2/'

Output

604800 900

Notes

  • The (..) in sed is used for selections.
  • [[:digit:]]* or 0-9 is used to match any number of digits
  • The \1 and \2 is used to replace the matched stuff, in order.
sjsam
  • 21,411
  • 5
  • 55
  • 102
0

Try using this.

unlock_time=$(echo "auth required pam_faillock.so preauth silent deny=3 unlock_time=604800 fail_interval=900" | awk -F'unlock_time=' '{print $2}' | awk '{print $1}')

echo "$unlock_time"

fail_interval=$(echo "auth required pam_faillock.so preauth silent deny=3 unlock_time=604800 fail_interval=900" | awk -F'fail_interval=' '{print $2}' | awk '{print $1}')

echo "$fail_interval"
chepner
  • 497,756
  • 71
  • 530
  • 681
Faizan Younus
  • 793
  • 1
  • 8
  • 13
0

Given an input variable:

input="auth required pam_faillock.so preauth silent deny=3 unlock_time=604800 fail_interval=900"

With GNU grep:

$ grep -oP 'fail_interval=\K([0-9]*)' <<< "$input"
900
$ grep -oP 'unlock_time=\K([0-9]*)' <<< "$input"
604800
SLePort
  • 15,211
  • 3
  • 34
  • 44