LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-22-2017, 12:26 PM   #1
luftwaffe
LQ Newbie
 
Registered: Jul 2002
Posts: 19

Rep: Reputation: 0
Best way to parse this line in shell


Code:
line='Owner: C=US, O="Some Content, Inc.", OU=ABCDE12345, CN="Some Distribution: Some Content, Inc. (ABCDE12345)", UID=ABCDE12345'
Single quotes are not part of the string. The closest I could get is:
Code:
IFS="," read -ra splitted string <<< "$line"
The problem is that IFS does not tolerate double quotes and splits like:
${splitted[2]} == "O=Some Content"
${splitted[3]} == "Inc."

Ideally I would like to get a result like:
[0] Owner
[1] C=US
[2] O=Some Content, Inc.
[3] OU=ABCDE12345
[4] CN=Some Distribution: Some Content, Inc. (ABCDE12345)
[5] UID=ABCDE12345

I looked at awk and sed, and both cuts through the quoted line. Is there any idea / help on how to do it in just with standard shell tools? I have perl and python on this box too, just very unsure about syntax.

Thanks a lot

Last edited by luftwaffe; 06-22-2017 at 12:32 PM. Reason: adding code tags
 
Old 06-22-2017, 02:09 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
Blog Entries: 4

Rep: Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894
Will you have a lot of such lines to parse from a file?

What can you say about the format or structure? There is probably already a perl module at CPAN to parse it, if you can name it. It looks similar to LDAP.
 
Old 06-22-2017, 07:55 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,286

Rep: Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165
awk will do it with patsplit() - there is a good explanation with code under "Defining Fields by Content". The regex needs adjusting for what you want, but is achievable.
Your situation is a little more complex than a csv with double quotes as you appear to also want the colon as a field separator. As I said, a little regex fu and you are done.

Or go play in CPAN as Turbocapitalist suggested.
 
1 members found this post helpful.
Old 06-22-2017, 08:40 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,286

Rep: Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165
Some simple updates to the supplied code generated this - I note (now) that you don't want the double quotes. Easiest to probably post-process them.
Code:
[me@laptop awktst]$ echo $line | awk -f simpl.csv.awk 
NF =  6
$1 = <Owner>
$2 = <C=US>
$3 = <O="Some Content, Inc.">
$4 = <OU=ABCDE12345>
$5 = <CN="Some Distribution: Some Content, Inc. (ABCDE12345)">
$6 = <UID=ABCDE12345>
 
Old 06-25-2017, 06:16 PM   #5
sweepnine
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
Unless your are an experienced sed/awk-script-guy you will be better off with an existing parser.

Code:
apt-get install python-openssl
Code:
#!/usr/bin/python
import OpenSSL
path         = 'your_cert.crt'
file_content = open(path).read()
cert         = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, file_content)
subject_dict = dict(cert.get_subject().get_components())
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
PHP Parse error: syntax error, unexpected end of file in Command line code on line 1 pizzipie Programming 8 12-11-2014 01:49 PM
read a line from file , parse it and store in variables,print them using shell script swapnika.a60 Linux - Newbie 2 12-20-2012 08:20 AM
command line parse?? shanky Linux - Newbie 4 10-14-2011 01:45 AM
How to parse a line of text rkinch Linux - Newbie 19 05-01-2011 07:55 PM
Korn Shell Parse Something out of a Line xikspan Linux - Newbie 5 02-23-2009 02:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:01 PM.

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