- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- uniq sort and group
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-19-2005 10:48 PM
тАО08-19-2005 10:48 PM
I have a text file like below which has several hundred lines.
createuser ba026860 ac50003000
createuser na027483 ae50007009
createuser rax78895 af60003001
createuser tmxr8983 af50003001
The 3rd field in 3rd and 4th line is same. I want to sort it on the third field first, and print the output of the third field as uniq (grouping); like below (if the 3rd field has the same value; then group and print only one time as below:
createuser ba026860 ac50003000
createuser na027483 ae50007009
createuser rax78895 af60003001
createuser tmxr8983
Please advise.
Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-20-2005 02:33 AM
тАО08-20-2005 02:33 AM
Re: uniq sort and group
In order to sort a file by 3rd field you can use the command:
sort -k 3 filename
For the desired output you can use awk:
sort -k 3 filename| awk '{if($3!=prev){prev=$3;print}}'
HTH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-20-2005 02:34 AM
тАО08-20-2005 02:34 AM
Re: uniq sort and group
Sort the file on the third fild first.
Then, use the command "uniq -f 2
The option "-f 2" ignores the first two filds when comparing the lines. Else the entire line is compared.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-20-2005 04:36 PM
тАО08-20-2005 04:36 PM
Re: uniq sort and group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-20-2005 05:46 PM
тАО08-20-2005 05:46 PM
Re: uniq sort and group
createuser ba026860 ac50003000
createuser na027483 ae50007009
createuser rax78895 af50003001
createuser tmxr8983 af50003001
The 3rd field in the last two lines are same.
So I need the result to come as below:
createuser ba026860 ac50003000
createuser na027483 ae50007009
createuser rax78895 af50003001
createuser tmxr8983
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-20-2005 06:22 PM
тАО08-20-2005 06:22 PM
Re: uniq sort and group
Minor tweak on Victor's reply gets the desired output:
sort -k 3 your-file | awk '{x=($3==old)? "" : $3; old=$3; print $1,$2,x}'
For more complex options you may want to use a perl frame work and then tweak that.
For example:
-------------------- group.p ---------
%lines = <>;
sub sort_3_2 {
($a1,$a2,$a3) = split(/\s+/,$a);
($b1,$b2,$b3) = split(/\s+/,$b);
$a3.$a2 cmp $b3.$b2
}
foreach (sort sort_3_2 %lines) {
($a1,$a2,$a3) = split;
$x3 = ($a3 eq $old)? "" : $a3;
print "$a1 $a2 $x3\n";
$old = $a3;
}
--------------------------------
perl group.p your-file
hth,
Hein.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-20-2005 09:37 PM
тАО08-20-2005 09:37 PM
Re: uniq sort and group
The awk one does not work. The perl one works. But I cannot implement here as I do not know perl.
Can you please check the awk on.
This is the error I get form the awk.
awk: syntax error near line 1
awk: illegal statement near line 1
thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2005 02:54 AM
тАО08-21-2005 02:54 AM
Re: uniq sort and group
Hmmm, the awk one really seems to work for me.
Did you re-type or cut & paste (and change the file name)
% uname -a
HP-UX hpgsp05c B.11.11 U 9000/800 690339353 unlimited-user license
% cat x
createuser ba026860 ac50003000
createuser na027483 ae50007009
createuser rax78895 af50003001
createuser tmxr8983 af50003001
createuser ba026861 ac50003000
createuser na027483 ae50007009
createuser na027485 ae50007009
createuser na027484 ae50007009
createuser rax78895 af50003001
createuser tmxr8983 af50003001
% sort -k 3 x | awk '{x=($3==old)? "" : $3; old=$3; print $1,$2,x}'
createuser ba026860 ac50003000
createuser ba026861
createuser na027483 ae50007009
createuser na027483
createuser na027484
createuser na027485
createuser rax78895 af50003001
createuser rax78895
createuser tmxr8983
createuser tmxr8983
fwiw,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-21-2005 05:31 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2005 04:51 PM
тАО08-22-2005 04:51 PM
Re: uniq sort and group
Yashy.