Reputation: 73
I have this ldapsearch query:
ldapsearch -D cn=root -w password -b "o=ldaphost" "(failedAttempts>=3)" "cn=Users" "mail" "jobTitle" "lastLoginDate"
and results it produces is :
# insures, Users, C0001, C0001, Organisations, orgname
dn: cn=insures,cn=Users,o=C0001,o=C0001,cn=Organisations,o=ldap
mail: [email protected]
jobTitle:
lastLoginDate: 20090717182042Z
# mckeown, Users, C0002, C0002, Organisations, ldaphost
dn: cn=mccane,cn=Users,o=C0002,o=C0002,cn=Organisations,o=ldaphost
jobTitle: Director
lastLoginDate: 20120713103418Z
# satasa4, Users, C0003, C0003, Organisations, org
dn: cn=satasa4,cn=Users,o=C0003,o=C0003,cn=Organisations,o=org
mail: [email protected]
jobTitle:
lastLoginDate: 20140506104804Z
I need to format output for lastLoginDate to look human readable something like
lastLoginDate 20014-05-06.
How can I do this for multiple output lastLoginDate?
Upvotes: 1
Views: 235
Reputation: 340
Get the logindate into a variable and separate the date fields and pass it to DATE command.
echo $logindate | cut -b 1-8 | xargs date +"%Y-%m-%d" -d
However, as others mentioned, the logindate has timestamp information as well, so better approach would be to get the timestamp info as well.
paste -d ' ' <( echo $logindate | cut -b 1-8 | xargs date +"%Y-%m-%d" -d ) <( paste -d ':' <( echo $logindate | cut -b 9-10 ) <( echo $logindate | cut -b 11-12 ) <( echo $logindate | cut -b 13-14 ))
Upvotes: 0
Reputation: 174874
Seems like you want something like this,
$ ......... | sed 's/\(lastLoginDate: [0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)[^ ]*/\1-\2-\3/g'
Example:
$ echo 'lastLoginDate: 20140506104804Z' | sed 's/\(lastLoginDate: [0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)[^ ]*/\1-\2-\3/g'
lastLoginDate: 2014-05-06
$ echo 'lastLoginDate: 20140506104804Z' | sed 's/\(lastLoginDate: [0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)[^ ]*/\1-\2-\3 \4:\5/g'
lastLoginDate: 2014-05-06 10:48
Upvotes: 0
Reputation: 74705
For more advanced handling of the output format, you should consider parsing the time using something like the Time::Piece
core module in Perl:
perl -MTime::Piece -lane 'if (/^lastLoginDate/) {
$t=Time::Piece->strptime($F[1], "%Y%m%d%H%M%SZ");
print "Last login time: ", $t->strftime("%H:%M:%S, %F")
}' file
The -a
switch splits each line into the array @F
. If the line starts with "lastLoginDate", it parses the second field into a time object. You can then print out the result in whatever format you like. The output of the above example is:
Last login time: 18:20:42, 2009-07-17
Last login time: 10:34:18, 2012-07-13
Last login time: 10:48:04, 2014-05-06
The format specifiers for the parsing and output can be seen on the strftime
man page.
Upvotes: 0
Reputation: 30873
This should format the lastLoginDate lines like you want while keeping the remaining ones as is:
ldapsearch ... | awk '$1 == "lastLoginDate:" {
y=substr($2,1,4);
m=substr($2,5,2);
d=substr($2,7,2);
printf "%s %s-%s-%m\n", $1, y,m,d); next } 1'
Upvotes: 1
Reputation: 5092
You can try this sed command
sed -r '/lastLoginDate:/{s/([^:]+:) (.{4})(.{2})(.{2})(.*)/\1\2-\3-\4/g}' FileName
Upvotes: 1