Visit Our UNIX and Linux User Community


Is it possible to get the same result in one sort?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Is it possible to get the same result in one sort?
# 1  
Old 04-27-2020
Is it possible to get the same result in one sort?

Hi,


This is the file that I want to sort:



Code:
$: cat files.tmp
1237008222      10664   log.10
1237008222      10664   log.12
1237008222      10664   log.14
1237008222      10664   log.16
1237008222      10664   log.18
1237008222      10664   log.2
1237008222      10664   log.4
1237008222      10664   log.6
1237008222      10664   log.8
2296620157      10696   log.1
2296620157      10696   log.11
2296620157      10696   log.13
2296620157      10696   log.15
2296620157      10696   log.17
2296620157      10696   log.3
2296620157      10696   log.5
2296620157      10696   log.7
2296620157      10696   log.9


The desired output is as below:


Code:
1237008222      10664   log.2
1237008222      10664   log.4
1237008222      10664   log.6
1237008222      10664   log.8
1237008222      10664   log.10
1237008222      10664   log.12
1237008222      10664   log.14
1237008222      10664   log.16
1237008222      10664   log.18
2296620157      10696   log.1
2296620157      10696   log.3
2296620157      10696   log.5
2296620157      10696   log.7
2296620157      10696   log.9
2296620157      10696   log.11
2296620157      10696   log.13
2296620157      10696   log.15
2296620157      10696   log.17

Currently, I have had to loop thru files.tmp and grep for lines containing field 1 and run it thru sort.



Code:
$: cat files.tmp | grep "^1237008222" | sort -t. -n -k2
1237008222      10664   log.2
1237008222      10664   log.4
1237008222      10664   log.6
1237008222      10664   log.8
1237008222      10664   log.10
1237008222      10664   log.12
1237008222      10664   log.14
1237008222      10664   log.16
1237008222      10664   log.18
$: cat files.tmp | grep "^2296620157" | sort -t. -n -k2
2296620157      10696   log.1
2296620157      10696   log.3
2296620157      10696   log.5
2296620157      10696   log.7
2296620157      10696   log.9
2296620157      10696   log.11
2296620157      10696   log.13
2296620157      10696   log.15
2296620157      10696   log.17


I thought the following should have done the trick but no.



Code:
$: cat files.tmp | sort -k1 | sort -t. -k2n
2296620157      10696   log.1
1237008222      10664   log.2
2296620157      10696   log.3
1237008222      10664   log.4
2296620157      10696   log.5
1237008222      10664   log.6
2296620157      10696   log.7
1237008222      10664   log.8
2296620157      10696   log.9
1237008222      10664   log.10
2296620157      10696   log.11
1237008222      10664   log.12
2296620157      10696   log.13
1237008222      10664   log.14
2296620157      10696   log.15
1237008222      10664   log.16
2296620157      10696   log.17
1237008222      10664   log.18


I can't find an example of the kind of sorting that am after. It is to sort on the first column and then the third column 'grouped' based on the first column as a set. Smilie


At the moment, I am using a shell script and grep'ing for field1 and then doing the sort.
# 2  
Old 04-27-2020
Try
Code:
sort  -t. -k1,1 -k2n file


Previous Thread | Next Thread
Test Your Knowledge in Computers #740
Difficulty: Medium
FORTRAN II first appeared in 1958.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort by record column, Compare with conditons and export the result

Hello, I am new to Unix and would like to seek a help, please. I have 2 files (file_1 and file_2), I need to perform the following actions. 1 ) Sort the both file by the column 26-36 (which is Invoice number) what is sort command with the column sort? 2) Compare the file_1.sorted and... (3 Replies)
Discussion started by: Usagi
3 Replies

2. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

3. Shell Programming and Scripting

How to sort grep result based on timestamp?

Hi, Trying to sort grep result based on timestamp of the filename. I have the following result and want to sort them on timestampgrep -i 'ERROR' *log*2013* s_m_xxx_xxx_xxx_xxx_xxxx.log.20130906092431:TRANSF_1_1_1> DBG_21216 Finished transformations for Source Qualifier . Total errors ... (5 Replies)
Discussion started by: bobbygsk
5 Replies

4. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

5. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. UNIX for Dummies Questions & Answers

Weird: unexpected result after piping a sort

Hello, And when you think you know the basics of something, UNIX in this case, something like what I will describe below comes along.... On a Linux system, a "typical" directory with some files. Say 20. I do: > ls | sort > mylisting Now when I: > vi mylisting There is mylisting... (13 Replies)
Discussion started by: stavros
13 Replies

7. Shell Programming and Scripting

sort, columns, no result! can I print files of "planes"?

hi, please can I ask you for some help? I have data from 3D situation, x y z value I'd like to use gnuplot to generate maps of the value in the planes z=0 to z=1 for example, my file looks like -0,012 0,0060 0,0 0,13972813076023477 -0,012 0,0064319163 4,2894483E-4 ... (1 Reply)
Discussion started by: kocour
1 Replies

8. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

9. UNIX for Dummies Questions & Answers

display the result of wc -l with words before and after the result

hello showrev -p | wc -l returns: 381 What to do in case I want to have this output: number of lines returned by showrev -p is: 381 thx (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

10. Shell Programming and Scripting

Outputting formatted Result log file from old 30000 lines result log<help required>

Well I have a 3000 lines result log file that contains all the machine data when it does the testing... It has 3 different section that i am intrsted in 1) starting with "20071126 11:11:11 Machine Header 1" 1000 lines... "End machine header 1" 2) starting with "20071126 12:12:12 Machine... (5 Replies)
Discussion started by: vikas.iet
5 Replies

Featured Tech Videos