Please help in sorting record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please help in sorting record
# 1  
Old 03-31-2010
Please help in sorting record

Code:
dn: uid=peter@exmaple.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Peter
sn: Norton
displayName: Peter Norton



dn: uid=ras@exmaple.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Ras
sn: Kam
displayName: Ras Kam

i have a text file [above text] with 300 entries with multiple ldap entries but i want to search specific entries like all users from @example.com email addres with their name as well, like

Code:
email:peter@example.com Name: Peter Norton
email:ras@example.com Name: Ras Kam


Thanks,

Last edited by vgersh99; 03-31-2010 at 01:04 PM.. Reason: code tags, PLEASE!
# 2  
Old 03-31-2010
like this?

Code:
awk -F "[ ,:=]" '/@exmaple.com/ {R=NR;ID=$4}NR==R+3{print "Email:"ID" Name:"$3,$4}' file

# 3  
Old 03-31-2010
Code:
$
$
$ cat f3
dn: uid=peter@example.com,ou=example-com...=example,dc=to
cn: Peter
sn: Norton
displayName: Peter Norton
 
dn: uid=ras@example.com,ou=example-com,o...=example,dc=to
cn: Ras
sn: Kam
displayName: Ras Kam
 
dn: uid=james@mi6.com,ou=example-com,ou=...=example,dc=to
cn: James
sn: Bond
displayName: James Bond
 
dn: uid=Bat@example.com,ou=example-com,o...=example,dc=to
cn: Bat
sn: Man
displayName: Bat Man
$
$
$ perl -lne 'if (/^.*?uid=(.*?\@example.com).*$/){$x="email:$1"}
>            elsif(/^displayName: (.*?)$/ && $x){print $x," Name: ",$1; $x=""}' f3
email:peter@example.com Name: Peter Norton
email:ras@example.com Name: Ras Kam
email:Bat@example.com Name: Bat Man
$
$

# 4  
Old 03-31-2010
Code:
sed -n '/^dn: uid=[^@]*@example\.com/{s/[^=]*=\([^,]*\).*/email:\1/; h; n; n; n; s/display//; H; g; y/\n/ /; p;}' file

email:peter@example.com Name: Peter Norton
email:ras@example.com Name: Ras Kam



---------- Post updated at 02:40 PM ---------- Previous update was at 02:30 PM ----------

Quote:
Originally Posted by anchal_khare
like this?

Code:
awk -F "[ ,:=]" '/@exmaple.com/ {R=NR;ID=$4}NR==R+3{print "Email:"ID" Name:"$3,$4}' file

This solution does not work correctly if a matching email address isn't found in the first 3 lines. If that's the case, an unset R evaluates to 0 and NR==R+3 will evalute to true when NR is 3. At that point, unwanted data may print.

Regards,
Alister
# 5  
Old 03-31-2010
Quote:
Originally Posted by durden_tyler
Code:
$
$
$ cat f3
dn: uid=peter@example.com,ou=example-com...=example,dc=to
cn: Peter
sn: Norton
displayName: Peter Norton
 
dn: uid=ras@example.com,ou=example-com,o...=example,dc=to
cn: Ras
sn: Kam
displayName: Ras Kam
 
dn: uid=james@mi6.com,ou=example-com,ou=...=example,dc=to
cn: James
sn: Bond
displayName: James Bond
 
dn: uid=Bat@example.com,ou=example-com,o...=example,dc=to
cn: Bat
sn: Man
displayName: Bat Man
$
$
$ perl -lne 'if (/^.*?uid=(.*?\@example.com).*$/){$x="email:$1"}
>            elsif(/^displayName: (.*?)$/ && $x){print $x," Name: ",$1; $x=""}' f3
email:peter@example.com Name: Peter Norton
email:ras@example.com Name: Ras Kam
email:Bat@example.com Name: Bat Man
$
$

Thanks so much, it is perfect, can you suggest how to sort my lines so All [a,A] shows first and [zZ] shows in last, i mean text orderwise on any column better on name basis.

Once again thanks.
# 6  
Old 04-01-2010
Quote:
Originally Posted by learnbash
... how to sort my lines so All [a,A] shows first and [zZ] shows in last, i mean text orderwise on any column better on name basis.
...
Code:
$
$
$ cat f3
dn: uid=peter@example.com,ou=example-com...=example,dc=to
cn: Peter
sn: Norton
displayName: Peter Norton
dn: uid=ras@example.com,ou=example-com,o...=example,dc=to
cn: Ras
sn: Kam
displayName: Ras Kam
dn: uid=james@mi6.com,ou=example-com,ou=...=example,dc=to
cn: James
sn: Bond
displayName: James Bond
dn: uid=Bat@example.com,ou=example-com,o...=example,dc=to
cn: Bat
sn: Man
displayName: Bat Man
$
$ ##
$ perl -lne 'if (/^.*?uid=(.*?\@example.com).*$/){$x="email:$1"}
>            elsif(/^displayName: (.*?)$/ && $x){push @a, "$x Name: $1"; $x=""}
>            END {print foreach (sort @a)}' f3
email:Bat@example.com Name: Bat Man
email:peter@example.com Name: Peter Norton
email:ras@example.com Name: Ras Kam
$
$

tyler_durden

Looks like you asked for a case-insensitive sort. Here's an idea -

Code:
$
$
$ cat f3
dn: uid=peter@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Peter
sn: Norton
displayName: Peter Norton

dn: uid=adam@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: adam
sn: smith
displayName: adam smith

dn: uid=james@mi6.com,ou=example-com,ou=mail,dc=example,dc=to
cn: James
sn: Bond
displayName: James Bond

dn: uid=Bat@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Bat
sn: Man
displayName: Bat Man
$
$
$ perl -lne 'if (/^.*?uid=(.*?\@example.com).*$/){$x="email:$1"}
>            elsif(/^displayName: (.*?)$/ && $x){push @y, "$x Name: $1"; $x=""}
>            END {print foreach (sort {uc($a) cmp uc($b)} @y)}' f3
email:adam@example.com Name: adam smith
email:Bat@example.com Name: Bat Man
email:peter@example.com Name: Peter Norton
$
$

The prior script, when run on this file will put "Bat Man" before "adam smith" because ascii('B') < ascii('a').

HTH,
tyler_durden

Last edited by durden_tyler; 04-01-2010 at 11:26 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need code for updating second record to first record in shell scripting

Hi,, I have requirement that i need to get DISTINCT values from a table and if there are two records i need to update it to one record and then need to submit INSERT statements by using the updated value as a parameter. Here is the example follows.. SELECT DISTINCT ID FROM OFFER_GROUP WHERE... (1 Reply)
Discussion started by: Samah
1 Replies

2. Shell Programming and Scripting

Replace a string for every record after the 1st record

I have data coming in the below format for each record <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox> <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox... (8 Replies)
Discussion started by: dsravanam
8 Replies

3. Shell Programming and Scripting

Sorting group of records and loading last record

Hi Everyone, I have below record set. File is fixed widht file 101newjersyus 20150110 101nboston us 20150103 102boston us 20140106 102boston us 20140103 I need to group record based on first 3 letters in our case(101 and 102) and sort last 8 digit in ascending order and print only... (4 Replies)
Discussion started by: patricjemmy6
4 Replies

4. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

5. Shell Programming and Scripting

Require original field position after sorting the values in a record

Dear Team, Can any body help me out to get the filed position of the records post sorting using AWK programming. Thanks in advance Example Input: StudentID col-1 col-2 col-3 col-4 1234 14 10 12 13 1235 10 11 12 13 1236 13 12 11 10 ... (3 Replies)
Discussion started by: Srinivasa Reddy
3 Replies

6. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

7. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

8. Shell Programming and Scripting

Sorting within a record using AWK

Hello, I have a file which has the following format: I have to do is sort individual records in the file based on the 4th field. Each record starts with "Module". Is there an easy way to do this using awk. I have tried piping output from awk to sort and also using "sort" inside awk but... (8 Replies)
Discussion started by: fifteate
8 Replies

9. Shell Programming and Scripting

Sorting record

Hi all, Can any one help whether we can able to sort a record with delimiter plz ? (3 Replies)
Discussion started by: thelakbe
3 Replies

10. UNIX for Advanced & Expert Users

Print Full record and substring in that record

I have i got a requirement like below. I have input file which contains following fixed width records. 00000000000088500232007112007111 I need the full record and concatenated with ~ and characters from 1to 5 and concatenated with ~ and charactes from 10 to 15 The out put will be like... (1 Reply)
Discussion started by: ukatru
1 Replies
Login or Register to Ask a Question