S
S
SeoNk2017-03-26 07:48:48
bash
SeoNk, 2017-03-26 07:48:48

How to process a string in a bash script and pass the result to a variable?

The bash script has this command:
grep 'temporary password' /var/log/mysqld.log
It produces something like this:
2017-12-01T00:22:31.416107Z 1 [Note] A temporary password is generated for [email protected] : mqRfBU_3Xk>r
I want the script to extract the last 12 characters of this string and store them in a VAR variable.
I know how to do it in php and javascript but never wrote bash.
Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2017-03-26
@jcmvbkbc

line=`grep 'temporary password' /var/log/mysqld.log`
VAR=${line: -12}

S
Saboteur, 2017-03-26
@saboteur_kiev

VAR=`grep 'temporary password' /var/log/mysqld.log|grep -oP ".{12}$"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question