file - Unix command sort by number of occurances of a specific character -


i have file format:

key value:value:value:value key value:value key value key value:value:value 

where there key @ first column , in second list of values delimited :. there command can sort file based on number of occurances of : in value list i.e. values least e.g. previous example this:

key value:value:value:value key value:value:value key value:value key value 

quick , dirty:

awk -f: '{$0=nf"#"$0}1' file|sort -nr|sed 's/.*#//'  

test example:

kent$  echo "key value:value:value:value key value:value key value key value:value:value"|awk -f: '{$0=nf"#"$0}1'|sort -nr|sed 's/.*#//' key value:value:value:value key value:value:value key value:value key value 

edit

the sort tool on linux/unix box powerful. sorts against column/fields, not based on calculation. requirement needs calculation first , sort against on result.

so add new column, count of :, sort sort command, @ end remove column.

awk adds column sort sort sed removes column 

you can remove pipes , see output, see how worked.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -