-1

I have to change the output of uniq -c: (example)

92 root
80 user

in

root 92
user 80

how can I do without using awk force?

2
  • 1
    What is wrong with awk?
    – jesse_b
    Mar 23, 2018 at 14:01
  • Nothing I would like to know if there are other ways
    – F.G.
    Mar 23, 2018 at 14:03

3 Answers 3

1

Here you go with GNU sed:

... | sed -E 's/(\S*) (\S*)/\2 \1/'

Or POSIXly,

... | sed 's/\([^ ]*\) \(.*\)/\2 \1/'
2
  • @Stéphane Chazelas - fwiw, none of these works with actual uniq -c output - the quantifier in the 1st group has to be + (or \{1,\}) to deal with the leading whitespace Mar 23, 2018 at 15:22
  • @don_crissti, that would depend on the uniq implementation. The OP's seems to not output leading spaces. In any case, yes, using + would help making it work with uniq implementations that output leading spaces. Mar 23, 2018 at 15:49
1

Here is a solution with cut and paste, given your input is a file and the delimiter is a space:

cut -d' ' -f1 input > temp1
cut -d' ' -f2 input > temp2
paste -d' ' temp2 temp1 > output
rm temp*
4
  • If somebody knows how to have a tab as a delimiter in cut other than copy/paste i'd be very very thankful.
    – 3nrique0
    Mar 23, 2018 at 14:22
  • 1
    Try : cut -d $'\t' Mar 23, 2018 at 14:22
  • 2
    @GillesQuenot- that is utterly superfluous: TAB is the default delimiter when using cut and paste Mar 23, 2018 at 14:57
  • and btw, if that uniq -c output is exactly as posted by the OP (no leading spaces, columns separated by a single space) and if it's saved in a file you don't need temporary files etc you can simply run: paste -d' ' infile infile | cut -d' ' -f2,3 Mar 23, 2018 at 19:09
0

Using perl

... | perl -ane 'print "$F[1] $F[0]\n"'

You can refer to this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .