Sponsored Content
Top Forums Shell Programming and Scripting Sorting based on multiple delimiters Post 302519077 by rdcwayx on Tuesday 3rd of May 2011 02:32:28 AM
Old 05-03-2011
Quote:
Originally Posted by gimley
Hello,
I tested the file and what I get is the message
0 delimiters
followed by the full set of sample test data.
I checked the script abd the syntax shows that the files should be sorted as per number of delimiters.
What has gone wrong ?
I am enclosing the testdata as a zip file.
Many thanks
No problem I found.

If you run the awk in Solaris, please replace the command with nawk or /usr/xpg4/bin/awk
Code:
awk -F = 'NR==1{max=NF;min=NF}
         {max=(max>NF)?max:NF;min=(min<NF)?min:NF;a[NF]=(a[NF]=="")?$0:a[NF] ORS $0}
    END{for (i=max;i>=min;i--) {if (a[i]!="") print i-1 " delimiters" ORS a[i]}}' test |head -10

6 delimiters
pathan=inayat=khan=rashid=khan=sahebzadi=m
shiv=ram=tandale=ganesh=laxman=hirabai=m
5 delimiters
gore=bibi=sakina=irfanali=tayeba=f
jamadar=aves=ahmed=ashfaque=sherbano=m
ram=tandale=ganesh=laxman=hirabai=m
4 delimiters
kale=amita=bhanudas=shobha=f
lande=amit=chandrabhan=asha=m

---------- Post updated at 04:32 PM ---------- Previous update was at 04:25 PM ----------

Quote:
Originally Posted by kevintse
Try this:
Code:
awk -F= '{print NF, $0}' infile | sort -k1 -nr | awk '!d||$1!=d{d=$1; print d-1 " delimiters"}{print $2}'

Clever way.

little adjust (!a[$1]++) to look better, and -k1 is useless.
Code:
awk -F= '{print NF, $0}' infile | sort -nr |awk '!a[$1]++ {print $1-1 " delimiters" }{print $2}'

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting a flat file based on multiple colums(using character position)

Hi, I have an urgent task here. I am required to sort a flat file based on multiple columns which are based on the character position in that line. I am restricted to use the character position instead of the space and sort +1 +2 etc to do the sorting. I understand that there is a previous... (8 Replies)
Discussion started by: cucubird
8 Replies

2. Shell Programming and Scripting

Cut based on Two Delimiters at one go

Hi I wanted to cut the feilds comming after % and After $ at one go can we do some thing like this cut -f 2 -d "%|$" (But it doesnot work) Input File BWPG %TCPRP1 $SCSPR000 BWPH %TCPRP1 $SCSPR003 BWPI %TRTYUP ResourceDescription="IMPRIMANTE " $BWOPTY BWPJ %ZOMBIE ... (4 Replies)
Discussion started by: pbsrinivas
4 Replies

3. Shell Programming and Scripting

Sorting based on Multiple columns

Hi, I have a requirement whereby I have to sort a flat file based on Multiple Columns (similar to ORDER BY Clause of Oracle). I am getting 10 columns in the flat file and I want the file to be sorted on 1st, 3rd, 4th, 7th and 9th columns in ascending order. The flat file is pipe seperated. Any... (15 Replies)
Discussion started by: dharmesht
15 Replies

4. Shell Programming and Scripting

sorting(both Ascending & Descending) files based on multiple fields

Hi All, I am encountered with a problem while sorting a file based on multiple columns . I need to sort like: (field2,ascending) , (field3,ascending) ,(field8,descending) , (field7,ascending),(field13,ascending). So far i was sorting only in ascending order but here i need to use one... (1 Reply)
Discussion started by: apjneeraj
1 Replies

5. Shell Programming and Scripting

AWK with multiple delimiters

I have the following string sample: bla bla bla bla bla I would like to extract the "123" using awk. I thought about awk -F"]" '{ print $1 }' but it doesn't work Any ideas ? (7 Replies)
Discussion started by: gdub
7 Replies

6. Shell Programming and Scripting

Sorting problem: Multiple delimiters, multiple keys

Hello If you wanted to sort a .csv file that was filled with lines like this: <Ticker>,<Date as YYYYMMDD>,<Time as H:M:S>,<Volume>,<Corr> (H : , M, S: ) by date, does anybody know of a better solution than to turn the 3rd and 4th colons of every line into commas, sorting on four keys,... (20 Replies)
Discussion started by: Ryan.
20 Replies

7. Shell Programming and Scripting

Concatinating the lines based on number of delimiters

Hi, I have a problem to concatenate the lines based on number of delimiters (if the delimiter count is 9 then concatenate all the fields & remove the new line char bw delimiters and then write the following data into second line) in a file. my input file content is Title| ID| Owner|... (4 Replies)
Discussion started by: bi.infa
4 Replies

8. Shell Programming and Scripting

treating multiple delimiters[solved]

Hi, I need to display the last column value in the below o/p. sam2 PS 03/10/11 0 441 Unable to get o/p with this awk code awk -F"+" '{ print $4 }' pwdchk.txt I need to display 441(in this eg.) and also accept it as a variable to treat it with if condition and take a decision.... (1 Reply)
Discussion started by: sam_bd
1 Replies

9. Shell Programming and Scripting

awk multiple delimiters

Hi Folks, This is the first time I ever encountered this situation My input file is of this kind cat input.txt 1 PAIXAF 0 1 1 -9 0 0 0 1 2 0 2 1 2 1 7 PAIXEM 0 7 1 -9 1 0 2 0 1 2 2 1 0 2 9 PAKZXY 0 2 1 -9 2 0 1 1 1 0 1 2 0 1 Till the sixth column (which is -9), I want my columns to... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

10. Shell Programming and Scripting

Insert Columns before the last Column based on the Count of Delimiters

Hi, I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number. Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field awk -F 'Ç' '{ if (NF-1 < 139)} END { "Insert 2... (5 Replies)
Discussion started by: arunkesi
5 Replies
INNWATCH.CTL(5) 						File Formats Manual						   INNWATCH.CTL(5)

NAME
innwatch.ctl - control Usenet supervision by innwatch DESCRIPTION
The file /etc/news/innwatch.ctl is used to determine what actions are taken during the periodic supervisions by innwatch. The file consists of a series of lines; blank lines and lines beginning with a number sign (``#'') are ignored. All other lines consist of seven fields, each preceded by a delimiting character: :label:state:condition:test:limit:command:reason The delimiter can be any one of several non-alphanumeric characters that does not appear elsewhere in the line; there is no way to quote it to include it in any of the fields. Any of ``!'', ``,'', ``:'', ``@'', ``;'', or ``?'' is a good choice. Each line can have a different delimiter; the first character on each line is the delimiter for that line. White space surrounding delimiters, except before the first, is ignored, and does not form part of the fields, white space within fields is permitted. All delimiters must be present. The first field is a label for the control line. It is used as an internal state indicator and in ctlinnd messages to control the server. If omitted, the line number is used. The second field specifies when this control line should be used. It consists of a list of labels, and special indicators, separated by whitespace. If the current state matches against any of the labels in this field, this line will be used as described below. The values that may be used are: - This line matches if the current state is the same as the label on this line, or if the current state is ``run,'' the initial state. This is also the default state if this field is empty. + This line matches if the current state is ``run.'' * This line always matches. label This line matches if the current state is the specified ``label.'' -label This line matches if the current state is not the specified ``label.'' The third field specifies a shell command that is invoked if this line matches. Do not use any shell filename expansion characters such as ``*'', ``?'', or ``['' (even quoted, they're not likely to work as intended). If the command succeeds, as indicated by its exit status, it is expected to have printed a single integer to standard output. This gives the value of this control line, to be used below. If the com- mand fails, the line is ignored. The command is executed with its current directory set to the news spool directory, /var/spool/news. The fourth field specifies the operator to use to test the value returned above. It should be one of the two letter numeric test operators defined in test(1) such as ``eq'', ``lt'' and the like. The leading dash (`'-'') should not be included. The fifth field specifies a constant with which to compare the value using the operator just defined. This is done by invoking the command test value -operator constant The line is said to ``succeed'' if it returns true. The sixth field specifies what should be done if the line succeeds, and in some cases if it fails. Any of the following words may be used: throttle Causes innwatch to throttle the server if this line succeeds. It also sets the state to the value of the line's label. If the line fails, and the state was previously equal to the label on this line (that is, this line had previously succeeded), then a go command will be sent to the server, and innwatch will return to the ``run'' state. The ``throttle'' is only performed if the current state is ``run'' or a state other than the label of this line, regardless of whether the command succeeds. pause Is identical to ``throttle'' except that the server is paused. shutdown Sends a ``shutdown'' command to the server. It is for emergency use only. flush Sends a ``flush'' command to the server. go Causes innwatch to send a ``go'' command to the server and to set the state to ``run.'' exit Causes innwatch to exit. skip The result of the control file is skipped for the current pass. The last field specifies the reason that is used in those ctlinnd commands that require one. More strictly, it is part of the reason -- innwatch appends some information to it. In order to enable other sites to recognize the state of the local innd server, this field should usually be set to one of several standard values. Use ``No space'' if the server is rejecting articles because of a lack of filesystem resources. Use ``loadav'' if the server is rejecting articles because of a lack of CPU resources. Once innwatch has taken some action as a consequence of its control line, it skips the rest of the control file for this pass. If the action was to restart the server (that is, issue a ``go'' command), then the next pass will commence almost immediately, so that innwatch can discover any other condition that may mean that the server should be suspended again. EXAMPLES
@@@df .|awk 'NR==2 {print $4}'@lt@10000@throttle@No space @@@df -i .|awk 'NR==2 {print $4}'@lt@1000@throttle@No space (inodes) The first line causes the server to be throttled if the free space drops below 10000 units (using whatever units df uses), and restarted again when free space increases above the threshold. The second line does the same for inodes. The next three lines act as a group and should appear in the following order. It is easier to explain them, however, if they are described from the last up. !load!load hiload!loadavg!lt!5!go! :hiload:+ load:loadavg:gt:8:throttle:loadav /load/+/loadavg/ge/6/pause/loadav The final line causes the server to be paused if innwatch is in the ``run'' state and the load average rises to, or above, six. The state is set to ``load'' when this happens. The previous line causes the server to be throttled when innwatch is in the ``run'' or ``load'' state, and the load average rises above eight. The state is set to ``hiload'' when this happens. Note that innwatch can switch the server from ``paused'' to ``throttled'' if the load average rises from below six to between six and seven, and then to above eight. The first line causes the server to be sent a ``go'' command if innwatch is in the ``load'' or ``hiload'' state, and the load average drops below five. Note that all three lines assume a mythical command loadavg that is assumed to print the current load average as an integer. In more prac- tical circumstances, a pipe of uptime into awk is more likely to be useful. BUGS
This file must be tailored for each individual site, the sample supplied is truly no more than a sample. The file should be ordered so that the more common problems are tested first. The ``run'' state is not actually identified by the label with that three letter name, and using it will not work as expected. Using an ``unusual'' character for the delimiter such as ``('', ``*'', ``&'', ```'', ``''', and the like, is likely to lead to obscure and hard to locate bugs. HISTORY
Written by <kre@munnari.oz.au> for InterNetNews. This is revision 1.5, dated 1996/09/06. SEE ALSO
innd(8), ctlinnd(8), news.daily(8). INNWATCH.CTL(5)
All times are GMT -4. The time now is 07:25 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy