LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2020, 05:50 AM   #1
JugaruGabi
LQ Newbie
 
Registered: Jun 2019
Posts: 15

Rep: Reputation: Disabled
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.

How could I achieve these things?
 
Old 08-24-2020, 06:10 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
2 members found this post helpful.
Old 08-24-2020, 08:52 AM   #3
JugaruGabi
LQ Newbie
 
Registered: Jun 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
Hey,

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.
 
Old 08-24-2020, 08:57 AM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Doesn’t the code in #3 do what you want? Looks like it should.
 
Old 08-24-2020, 09:01 AM   #5
JugaruGabi
LQ Newbie
 
Registered: Jun 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
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?
 
Old 08-24-2020, 09:06 AM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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...
 
1 members found this post helpful.
Old 08-24-2020, 09:28 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
AWK can do most of it, with one call to date from inside the script.
 
1 members found this post helpful.
Old 08-24-2020, 10:15 AM   #8
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
If you want to use plain Bash, you can read the file into an array and manipulate the lines how you need.
Code:
#!/usr/bin/env bash

readarray lines <certsfile
count=${#lines[@]}

i=1
while [[ $i -lt $count ]]; do
    # extract the certificate name
    server="${lines[$((i-1))]}"
    server="${server%%,*}"
    server="${server##* }"

    # convert the date/time to date
    datetime="$(date +'%d-%m-%Y' <<<"${lines[$i]}")"

    echo "$server $datetime"
    i=$((i + 2))
done
 
Old 08-24-2020, 10:43 AM   #9
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
I'd probably do it in perl...but I'd have wanted to see some effort from the OP first.
 
Old 08-24-2020, 01:30 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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
 
Old 08-25-2020, 04:16 AM   #11
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
awk is not something I’ve yet learned...I probably should. I’m currently trying to get up to speed with CSS...amazing...
 
Old 08-25-2020, 05:45 AM   #12
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
Code:
 awk '/^Owner/ {printf "%s ",$2 ; next} ; {a=$10" "$11" "$12" "$13" "$14" "$15 ; print a}' server.log
 
2 members found this post helpful.
Old 08-31-2020, 01:12 AM   #13
JugaruGabi
LQ Newbie
 
Registered: Jun 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
I have managed to get it sorted out next day since the posting.
However, had something to do.

Solution is similar to what @syg00 suggested:

Code:
string=$(cat filename.txt)
echo -e ${string} | awk 'NR % 2 {print $2}
!(NR % 2) {
  args = $10" "$11" "$12" "$13" "$14" "$15
  cmd = "date -d \""args"\" \"+%d-%b-%Y\""
  cmd | getline expiry_date
  close(cmd)
  print expiry_date
}' > validitylist.txt
Thanks for helping out guys!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] read txt file into an array and make a second txt file zimbot Linux - General 12 09-05-2015 01:39 PM
Converting .txt file to a csv file kamesp Linux - Software 9 04-20-2013 07:45 AM
cut first 10 lines of file master.txt and paste in ab1.txt and so on yogeshkumkar Programming 4 08-31-2011 07:23 AM
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 06:38 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration