On a Linux system I have some output like this:
Subject = CN=User_A,OU=users
Status = Valid Kind = IKE Serial = 98505 DP = 9
Not_Before: Wed Jun 15 13:53:55 2022 Not_After: Sun Jun 25 08:25:20 2023
Subject = CN=User_B,OU=users
Status = Valid Kind = IKE Serial = 98934 DP = 8
Not_Before: Sun Apr 18 18:24:16 2021 Not_After: Fri Apr 21 18:24:16 2023
I can use | grep 2022 | grep Jun to find certain data, but how can get Subject line in the output? I need to get the username whose certificate is about to expire ) Something like "Show me the Subject if "grep 2022 | grep Jun"".
Thank you in advance!
CodePudding user response:
What about this:
grep -B 2 "2022" test.txt | grep -o "CN=[A-Za-z0-1_]*" | cut -c 4-
grep -B 2 // show the matching line and two lines before too.
[A-Za-z0-1_]* // any character, being letters, digits and an underscore
grep -o "CN=[...]"
// show only the part, containing "CN=", followed by ...
cut -c 4- // instead of "CN=...", only show "..." (starting at 4th character)
CodePudding user response:
Using grep
$ grep -m2 -e '^Subject' -e 'Jun' -e '2022' input_file
Subject = CN=User_A,OU=users
Not_Before: Wed Jun 15 13:53:55 2022 Not_After: Sun Jun 25 08:25:20 2023
Using sed
$ sed -n '/^Subject/{p;:a;n;/Jun\|2022/p;ba}' input_file
Subject = CN=User_A,OU=users
Not_Before: Wed Jun 15 13:53:55 2022 Not_After: Sun Jun 25 08:25:20 2023