Sort data by the end of each line using BASH.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort data by the end of each line using BASH.
# 1  
Old 10-27-2010
Sort data by the end of each line using BASH.

I am trying to sort data within a text document by the information at the end of each line. Please see below for an example:

Code:
<Profile_0 Name="Random name 0" Description="This is the description." Category="System" ProfileFlags.DWD="6" ABCD="{FF350E61-4FFF-4600-BFFF-3B27DD4BA746}"/>
<Profile_1 Name="Random name 1" Description="This is the description. More words are being added." Category="System" ProfileFlags.DWD="6" ABCD="{73F11780-FFFF-420A-B0FC-FFFFF62B2AA0}"/>
<Profile_2 Name="Random name 2" Description="This is the description. Even more words are being added." Category="System" ProfileFlags.DWD="6" ABCD="{4247D9B0-5E89-FFFF-B2C3-FFFF4771BC4C}"/>
<Profile_3 Name="Random name 3" Description="This is the description." Category="System" ProfileFlags.DWD="6" ABCD="{EAA268CD-42F1-4B49-B7A9-608FFFF56748}"/>
<Profile_4 Name="Random name 4 with more words" Description="This is the description." Category="System" ProfileFlags.DWD="6" ABCD="{26BFFFF1-FFFF-4D3C-FFFF-608D23162123}"/>
<Profile_5 Name="Random name 5" Description="Description." Category="System" ProfileFlags.DWD="6" ABCD="{99BFFFF1-FFFF-4D3C-B7A9-608D23162123}"/>

My desired output is to re-order the lines alphabetically based on the information within the ABCD tag.

Code:
<Profile_4 Name="Random name 4 with more words" Description="This is the description." Category="System" ProfileFlags.DWD="6" ABCD="{26BFFFF1-FFFF-4D3C-FFFF-608D23162123}"/>
<Profile_2 Name="Random name 2" Description="This is the description. Even more words are being added." Category="System" ProfileFlags.DWD="6" ABCD="{4247D9B0-5E89-FFFF-B2C3-FFFF4771BC4C}"/>
<Profile_1 Name="Random name 1" Description="This is the description. More words are being added." Category="System" ProfileFlags.DWD="6" ABCD="{73F11780-FFFF-420A-B0FC-FFFFF62B2AA0}"/>
<Profile_5 Name="Random name 5" Description="Description." Category="System" ProfileFlags.DWD="6" ABCD="{99BFFFF1-FFFF-4D3C-B7A9-608D23162123}"/>
<Profile_3 Name="Random name 3" Description="This is the description." Category="System" ProfileFlags.DWD="6" ABCD="{EAA268CD-42F1-4B49-B7A9-608FFFF56748}"/>
<Profile_0 Name="Random name 0" Description="This is the description." Category="System" ProfileFlags.DWD="6" ABCD="{FF350E61-4FFF-4600-BFFF-3B27DD4BA746}"/>

I would like to accomplish this with regular BASH expressions. Any thoughts?
# 2  
Old 10-27-2010
Code:
sort -t'(' +1

or a bubble sort in an array.
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 10-27-2010
Code:
nawk -F'[{}]' '{print $(NF-1),$0}' myFile |sort | cut -d' ' -f2-

# 4  
Old 10-28-2010
With ksh (may need ksh93 on some systems):

Code:
typeset -A a
while read x; do a[${x##*\{}]=$x; done
set -s -- "${!a[@]}"
for i; do echo "${a[$i]}"; done

In bash4, you can use associative array. But bash built-in set doesn't have the "-s" option.
# 5  
Old 10-28-2010
If the simple sort line offends your need for smoke and mirrors, I am sure PERL and other real languages have tree and other ordered containers that sort what is inserted (some containers are hash order or unsorted).

I was thinking they should be lower latency than sort, which does traditional sorting: using memory to build maximum length sequences of sorted data in a series of temp files and then merging them. A container sorts as each item is inserted, which lacks economy of scale and serial speed, but as soon as the input is closed, output can start! Smilie
# 6  
Old 10-28-2010
Wow, thanks for all the great replies. For now, I think the first reply from DGPickett with the simple sort command will work for me, however I will stash the other examples away for when that will make more sense. Smilie
# 7  
Old 10-28-2010
Just be careful a wandering ( does not destabilize it! Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. BSD

Keep getting error "-bash: ./.profile_z2: line 52: syntax error: unexpected end of file"

#!/bin/bash #-------------------------------------------------------- # Setup prompt # Author Zeeshan Mirza # Data: 06-08-2017 #-------------------------------------------------------- if then . ./.profile_custom_pre fi umask 022 set -o vi export EDITOR=vi export VISUAL=vi... (3 Replies)
Discussion started by: getzeeshan
3 Replies

2. UNIX for Beginners Questions & Answers

UNIX help adding data to end of a line with a variable

I am trying to add a date variable to the end of each line. This is what I have to start with cat ${DATAPATH}/Participate_Stream${STREAMDATE}.dryak1.csv | grep ^',' | awk '{print $0}' >> ${DATAPATH}/badparticipant.csv This is what I tried $DATE is a variable I have defined. cat... (3 Replies)
Discussion started by: req62861
3 Replies

3. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

4. Shell Programming and Scripting

To append new data at the end of each line based on substring of last column

Hi guys, I need to append new data at the end of each line of the files. This new data is based on substring (3rd fields) of last column. Input file xxx.csv: U1234|1-5X|orange|1-5X|Act|1-5X|0.1 /sac/orange 12345 0 U5678|1-7X|grape|1-7X|Act|1-7X|0.1 /sac/grape 5678 0... (5 Replies)
Discussion started by: null7
5 Replies

5. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

6. Shell Programming and Scripting

BASH: Sort four lines based on first line

I am in the process of sorting an AutoHotkey script's contents so as to make it easier for me to find and view its nearly 200 buzzwords (when I forget which one corresponds with what phrase, which I do now and then). About half to two-thirds of the script's key phrases correspond to locations... (7 Replies)
Discussion started by: SilversleevesX
7 Replies

7. Shell Programming and Scripting

Appending data to the end of a line

I have searched the forms and I can not find info on appending each line of one file to the same line of another file. I know that I can cat one file to another or append the 2nd file to the end of the 1st but not quite sure how to append one line of data to another. For example File 1 has ... (2 Replies)
Discussion started by: scw132
2 Replies

8. Shell Programming and Scripting

Sort from start index and end index in line

Hi All, I have a file (FileNames.txt) which contains the following data in it. $ cat FileNames.txt MYFILE17XXX208Sep191307.csv MYFILE19XXX208Sep192124.csv MYFILE20XXX208Sep192418.csv MYFILE22XXX208Sep193234.csv MYFILE21XXX208Sep193018.csv MYFILE24XXX208Sep194053.csv... (5 Replies)
Discussion started by: krish_indus
5 Replies

9. UNIX for Dummies Questions & Answers

add data from command line to end of file

how can I add data from command line to end of file? (3 Replies)
Discussion started by: bryan
3 Replies

10. UNIX for Dummies Questions & Answers

End of line character in BAsh

I'm trying to match the end of line character in a shell script under bash. What I have done it got is a loop that reads each line of a file into a temp variable and then I get each letter of that variable and do some work on it. One think I want to do it check if the character I checking if the... (1 Reply)
Discussion started by: Rukshan
1 Replies
Login or Register to Ask a Question