Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Sort by id value in every two rows


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Sort by id value in every two rows

Hello,
I am running ubuntu 14.04. My purpose is to sort below file according to value of id even though it contains alphanumeric strings inside double quote. File is not tab separated. I am not sure that can be done...
mydata
Code:
#INFO id="133 texas" logo="http://tx.yy.zz
http://11.22.48
#INFO id="21 michigan" logo="http://mx.yy.zz
http://11.22.55
#INFO id="18 london" logo="http://lx.yy.zz
http://11.22.77
#INFO id="299 paris" logo="http://px.yy.zz
http://11.22.00

Expected output:
Code:
#INFO id="18 london" logo="http://lx.yy.zz
http://11.22.77
#INFO id="21 michigan" logo="http://mx.yy.zz
http://11.22.55
#INFO id="133 texas" logo="http://tx.yy.zz
http://11.22.48
#INFO id="299 paris" logo="http://px.yy.zz
http://11.22.00

What I tried:
Code:
awk '{print int((NR-1)/2), $0}' mydata | sort -n -k2,2 | cut -f2- -d' '


Thank you
Boris

EDIT:
Hello,
I have solved somehow with a bit long way but it's okay now.
Code:
for i in {10..900}
do
echo "id=\"$i"
done > output

Then with grep and while read -r line loop.

Thanks
Boris

Last edited by baris35; 06-02-2019 at 06:40 PM.. Reason: solved
# 2  
You were very close with your awk, sort, cut pipeline:

Code:
awk -F\" 'NR%2{printf "%s\t%s\t",$2,$0; next }1' mydata |
  sort -n | cut -f2- | tr '\t' '\n'

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Hi.

Noting that in this specific dataset, the "#" is essentially the record separator, we can add a dummy record at the top, and use some of the features of a flexible alternative sorting code msort.

Here is a demonstration script with results:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate sort of multi-line record, msort, sed

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C msort sed

FILE=${1-data2}

pl " Input data file $FILE:"
head $FILE

pl " Results:"
msort -q -r "#" -d '"' -n 2,2 -c h $FILE |
tee t1 |
sed '1d;$d'

pl " Temporary file t1:"
head -20 t1

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution        : Debian 8.11 (jessie) 
bash GNU bash 4.3.30
msort 8.53
sed (GNU sed) 4.2.2

-----
 Input data file data2:
# junk "0 "
#INFO id="133 texas" logo="http://tx.yy.zz
http://11.22.48
#INFO id="21 michigan" logo="http://mx.yy.zz
http://11.22.55
#INFO id="18 london" logo="http://lx.yy.zz
http://11.22.77
#INFO id="299 paris" logo="http://px.yy.zz
http://11.22.00

-----
 Results:
#INFO id="18 london" logo="http://lx.yy.zz
http://11.22.77
#INFO id="21 michigan" logo="http://mx.yy.zz
http://11.22.55
#INFO id="133 texas" logo="http://tx.yy.zz
http://11.22.48
#INFO id="299 paris" logo="http://px.yy.zz
http://11.22.00

-----
 Temporary file t1:
 junk "0 "
#INFO id="18 london" logo="http://lx.yy.zz
http://11.22.77
#INFO id="21 michigan" logo="http://mx.yy.zz
http://11.22.55
#INFO id="133 texas" logo="http://tx.yy.zz
http://11.22.48
#INFO id="299 paris" logo="http://px.yy.zz
http://11.22.00
#

Note that the raw results -- file t1 -- has a beginning and ending line that should be removed, which is what the sed command does. The sorting mode is hybrid, a combination of alphabetic and numeric.

If you have any odd sorting requirements that the standard sort does not address, it may be useful to consider msort.

Some more information on msort:
Code:
msort   sort records in complex ways (man)
Path    : /usr/bin/msort
Version : 8.53
Type    : ELF 64-bit LSB executable, x86-64, version 1 (SYS ...)
Help    : probably available with -h,--help
Repo    : Debian 8.11 (jessie) 
Home    : http://www.billposer.org/Software/msort.html (pm)

Best wishes ... cheers, drl
These 2 Users Gave Thanks to drl For This Post:
# 4  
Try also
Code:
$ paste -sd"\t\n" file | sort -nt\" -k2 | tr $'\t' $'\n'
#INFO id="18 london" logo="http://lx.yy.zz
http://11.22.77
#INFO id="21 michigan" logo="http://mx.yy.zz
http://11.22.55
#INFO id="133 texas" logo="http://tx.yy.zz
http://11.22.48
#INFO id="299 paris" logo="http://px.yy.zz
http://11.22.00

These 4 Users Gave Thanks to RudiC For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Sort tab delimited file according to which rows have missing values
MBarrett1213
Hello! I have a tab delimited file with values in three columns. Some values occur in all three columns, other values are present in only one or two columns. I would like to sort the file so that rows with no missing values come first, rows with one missing values come next, and rows with two...... UNIX for Dummies Questions & Answers
9
UNIX for Dummies Questions & Answers
Converting Columns To Rows Sort Of
jimmyf
Hi I'm a UNIX awk and sed novice at best. I'm trying to creat a .csv file so it can be graphed in Excel. Tried various xargs, awk, sed and paste but just can't seem to get the data to line up. Not sure if this is beyond for a question in these forums. Any help would greatly be appreciated. Have...... UNIX for Dummies Questions & Answers
4
UNIX for Dummies Questions & Answers
Sort the multi column rows
sandy1028
abc xyz - - - - - - - - - - - How to sort the second column in ascending order.... Shell Programming and Scripting
2
Shell Programming and Scripting
Deleting specific rows in large files having rows greater than 100000
manish2009
Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf...... Shell Programming and Scripting
9
Shell Programming and Scripting
Sort, group rows
patrykxes
I wrote script in bash which generates this report: User1,admin,rep,User2,shell,path1,x1,r1 User2,admin,rep,User7,shell,path1,x1,r1 User3,admin,rep,User4,shell,path1,x1,r1 User4,admin,rep,User3,shell,path1,x1,r1 User5,admin,rep,User1,shell,path1,x1,r1 User6,admin,rep,User5,shell,path1,x1,r1...... Shell Programming and Scripting
6
Shell Programming and Scripting

Featured Tech Videos