[SOLVED] Query about trimming and converting date from txt file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Query about trimming and converting date from txt file
Hello,
So I am facing the following situation.
A txt file contains the following information:
Code:
Owner: CN=servername28, O=YourSociety, DC=host, DC=com
Valid from: Fri Jul 10 21:52:55 CEST 2020 until: Sat Jan 16 18:00:00 CET 2021
Owner: CN=servernameA29, O=YourSociety, DC=host, DC=com
Valid from: Fri Jul 10 21:52:49 CEST 2020 until: Sat Jan 16 18:00:00 CET 2021
Owner: CN=servernameB30, O=YourSociety, DC=host, DC=com
Valid from: Fri Jul 10 21:52:58 CEST 2020 until: Sat Jan 16 18:00:00 CET 2021
Owner: CN=servernameA31, O=YourSociety, DC=host, DC=com
Valid from: Fri Jul 10 21:43:34 CEST 2020 until: Sat Jan 16 18:00:00 CET 2021
Owner: CN=servernameB32, O=YourSociety, DC=host, DC=com
Valid from: Fri Jul 10 21:46:36 CEST 2020 until: Sat Jan 16 18:00:00 CET 2021
Now, I need to perform the following actions:
1. For each occurrence I just need to keep only "CN=servernameA/BXX, "
2. Remove the Valid from: <full date> until
3. Convert date from until from Sat Jan 16 18:00:00 CET 2021 to 16-01-2021
4. Move the resulted converted date after the "CN=servernameA/BXX, " on line above.
Am using a monitoring tool that reads the information from the txt file which is formatted this way and sends out notifications based on the expiration dates.
Read a record, remove unwanted data, save good data - read next record, do likewise; print final record after converting date. Pick a language you are comfortable with.
This is your problem not ours, make an effort. We'll help where we can if you run into roadblocks.
I would love to get this sorted out using bash but I don't know how to achieve what I need. While for the rest I digged and sorted out in the meantime, the biggest problem is that I have no idea how to convert the date "until: Sat Jan 16 18:00:00 CET 2021" to 16-Jan-2021 in all that big file automatically.
This is because I have no idea how to define the "until: long date" as parameters to all my 72 servers automatically and not have to manually edit and define each server's expiration date similar to:
Code:
a='Sat Jan 16 18:00:00 CET 2021'
so I can run:
Code:
b=`date --date="$a" '+%d-%b-%Y'`
echo $b
Long story short, I am using the keytool command to pull the records regarding the expiration date of the certs from my servers into one big txt file.
This file is processed and trimmed to get the desired result.
But I have no idea how to define the "until date" as parameter for each line and to have the command mentioned above for the desired date format.
The single command works.
But how do I pass that date automatically as parameter $a in the big text file for each server?
So I am able to issue the "echo $b" command using a script and have the desired date format in the final form of the txt file?
Parse the date from the input into $a, do the conversion to $b, use $b in the output for each line.
You have already managed getting the CN part, right? Looks like you need to read two lines in for each one line out...
I second awk as probably the easiest and most flexible tool for exactly this kind of text manipulation, especially with multi-line records. Consider using ^Owner as the record separator, then the number of fields appears consistent and the problem immediately becomes a matter of selecting and transforming indexed fields into the output stream.
That said, bash can do it easily enough if you prefer to do it that way, suggest using array structures as others have said. The key as always is to organize the transformation tasks into well defined individual parts, testing them in isolation, then build the final result from those pieces.
The call to date can perform the date translation from either bash or awk, but you can do that with a simple array based operation, too, if desired.
As others have indicated, most members are more willing to help you solve problems with your own efforts than to simply produce a working solution to order. Give each part of the puzzle your best effort and show what you have tried and cannot get working. That helps you understand each problem more clearly and also helps those offering help to offer more specific suggestions consistent with your own chosen approach.
Good luck!
Last edited by astrogeek; 08-24-2020 at 01:35 PM.
Reason: grammar, typos
Something simple to p'raps help kick things off - date needs managing ... Relies on very specific records, which is generally to be avoided, but done for simplicity.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.