need help in correcting the code

Hi Guys !

can anyone help me to write the code doing same thing without using awk. is it possible using cut command?

awk '{c[$1]++} END {for(k in c} print k "\t\t" c[k ];}' file_name | sort -nrk 2 | column -t

thanks in advance

BR

Ewa

how about your input file?
maybe you can try like this.(while FS is space!)

$ cut -d" " -f1 file_name|sort|uniq -c|sed 's/ *\(.*\) \(.*\)/\2  \1/'

regards
ygemici

1 Like

Hi

your code is working fine but can you explain a bit only this part >>>> sed 's/ *\(.\) \(.\)/\2 \1/'

and if you don't mind please explain also

what is difference between these two lines why they give difference results?

[ cut -f1 -d' ' file_name | sort |uniq -c | sort -nr ]

"what the first sort does in the above line"?

[ cut -f1 -d' ' file_name | uniq -c | sort -nr ]

"the second line produce different result "

BR

Eva

we must firstly use the sort command, because of uniq command process for sequential lines.therefore we use firstly sort command for get the sequential lines.

# cat file
a b
b c
a b
d c
a b
a b

uniq + sort (unsuccesfull)

# cut -f1 -d' ' file|uniq -c
      1 a
      1 b
      1 a
      1 d
      2 a
# cut -f1 -d' ' file|uniq -c|sort
      1 a
      1 a
      1 b
      1 d
      2 a

sort + uniq (successfull)

# cut -f1 -d' ' file|sort
a
a
a
a
b
d
# cut -f1 -d' ' file|sort|uniq -c
      4 a
      1 b
      1 d

and with sed

# sed 's/ *\(.*\) \(.*\)/\2  \1/' 

s (substitute) --> s/regexp/replacement/

  • --> represent the initial "spaces" of string (between the quotes)
"      "4 a
"      "1 b
"      "1 d

\(.\) --> 4 --> \(.\) this strucutes saves the our string (.* is means everything) --> represent '\1' while writes it
and there is a space " "
\(.*\) --> a --> represent '\2' while writes it

finaly we write our values to stdoutp
\2 --> a
and two spaces
\1 --> 4

# cut -f1 -d' ' file |sort|uniq -c|sed 's/ *\(.*\) \(.*\)/\2 \1/'
a  4
b  1
d  1

regards
ygemici

1 Like

Hi ygemici

very grateful to you... couldn't be taught in a better and simplest way as you did, each line is precisely explain, peace on you

thanks once again

kind Regards,

Ewa