sort a field in ascending order and delete the first and last number [closed]

2024/11/7 12:32:13

I have a data that looks like following:

a     10,5,3,66,50
b     2,10,1,88,5,8,9
c     4,60,10,39,55,22
d     1,604,3,503,235,45,60,7
e     20,59,33,2,6,45,36,34,22

I want to sort the data in second column in ascending order

a     3,5,10,50,66
b     1,2,5,8,9,10,88
c     4,10,22,39,55,60
....
....

Then delete the smallest and the largest value from it. So like this:

a     5,10,50
b     2,5,8,9,10
c     10,22,39,55
....
....

Any help would be appreciated!

Answer

Here you go:

awk '{l=split($2,a,",");asort(a);printf "%s\t",$1;for(i=2;i<l;i++) printf "%s"(i==l-1?RS:","),a[i]}' t
a       5,10,50
b       2,5,8,9,10
c       10,22,39,55
d       3,7,45,60,235,503
e       6,20,22,33,34,36,45

PS If I remember correct, you need gnu awk due to asort

How it works:

awk '{l=split($2,a,",")                      # Split the data into array "a" and set "l" to length of arrayasort(a)                                # Sort the array "a"printf "%s\t",$1                        # Print the first columnfor(i=2;i<l;i++)                        # Run a loop from second element to second last element in array "a"printf "%s"(i==l-1?RS:","),a[i]     # Print the element separated by "," except for last element, print a new line}'  file                                # Read the file
https://en.xdnf.cn/q/120738.html

Related Q&A

PermissionError: [Errno 13] Permission denied:

Im trying to write in a txt file the vertices of a spline mesh, but I get this error: PermissionError: [Errno 13] Permission denied: C\:Windows\system32\vt_84.txtThe code is

python calculator [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

How to sequence row based on another row?

I am trying to convert a formula from excel to pandas.The DataFrame looks like this: Column A Column B H H H J J J J K K I want to fill column B to increment while the value in co…

Multiclassification task using keras [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

Clarification needed regarding immutability of strings in Python [closed]

Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting …

Please help me in solving Fractional Knapsack problem (Maximum Value of the Loot)

Maximum Value of the LootProblem Introduction: A thief finds much more loot than his bag can fit. Help him to find the most valuable combination of items assuming that any fraction of a loot item can b…

Python countdown clock with GUI [duplicate]

This question already has an answer here:Making a countdown timer with Python and Tkinter?(1 answer)Closed 8 years ago.Im having problems with a countdown clock that I was making in Python for a Raspb…

Confidence calculation in association rule [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

How to write the names that start with A - L to one file and the rest to another?

Hello my assignment is :Create a system that allows the user to enter their name, title, surname, Dob, email and phone number. Once details are submitted, they should be written to a file. Surnames tha…

How is it possible to use a while loop to print even numbers 2 through 100?

I am a beginner and I am stuck on this problem, "Write a python code that uses a while loop to print even numbers from 2 through 100. Hint ConsecutiveEven differ by 2."Here is what I came up …