Sponsored Content
Full Discussion: Sort based on one column
Top Forums UNIX for Beginners Questions & Answers Sort based on one column Post 303041928 by Scrutinizer on Saturday 7th of December 2019 07:46:28 AM
Old 12-07-2019
Hi, try:

Code:
awk '/^[0-9]/{print x}1' file | 
awk '{$1=$1}1' RS= FS='\n' OFS=§ |
sort -rnk 5,5 |
tr § '\n'

  1. Split the records with a blank line, using a digit on the first position as the indicator of a new record
  2. Replace the line terminators of records with an obscure character (paragraph character was used here (§), but it could be any character as long as it is not used in the input file) and remove the empty line that was introduced in step 1
  3. Numeric reverse sort of the records on the 5th field
  4. Reassemble the records to their original state by replacing the obscure character by a newline

Last edited by Scrutinizer; 12-07-2019 at 09:07 AM..
This User Gave Thanks to Scrutinizer For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies

2. Shell Programming and Scripting

sort on second column only based on first column

I have an input file like this... AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Sunlight AAAlkalines Sunlight AAAlkalines Sunlight AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Energizer AAASalines ... (7 Replies)
Discussion started by: malcomex999
7 Replies

3. Shell Programming and Scripting

Sort file based on column

Hi, My input file is $cat samp 1 siva 1 raja 2 siva 1 siva 2 raja 4 venkat i want sort this name wise...alos need to remove duplicate lines. i am using cat samp|awk '{print $2,$1}'|sort -u it showing raja 1 (3 Replies)
Discussion started by: rsivasan
3 Replies

4. UNIX for Dummies Questions & Answers

How to sort a column based on numerical ascending order if it includes e-10?

I have a column of numbers in the following format: 1.722e-05 2.018e-05 2.548e-05 2.747e-05 7.897e-05 4.016e-05 4.613e-05 4.613e-05 5.151e-05 5.151e-05 5.151e-05 6.1e-05 6.254e-05 7.04e-05 7.12e-05 7.12e-05 (6 Replies)
Discussion started by: evelibertine
6 Replies

5. UNIX for Dummies Questions & Answers

Sort command in one column and not effect to another column

If my data is numerical : 1 = 101 2 = 102 3 = 104 4 = 104 7 = 103 8 = 103 9 = 105 I need the result like below: 1 = 101 2 = 102 3 = 103 4 = 103 7 = 104 8 = 104 9 = 105 (4 Replies)
Discussion started by: GeodusT
4 Replies

6. Shell Programming and Scripting

Sort based on column 1, not working with awk

Hi Guru, I need some help regarding awking the output so it only show the first line (based on column) of each row. So If column has 1, three row, then it only show the first line of that row, based on similar character in column 1. So i am trying to achieve a sort, based on column one and... (3 Replies)
Discussion started by: Junes
3 Replies

7. Shell Programming and Scripting

Sort based on certain value in a column

Hi, i need to sort content of files based on a specific value. An example as below. Input1.txt Col_1 SW_MH2_ST ST_F72_9S SW_MH3_S6 Col_2 SW_MH3_AS7 ST_S15_9CH SW_MH3_AS8 SW_MH3_ST Col_3 ST_M93_SZ ST_C16_TC (12 Replies)
Discussion started by: redse171
12 Replies

8. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

9. UNIX for Beginners Questions & Answers

How to align/sort the column pairs of an csv file, based on keyword word specified in another file?

I have a csv file as shown below, xop_thy 80 avr_njk 50 str_nyu 60 avr_irt 70 str_nhj 60 avr_ngt 50 str_tgt 80 xop_nmg 50 xop_nth 40 cyv_gty 40 cop_thl 40 vir_tyk 80 vir_plo 20 vir_thk 40 ijk_yuc 70 cop_thy 70 ijk_yuc 80 irt_hgt 80 I need to align/sort the csv file based... (7 Replies)
Discussion started by: dineshkumarsrk
7 Replies

10. Shell Programming and Scripting

Use sort to sort numerical column

How to sort the following output based on lowest to highest BE? The following sort does not work. $ sort -t. -k1,1n -k2,2n bfd.txt BE31.116 0s 0s DOWN DAMP BE31.116 0s 0s DOWN DAMP BE31.117 0s 0s ... (7 Replies)
Discussion started by: sand1234
7 Replies
POE::Filter::Line(3pm)					User Contributed Perl Documentation				    POE::Filter::Line(3pm)

NAME
POE::Filter::Line - serialize and parse terminated records (lines) SYNOPSIS
#!perl use POE qw(Wheel::FollowTail Filter::Line); POE::Session->create( inline_states => { _start => sub { $_[HEAP]{tailor} = POE::Wheel::FollowTail->new( Filename => "/var/log/system.log", InputEvent => "got_log_line", Filter => POE::Filter::Line->new(), ); }, got_log_line => sub { print "Log: $_[ARG0] "; } } ); POE::Kernel->run(); exit; DESCRIPTION
POE::Filter::Line parses stream data into terminated records. The default parser interprets newlines as the record terminator, and the default serializer appends network newlines (CR/LF, or "x0Dx0A") to outbound records. Record terminators are removed from the data POE::Filter::Line returns. POE::Filter::Line supports a number of other ways to parse lines. Constructor parameters may specify literal newlines, regular expressions, or that the filter should detect newlines on its own. PUBLIC FILTER METHODS
POE::Filter::Line's new() method has some interesting parameters. new new() accepts a list of named parameters. In all cases, the data interpreted as the record terminator is stripped from the data POE::Filter::Line returns. "InputLiteral" may be used to parse records that are terminated by some literal string. For example, POE::Filter::Line may be used to parse and emit C-style lines, which are terminated with an ASCII NUL: my $c_line_filter = POE::Filter::Line->new( InputLiteral => chr(0), OutputLiteral => chr(0), ); "OutputLiteral" allows a filter to put() records with a different record terminator than it parses. This can be useful in applications that must translate record terminators. "Literal" is a shorthand for the common case where the input and output literals are identical. The previous example may be written as: my $c_line_filter = POE::Filter::Line->new( Literal => chr(0), ); An application can also allow POE::Filter::Line to figure out which newline to use. This is done by specifying "InputLiteral" to be undef: my $whichever_line_filter = POE::Filter::Line->new( InputLiteral => undef, OutputLiteral => " ", ); "InputRegexp" may be used in place of "InputLiteral" to recognize line terminators based on a regular expression. In this example, input is terminated by two or more consecutive newlines. On output, the paragraph separator is "---" on a line by itself. my $paragraph_filter = POE::Filter::Line->new( InputRegexp => "([x0Dx0A]{2,})", OutputLiteral => " --- ", ); PUBLIC FILTER METHODS
POE::Filter::Line has no additional public methods. SEE ALSO
Please see POE::Filter for documentation regarding the base interface. The SEE ALSO section in POE contains a table of contents covering the entire POE distribution. BUGS
The default input newline parser is a regexp that has an unfortunate race condition. First the regular expression: /(x0Dx0A?|x0Ax0D?)/ While it quickly recognizes most forms of newline, it can sometimes detect an extra blank line. This happens when a two-byte newline character is broken between two reads. Consider this situation: some stream dataCR LFother stream data The regular expression will see the first CR without its corresponding LF. The filter will properly return "some stream data" as a line. When the next packet arrives, the leading "LF" will be treated as the terminator for a 0-byte line. The filter will faithfully return this empty line. It is advised to specify literal newlines or use the autodetect feature in applications where blank lines are significant. AUTHORS &; COPYRIGHTS Please see POE for more information about authors and contributors. perl v5.14.2 2012-05-15 POE::Filter::Line(3pm)
All times are GMT -4. The time now is 06:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy