LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 08-23-2011, 03:52 AM   #1
TsubasaJM
LQ Newbie
 
Registered: Aug 2011
Posts: 2

Rep: Reputation: Disabled
Manipulating data per paragraph


Hi there,

I'm still pretty novice to bash, but I have some experience in manipulating data per line by using the 'while read' structure and for example extracting some data with awk and such.

Now I have an ldif file, which has data in it per paragraph (separated by a newline). Now I want to extract data from it (different attributes in it) per ldif record, which is harder cause there is no 'read paragraph' of course.

Example, if I have an ldif file like this:

dn: uid=test1,cn=example,cn=dom
time: 20110822105940
modifyTimestamp: 20110822085944Z

dn: uid=test2,cn=example,cn=dom
time: 20110822105941
modifyTimestamp: 20110822085945Z

dn: uid=test3,cn=example,cn=dom
time: 20110822105942
modifyTimestamp: 20110822085946Z

I want to be able to do checks and compares of attributes in every record, like comparing time and modifyTimestamp, but I need to do it in all of the records (skip none) and I would like it generic enough so I'm able to use it in a for or while loop and, say, grep the data out of it.

I'm sure there are cool solutions for this in sed or awk and I hope I can learn from it how it's done and I can try to use it to my own liking.

Thanks in advance!
 
Old 08-23-2011, 09:10 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Well using awk you can set the record separator and that will then make each paragraph a record. You will then need to choose whether you want to split the data or use
the field separator ... it will depend on how uniform your data is. As a demo:
Code:
awk '{print NR}' RS="" file
With above example this will print from 1 to 3
 
Old 08-23-2011, 08:50 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Depends how complex these checks are, but I'd seriously consider using Perl; its very good at this sort of thing.
 
Old 08-24-2011, 04:31 AM   #4
TsubasaJM
LQ Newbie
 
Registered: Aug 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks all for replying.

grail: the awk option sounds quite useful but now I need to figure out how to get data out of it on a per record basis. So how would I use awk to enter a sort of while or for loop (or split it so I can do stuff on each record individually) and grab the time and modifyTimestamp attributes from each record?

chrism01: thanks for the tip but I suck even more at perl than I suck at bash
 
Old 08-24-2011, 05:13 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Here is my bible. Have a look at field separators and the split function.
 
  


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
[SOLVED] Insert paragraph at particular line sunilsagar Programming 4 07-27-2011 10:33 PM
Fill paragraph in Abiword ciden Linux - Newbie 1 09-30-2009 09:00 PM
manipulating ascii data tables questions will.flanagan Linux - Newbie 8 04-17-2009 09:36 PM
grep and print paragraph B-Boy Programming 2 08-11-2008 02:30 AM
LXer: Manipulating WPC Data through DAS with Eclipse LXer Syndicated Linux News 0 10-31-2006 07:33 AM


All times are GMT -5. The time now is 06:26 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration