sort entire line based on part of the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort entire line based on part of the string
# 1  
Old 07-31-2008
sort entire line based on part of the string

hey gurus,

my-build1-abc
my-build10-abc
my-build2-abc
my-build22-abc
my-build3-abc

basically i want to numerically sort the entire lines based on the build number. I dont zero pad the numbers because thats "how it is" ;-)

sort -n won't work because it starts from the beginning.

so how would you do that in a one-liner. sed/awk maybe?

Cheers
# 2  
Old 07-31-2008
Code:
sort -n -k1.9 inputfile

Code:
> cat gurpal.dat
my-build1-abc
my-build10-abc
my-build2-abc
my-build22-abc
my-build3-abc
> sort -n -k1.9 gurpal.dat
my-build1-abc
my-build2-abc
my-build3-abc
my-build10-abc
my-build22-abc
>

Jean-Pierre.
# 3  
Old 07-31-2008
Quote:
Originally Posted by aigles
Code:
sort -n -k1.9 inputfile

Code:
> cat gurpal.dat
my-build1-abc
my-build10-abc
my-build2-abc
my-build22-abc
my-build3-abc
> sort -n -k1.9 gurpal.dat
my-build1-abc
my-build2-abc
my-build3-abc
my-build10-abc
my-build22-abc
>

Jean-Pierre.
great! so does the 1.9 mean start at position 9? what if it is variable? do we need some regex?

thanks
# 4  
Old 07-31-2008
ok so here is the real question. Essentially i'm listing labels from source control and format looks like

project_subproject_major.minor.build

example

myproject_abcdef_0.0.1
myproject_abcdef_0.0.2
myproject_abcdef_0.0.9
myproject_abcdef_0.0.10
myproject_abcdef_1.1.1
myproject_abcdef_1.1.10
myproject_abcdef_1.1.2
myproject_abcdef_1.0.1
myproject_abcdef_1.0.10

the major.minor.build numbers can be any magnitude. How does sort -k deal with things like this? So the output should be the same text but ordered numerically by major.minor.build.

That would be excellent if you could hint ;-)
# 5  
Old 07-31-2008
I haven't found a simplest solution :
Code:
awk -F_ '{v=$NF; gsub(/\./," ",v) ; print v,$0}' inputfile |
sort -n  -k1,1 -k2,2 -k3,3 | 
cut -d' ' -f4-

Inputfile:
Code:
myproject_abcdef_0.0.1
myproject_abcdef_0.0.2
myproject_abcdef_0.0.9
myproject_abcdef_0.0.10
myproject_abcdef_1.1.1
myproject_abcdef_1.1.10
myproject_abcdef_1.1.2
myproject_abcdef_1.0.1
myproject_abcdef_1.0.10

Output:
Code:
myproject_abcdef_0.0.1
myproject_abcdef_0.0.2
myproject_abcdef_0.0.9
myproject_abcdef_0.0.10
myproject_abcdef_1.0.1
myproject_abcdef_1.0.10
myproject_abcdef_1.1.1
myproject_abcdef_1.1.2
myproject_abcdef_1.1.10

Jean-Pierre.
# 6  
Old 07-31-2008
Quote:
Originally Posted by aigles
I haven't found a simplest solution :
Code:
awk -F_ '{v=$NF; gsub(/\./," ",v) ; print v,$0}' inputfile |
sort -n  -k1,1 -k2,2 -k3,3 | 
cut -d' ' -f4-

Inputfile:
Code:
myproject_abcdef_0.0.1
myproject_abcdef_0.0.2
myproject_abcdef_0.0.9
myproject_abcdef_0.0.10
myproject_abcdef_1.1.1
myproject_abcdef_1.1.10
myproject_abcdef_1.1.2
myproject_abcdef_1.0.1
myproject_abcdef_1.0.10

Output:
Code:
myproject_abcdef_0.0.1
myproject_abcdef_0.0.2
myproject_abcdef_0.0.9
myproject_abcdef_0.0.10
myproject_abcdef_1.0.1
myproject_abcdef_1.0.10
myproject_abcdef_1.1.1
myproject_abcdef_1.1.2
myproject_abcdef_1.1.10

Jean-Pierre.
now THAT is clever!
# 7  
Old 08-01-2008
The sort option -K

Hello aigles,
Could you elaborate that the option of sort -k?
I mean in your two solutions, the first one used -k1.9 and the second one used the -k1,1, are there any difference between them? I can't sort the file using -k1,9 in the first example.

And In the manual of sort, the option K should take the comma not the dot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Avoid printing entire line if string not found

so im searching the process table with: ps -ef | awk -F"./rello.java" '{ print substr($0, index($0,$2)) }' I only want it to print everything that's infront of the "./rello.java". That's because im basically getting the arguments that was passed to the rello.java script. this works. ... (2 Replies)
Discussion started by: SkySmart
2 Replies

2. Shell Programming and Scripting

Print entire line only if certain fixed character matches the string

Hi All, I have a file testarun.txt contains the below lines and i want to print the lines if the character positions 7-8 matches 01. 201401011111 201401022222 201402013333 201402024444 201403015555 201403026666 201404017777 201404028888 201405019999 201405020000 I am trying the... (4 Replies)
Discussion started by: Arunprasad
4 Replies

3. Shell Programming and Scripting

Match part of string in file2 based on column in file1

I have a file containing texts and indexes. I need the text between (and including ) INDEX and number "1" alone in line. I have managed this: awk '/INDEX/,/1$/{if (!/1$/)print}' file1.txt It works for all indexes. And then I have second file with years and indexes per year, one per line... (3 Replies)
Discussion started by: phoebus
3 Replies

4. Shell Programming and Scripting

Grep a part of file based on string identifiers

consider below file contents cat myOutputFIle.txt 8 CCM-HQE-ResourceHealthCheck: Resource List : No RED/UNKNOWN resource Health entries found ---------------------------------------------------------- 9 CCM-TraderLogin-Status: Number of logins: 0... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. Shell Programming and Scripting

Print only matched string instead of entire line

Hi, I have a file whose lines are something like Tchampionspsq^@~^@^^^A^@^@^@^A^A^Aÿð^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^?ð^@^@^@^@^@^@^@?ð^@^@^@^@^@^@pppsq^@~^@#@^@^@^@^@^@^Hw^H^@^@^@^K^@^@^@^@xp^At^@^FTtime2psq^@ ~^@^^^A^@^@^@^B^A I need to extract all words matching T*psq from the file. Thing is... (4 Replies)
Discussion started by: shekhar2010us
4 Replies

6. Shell Programming and Scripting

Need Awk command to get part of string based on delimeter

HI, Need awk command to get date and time alone from Input : "15:29:15 28.08.2010|SCHEDULE: Started program POSG1" Output expected : "15:29:15 28.08.2010" Please help. (9 Replies)
Discussion started by: shanneykar
9 Replies

7. Shell Programming and Scripting

Sort based on string lenght.

I'm not familiar with find. If i use find in a certain directory i want it to show based on hierarchy. find . type d fol1 fol1/subfol1 fol1/subfol1/subfol1 fol2 fol2/subfol2 i want it to show like this fol1/subfol1/subfol1 fol1/subfol1 fol1 fol2/subfol2 fol2 do i need to use... (5 Replies)
Discussion started by: ryandegreat25
5 Replies

8. Shell Programming and Scripting

Print entire line based on value in a column

Friends, File1.txt abc|0|xyz 123|129|opq def|0|678 890|pqw|sdf How do I print the entire line where second column has value is 0? Expected Result: abc|0|xyz def|0|678 Thanks, Prashant ---------- Post updated at 02:14 PM ---------- Previous update was at 02:06 PM ---------- ... (1 Reply)
Discussion started by: ppat7046
1 Replies

9. UNIX for Dummies Questions & Answers

Search for a string and copy the entire line

Hello All, I am after the script or the command which can scan the entire file for a string $PART_ID and when found to extract/copy the corresponding $PART_ID value (e.g THIRE_PTY_SOFTWARE for the 1st occurance of $PART_ID in the attached file) to a file. Appreciate your help. Thanks in... (3 Replies)
Discussion started by: forumthreads
3 Replies

10. Shell Programming and Scripting

Replace Entire line if any part matches regexp

Hey guys, I have a file that I've slowly been awking, seding, and greping for data entry. I am down to pull the addresses out to insert them into an excel file. Each address is a few lines, but i want to put a semicolon delimiter in between each address so I can export the text file into excel and... (6 Replies)
Discussion started by: Cocoabean
6 Replies
Login or Register to Ask a Question