Dimitar Dimov
Dimitar Dimov

Reputation: 11

Moving second row after fourth using awk

I have a task to get the output of:

ps auxfwww | sort -k2n | head -n4

Then I have to rearrange it, using awk, so that I get as a result 1st,3rd,11th column of rows 2,3,4. But the rows must be in order 3,4,2. The closest I get is this:

ps auxfwww | sort -k2n | head -n4 | awk 'NR>=3' | awk '{print $1, $3, $11}'

But I have no idea how to get row 2 to get after rows 3 and 4.

It also has to be done with a single line command.

Please shed some light :)

Upvotes: 1

Views: 93

Answers (2)

Ed Morton
Ed Morton

Reputation: 203169

ps auxfwww | sort -k2n | awk 'NR<5{a[NR]=$1" "$3" "$11} END{print a[3] ORS a[4] ORS a[2]}'

Upvotes: 0

user2533019
user2533019

Reputation: 46

ps auxfwww | sort -k2n | head -n4 | awk '{ a[i++] = $1" "$3" "$11 } END { print a[2]; print a[3]; print a[1] }'

This prints row 3, row 4, followed by row 2.

Upvotes: 2

Related Questions