BayLife
BayLife

Reputation: 339

Extract password expire date

I want to get the password expire date from this output:

>chage -l dsi
Last password change                                    : Feb 05, 2020
Password expires                                        : May 05, 2020
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 90
Number of days of warning before password expires       : 7

I already got so far that i only see the both dates for "last password change" and "Password expires", but i don´t get it working to only get the "password expires" date.

 >chage -l dsi | cut -d ':' -f2 | head -n 2

 Feb 05, 2020
 May 05, 2020

How can I only get "May 05, 2020" to store it in a variable for further processing?

Thanks.

Upvotes: 0

Views: 725

Answers (4)

Ivan
Ivan

Reputation: 7267

And a variant with sed

chage -l dsi | sed -n '/Password expires/s/^.*: //p'

Upvotes: 0

Freddy
Freddy

Reputation: 4688

With awk:

chage -l dsi | awk -F':' '$1 ~ /^Password expires/{ print $2 }'

or if you want to get rid of the space character after the colon:

chage -l dsi | awk -F':' '$1 ~ /^Password expires/{ sub(/^[[:blank:]]/, "", $2); print $2 }'

Upvotes: 1

Poshi
Poshi

Reputation: 5762

There are several options. The one that follows your schema could be:

chage -l dsi | cut -d ':' -f2 | head -n 2 | tail -1

So you get the first two lines and then the last line (which is the second in the whole text).

I don't like this approach, as it is completely dependent on the position of the information. If the order changes, you will get a wrong answer. I would go for searching the piece of information you need and then extracting the date:

chage -l dsi | grep "Password expires" | cut -d ':' -f2

Upvotes: 1

Simon Doppler
Simon Doppler

Reputation: 2093

You could use grep to filter the line you want before parsing the output:

chage -l dsi | grep "Password expires" | cut -d ':' -f2

That way you always get only the row you want.

Upvotes: 1

Related Questions