Re: sort a file.



On Thu, 29 Mar 2007 02:35:18 +0200, Mr. X. wrote:
Hello,
I need a program in unix (C)
that do : getting a file, and sort it (by lines).

No, you do not sort by lines, you sort the data.

It need to use sort command, and many process (by fork ...),
that each of the processes do sort of 100 lines (with temporary file of
pid).
and after all the processes are done - I need to merge the sorts into one
big output file.

I want to sort specific lines,
is it sort -k ?
I need help of that function (sort -k1,20 doesn't work : line 1 to 20).

If you were to do a man sort, you might notice
-k, --key=POS1[,POS2]
start a key at POS1, end it at POS2 (origin 1)

Now let's say you wanted to sort /etc/passwd by user id.
bittwister:x:1500:1500:Bit Twister:/home/bittwister:/bin/bash
junk:x:500:500:Junk Account:/home/junk:/bin/bash
^ ^
| |
UID----' `----- GID

As you can see, the uid field is not in the same column.
If you make the field terminator a : then you can use the -k switch to
pick which key/field (3) to be used during sorting.

So you have to first decide if your data is in fixed columns or in a
variable format. Then you decide which fields/columns to pick for
your sort criteria.


For extra points you might want to read
http://tldp.org/LDP/abs/html/index.html

Chapter 3. Special Characters has where you can get a pid.
.



Relevant Pages

  • Re: [ckrm-tech] [PATCH 0/5] Allow more than PAGESIZE data read in configfs
    ... you don't create a pid attribute per task. ... The sysfs file ... configfs, just located in a place where it is allowed. ... just a "list of this attr" sort of thing. ...
    (Linux-Kernel)
  • Re: Command (or something to kill) to force a logout?
    ... you only want the pid: ... You'll still need the sort -nr, ps doesn't have an option to reverse ... If you're going to to it in reverse pid order, ... killed by a terminal hangup as the window manager and terminal get ...
    (comp.sys.mac.system)
  • Re: Getting rid of nepomukserver
    ... that there was another complete level of 'service' management. ... This sort of layered obscurity pisses me off. ... (replace <PID> with the actual PID. ... To unsubscribe or change subscription options: ...
    (Fedora)
  • Re: Parse ASCII log ; sort and keep most recent entries
    ... > The log is an ASCII file that contains a process identifier, ... > username, date, and time field like this: ... If one wants/needs to sort records by date, life is also easier if you can ... > the most the most recent PID. ...
    (comp.lang.python)
  • Re: Parse ASCII log ; sort and keep most recent entries
    ... > So I need to sort by PID and date + time,then keep the most recent. ... output to be a list of unique PIDs, ... just open a file and with one line the reduced data ...
    (comp.lang.python)