[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: which command I should use to output sequentially,



>>>>> lina  <lina.lastname@gmail.com> writes:

	First of all, a kindly reminder: there's a news:comp.unix.shell
	newsgroup (also available via Google Groups [1], though a proper
	newsreader software is recommended), with a few truly
	knowledgeable folks among the subscribers, which such questions
	should've been pointed to.

[1] http://groups.google.com/group/comp.unix.shell/

[…]

 > I just remember the sort command, but I still don't know how to get
 > the ideal one,

 > after I tried the sort -n -k2 , something changed on field 2 but it's
 > still a bit away from the one I need.

	Unfortunately, the numeric (-n) sort cannot be performed over
	the field that contains numbers prefixed with arbitrary strings.
	Therefore, it's necessary to split such a field into the
	separate prefix and number parts, like (assuming GNU Sed):

$ sed -e 's/^\(\s*\w\+\s\+[^0-9[:blank:]]\+\)\([[:digit:]]\+\)/\1 \2/' 
 238CHO   C10 3617   1.697   5.334   9.317
 238CHO   C11 3624   1.665   5.468   9.092
^D
 238CHO   C 10 3617   1.697   5.334   9.317
 238CHO   C 11 3624   1.665   5.468   9.092
$ 

	Then, the output may be sorted on the now-third column, and the
	extra space removed.  Thus:

$ sed -e 's/^\(\s*\w\+\s\+[^0-9[:blank:]]\+\)\([[:digit:]]\+\)/\1 \2/' \
      | sort -nk 3,3 -k1,1 \
      | sed -e 's/^\(\s*\w\+\s\+[^0-9[:blank:]]\+\)\s\([[:digit:]]\+\)/\1\2/' 

[…]

-- 
FSF associate member #7257


Reply to: