UNIX for Beginners Questions & Answers

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Building hierarchy with the list

👤 Login to reply

    #1  
Old 10-04-2017
arunkumar_mca arunkumar_mca is offline
Registered User
 
Join Date: Oct 2004
Last Activity: 22 June 2018, 11:25 AM EDT
Posts: 416
Thanks: 81
Thanked 2 Times in 2 Posts
Building hierarchy with the list

Hi All,

Sorry for more question today. I am having a text file . Like below

Code:
704925680_TOTAL->MANUAL->TT IOR GSB
775116444_TOTAL->POO TO->TT
-572275295_TOTAL->MANUAL->MTO
-611408278_TOTAL->PRIE LEL
456690129_TOTAL->BTT TOO
475919266_TOTAL->MANUAL->COM
-172680236_TOTAL->BTT TOO->MTO
481202389_TOTAL->MANUAL->TT COM
655296953_TOTAL->POO TO->COLE
-434977163_TOTAL->POO TO->SPI
-766208436_TOTAL->MANUAL->TT 3W GSB
-1903630442_TOTAL->COLE
-541283452_TOTAL->MANUAL->CRT

These are the hierarchical data. But not on the hierarchy order. If you see all fall on total and after total the next data come in the file in the example above it is MANUAL. But the MANUAL data are not group and scattered. I am trying to bring these on the group. The data to be grouped always falls after -> . The example I have given is 3 level we may have 4 or level. The above files needs to be grouped as below



Code:
-766208436_TOTAL->MANUAL->TT 3W GSB
704925680_TOTAL->MANUAL->TT IOR GSB
-572275295_TOTAL->MANUAL->MTO
-541283452_TOTAL->MANUAL->CRT
481202389_TOTAL->MANUAL->TT COM
475919266_TOTAL->MANUAL->COM
-611408278_TOTAL->PRIE LEL
456690129_TOTAL->BTT TOO
-172680236_TOTAL->BTT TOO->MTO
775116444_TOTAL->POO TO->TT
655296953_TOTAL->POO TO->COLE
-434977163_TOTAL->POO TO->SPI
-1903630442_TOTAL->COLE

Is there any easy way to do in Perl or awk. I am thinking to do C code. Any help will be great.
Sponsored Links
    #2  
Old 10-04-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 July 2018, 12:24 PM EDT
Location: Aachen, Germany
Posts: 13,082
Thanks: 452
Thanked 4,017 Times in 3,693 Posts
Try
Code:
sort -t">" -k2 file
456690129_TOTAL->BTT TOO
-172680236_TOTAL->BTT TOO->MTO
-1903630442_TOTAL->COLE
475919266_TOTAL->MANUAL->COM
-541283452_TOTAL->MANUAL->CRT
-572275295_TOTAL->MANUAL->MTO
-766208436_TOTAL->MANUAL->TT 3W GSB
481202389_TOTAL->MANUAL->TT COM
704925680_TOTAL->MANUAL->TT IOR GSB
655296953_TOTAL->POO TO->COLE
-434977163_TOTAL->POO TO->SPI
775116444_TOTAL->POO TO->TT
-611408278_TOTAL->PRIE LEL

If this doesn't satisfy your needs, pls post why and how M(ANUAL) sorts before B(TT), and C(OLE) after P(OO).
The Following User Says Thank You to RudiC For This Useful Post:
arunkumar_mca (10-04-2017)
Sponsored Links
    #3  
Old 10-04-2017
arunkumar_mca arunkumar_mca is offline
Registered User
 
Join Date: Oct 2004
Last Activity: 22 June 2018, 11:25 AM EDT
Posts: 416
Thanks: 81
Thanked 2 Times in 2 Posts
Thanks that worked. I have the ID's too with delimited by ~. Tried with cut and sort it is sorting only the description. I need the whole record grouped by tag (Manual,POO TO etc)

Also one more question I have multiple level after > I can sort by each column and pipe it to next K2 right ?
Code:
179341~1704489~1704480~704925680_TOTAL->MANUAL->TT IOR GSB
179341~1704497~1704480~775116444_TOTAL->POO TO->TT
179341~1704505~1704480~-572275295_TOTAL->MANUAL->MTO
179341~1704507~1704480~-611408278_TOTAL->PRIE LEL
179341~1704509~1704480~456690129_TOTAL->BTT TOO
179341~1704481~1704480~475919266_TOTAL->MANUAL->COM

Expected output
Code:
179341~1704489~1704480~704925680_TOTAL->MANUAL->TT IOR GSB
179341~1704505~1704480~-572275295_TOTAL->MANUAL->MTO
179341~1704481~1704480~475919266_TOTAL->MANUAL->COM
179341~1704497~1704480~775116444_TOTAL->POO TO->TT
179341~1704507~1704480~-611408278_TOTAL->PRIE LEL
179341~1704509~1704480~456690129_TOTAL->BTT TOO


Last edited by arunkumar_mca; 10-04-2017 at 08:48 PM.. Reason: Added expected output
    #4  
Old 10-05-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 July 2018, 12:24 PM EDT
Location: Aachen, Germany
Posts: 13,082
Thanks: 452
Thanked 4,017 Times in 3,693 Posts
What do you mean by I can sort by each column and pipe it to next K2 ?
Sponsored Links
    #5  
Old 10-05-2017
arunkumar_mca arunkumar_mca is offline
Registered User
 
Join Date: Oct 2004
Last Activity: 22 June 2018, 11:25 AM EDT
Posts: 416
Thanks: 81
Thanked 2 Times in 2 Posts
Please ignore the statement.
Quote:
Also one more question I have multiple level after > I can sort by each column and pipe it to next K2 right ?
I was asking for below where it worked using sort -K4
Code:
TOT-COL-TOOT-SOF-KID
TOT-GEK-TOOT-SOF-MAN

Now the issue I am having is with the ID's as mentioned
Sponsored Links
    #6  
Old 10-05-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 July 2018, 12:24 PM EDT
Location: Aachen, Germany
Posts: 13,082
Thanks: 452
Thanked 4,017 Times in 3,693 Posts
Please be aware that a sort key by default stops at line end; to confine it to a single field, the stop position needs to be specified as well, e.g. -k4,4. Also note that it's a lower case k .
The Following User Says Thank You to RudiC For This Useful Post:
arunkumar_mca (10-05-2017)
Sponsored Links
    #7  
Old 10-05-2017
arunkumar_mca arunkumar_mca is offline
Registered User
 
Join Date: Oct 2004
Last Activity: 22 June 2018, 11:25 AM EDT
Posts: 416
Thanks: 81
Thanked 2 Times in 2 Posts
Thanks. You mean to say I can make the below worked with -k4,4

Original
Code:
179341~1704489~1704480~704925680_TOTAL->MANUAL->TT IOR GSB
179341~1704497~1704480~775116444_TOTAL->POO TO->TT
179341~1704505~1704480~-572275295_TOTAL->MANUAL->MTO
179341~1704507~1704480~-611408278_TOTAL->PRIE LEL
179341~1704509~1704480~456690129_TOTAL->BTT TOO
179341~1704481~1704480~475919266_TOTAL->MANUAL->COM

Expected output

Code:
Code:
179341~1704489~1704480~704925680_TOTAL->MANUAL->TT IOR GSB
179341~1704505~1704480~-572275295_TOTAL->MANUAL->MTO
179341~1704481~1704480~475919266_TOTAL->MANUAL->COM
179341~1704497~1704480~775116444_TOTAL->POO TO->TT
179341~1704507~1704480~-611408278_TOTAL->PRIE LEL
179341~1704509~1704480~456690129_TOTAL->BTT TOO

Output after execution. Not sure how can traverse through the ~ delimiter and sort the whole record
Code:
sort -t">" -k4,4  ttt
179341~1704481~1704480~475919266_TOTAL->MANUAL->COM
179341~1704497~1704480~775116444_TOTAL->POO TO->TT
179341~1704505~1704480~-572275295_TOTAL->MANUAL->MTO
179341~1704507~1704480~-611408278_TOTAL->PRIE LEL
179341~1704509~1704480~456690129_TOTAL->BTT TOO

Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rsync building file list/catalog path/location glev2005 UNIX for Advanced & Expert Users 1 01-11-2010 10:45 AM
building table from list f_o_555 Shell Programming and Scripting 2 12-15-2009 03:46 AM
Why are so many dirs used in solaris hierarchy? wrapster Solaris 1 06-19-2008 10:40 AM
Linux Filesystem Hierarchy tayyabq8 Tips and Tutorials 0 01-31-2007 02:55 AM



All times are GMT -4. The time now is 08:50 PM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?