Sponsored Content
Top Forums Shell Programming and Scripting sort entire line based on part of the string Post 302220513 by gurpal2000 on Friday 1st of August 2008 02:58:02 AM
Old 08-01-2008
Quote:
Originally Posted by Annihilannic
From the sort man page:

Code:
           -k keydef   The keydef argument defines a restricted sort key.
                       The format of this definition is
 
                            field_start[type][,field_end[type]]
...
                       A field_start position specified by m.n is
                       interpreted to mean the nth character in the mth
                       field. 
...

strange i'm using sort 6.9 on mandriva 2008 and this doesnt appear in the man pages:

[server~]$ sort --version
sort (GNU coreutils) 6.9
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License <The GNU General Public License - GNU Project - Free Software Foundation (FSF)>.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and Paul Eggert.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace Entire line if any part matches regexp

Hey guys, I have a file that I've slowly been awking, seding, and greping for data entry. I am down to pull the addresses out to insert them into an excel file. Each address is a few lines, but i want to put a semicolon delimiter in between each address so I can export the text file into excel and... (6 Replies)
Discussion started by: Cocoabean
6 Replies

2. UNIX for Dummies Questions & Answers

Search for a string and copy the entire line

Hello All, I am after the script or the command which can scan the entire file for a string $PART_ID and when found to extract/copy the corresponding $PART_ID value (e.g THIRE_PTY_SOFTWARE for the 1st occurance of $PART_ID in the attached file) to a file. Appreciate your help. Thanks in... (3 Replies)
Discussion started by: forumthreads
3 Replies

3. Shell Programming and Scripting

Print entire line based on value in a column

Friends, File1.txt abc|0|xyz 123|129|opq def|0|678 890|pqw|sdf How do I print the entire line where second column has value is 0? Expected Result: abc|0|xyz def|0|678 Thanks, Prashant ---------- Post updated at 02:14 PM ---------- Previous update was at 02:06 PM ---------- ... (1 Reply)
Discussion started by: ppat7046
1 Replies

4. Shell Programming and Scripting

Sort based on string lenght.

I'm not familiar with find. If i use find in a certain directory i want it to show based on hierarchy. find . type d fol1 fol1/subfol1 fol1/subfol1/subfol1 fol2 fol2/subfol2 i want it to show like this fol1/subfol1/subfol1 fol1/subfol1 fol1 fol2/subfol2 fol2 do i need to use... (5 Replies)
Discussion started by: ryandegreat25
5 Replies

5. Shell Programming and Scripting

Need Awk command to get part of string based on delimeter

HI, Need awk command to get date and time alone from Input : "15:29:15 28.08.2010|SCHEDULE: Started program POSG1" Output expected : "15:29:15 28.08.2010" Please help. (9 Replies)
Discussion started by: shanneykar
9 Replies

6. Shell Programming and Scripting

Print only matched string instead of entire line

Hi, I have a file whose lines are something like Tchampionspsq^@~^@^^^A^@^@^@^A^A^Aÿð^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^?ð^@^@^@^@^@^@^@?ð^@^@^@^@^@^@pppsq^@~^@#@^@^@^@^@^@^Hw^H^@^@^@^K^@^@^@^@xp^At^@^FTtime2psq^@ ~^@^^^A^@^@^@^B^A I need to extract all words matching T*psq from the file. Thing is... (4 Replies)
Discussion started by: shekhar2010us
4 Replies

7. Shell Programming and Scripting

Grep a part of file based on string identifiers

consider below file contents cat myOutputFIle.txt 8 CCM-HQE-ResourceHealthCheck: Resource List : No RED/UNKNOWN resource Health entries found ---------------------------------------------------------- 9 CCM-TraderLogin-Status: Number of logins: 0... (4 Replies)
Discussion started by: vivek d r
4 Replies

8. Shell Programming and Scripting

Match part of string in file2 based on column in file1

I have a file containing texts and indexes. I need the text between (and including ) INDEX and number "1" alone in line. I have managed this: awk '/INDEX/,/1$/{if (!/1$/)print}' file1.txt It works for all indexes. And then I have second file with years and indexes per year, one per line... (3 Replies)
Discussion started by: phoebus
3 Replies

9. Shell Programming and Scripting

Print entire line only if certain fixed character matches the string

Hi All, I have a file testarun.txt contains the below lines and i want to print the lines if the character positions 7-8 matches 01. 201401011111 201401022222 201402013333 201402024444 201403015555 201403026666 201404017777 201404028888 201405019999 201405020000 I am trying the... (4 Replies)
Discussion started by: Arunprasad
4 Replies

10. Shell Programming and Scripting

Avoid printing entire line if string not found

so im searching the process table with: ps -ef | awk -F"./rello.java" '{ print substr($0, index($0,$2)) }' I only want it to print everything that's infront of the "./rello.java". That's because im basically getting the arguments that was passed to the rello.java script. this works. ... (2 Replies)
Discussion started by: SkySmart
2 Replies
sort(1) 						      General Commands Manual							   sort(1)

NAME
sort - sort or merge files SYNOPSIS
output] char] keydef] [kmem]] recsz] dir] [file ...] char] keydef] [kmem]] recsz] dir] [file ...] DESCRIPTION
performs one of the following functions: 1. Sorts lines of all the named files together and writes the result to the specified output. 2. Merges lines of all the named (presorted) files together and writes the result to the specified output. 3. Checks that a single input file is correctly presorted. The standard input is read if is used as a file name or no input files are specified. Comparisons are based on one or more sort keys extracted from each line of input. By default, there is one sort key, the entire input line. Ordering is lexicographic by characters using the collating sequence of the current locale. If the locale is not specified or is set to the locale, then ordering is lexicographic by bytes in machine-collating sequence. If the locale includes multi-byte characters, single-byte characters are machine-collated before multi-byte characters. Behavior Modification Options The following options alter the default behavior: Sorts on a byte-by-byte basis using each character's encoded value. On some systems, extended characters will be considered negative values, and so sort before ASCII characters. If you are sorting ASCII characters in a non-C/POSIX locale, this flag performs much faster. Check that the single input file is sorted according to the ordering rules. No output is produced; the exit code is set to indicate the result. Merge only; the input files are assumed to be already sorted. The argument given is the name of an output file to use instead of the standard output. This file can be the same as one of the input files. Unique: suppress all but one in each set of lines having equal keys. If used with the option, check to see that there are no lines with duplicate keys, in addition to checking that the input file is sorted. The amount of main memory used by the sort can have a large impact on its performance. If this option is omitted, begins using a system default memory size, and continues to use more space as needed. If this option is presented with a value, kmem, starts using that number of kilobytes of memory, unless the administrative minimum or maximum is violated, in which case the corresponding extremum will be used. Thus, is guaranteed to start with minimum memory. By convention, (with no argument) starts with maximum memory. The size of the longest line read is recorded in the sort phase so that buffers can be allocated during the merge phase. If the sort phase is omitted via the or options, a popular system default size will be used. Lines longer than the buffer size will cause to terminate abnor- mally. Supplying the actual number of bytes in the longest line to be merged (or some larger value) will prevent abnor- mal termination. Use dir as the directory for temporary scratch files rather than the default directory, which is is one of the following, tried in order: the directory as specified in the environment variable; and finally, Ordering Rule Options When ordering options appear before restricted sort key specifications, the ordering rules are applied globally to all sort keys. When attached to a specific sort key (described below), the ordering options override all global ordering options for that key. The following options override the default ordering rules: Quasi-dictionary order: only alphanumeric characters and blanks (spaces and tabs), as defined by are significant in comparisons (see envi- ron(5)). (UNIX Standard only, see standards(5)) The behavior is undefined for a sort key to which -i or -n also applies. Fold letters. Prior to being compared, all lowercase letters are effectively converted into their uppercase equivalents, as defined by In non-numeric comparisons, ignore all characters which are non-printable, as defined by For the ASCII character set, octal character codes 001 through 037 and 0177 are ignored. The sort key is restricted to an initial numeric string consisting of optional blanks, an optional minus sign, zero or more digits with optional radix character, and optional thousands separators. The radix and thousands separator characters are defined by The field is sorted by arithmetic value. An empty (missing) numeric field is treated as arithmetic zero. Leading zeros and plus or minus signs on zeros do not affect the ordering. The option implies the option (see below). Reverse the sense of comparisons. Compare as months. The first several non-blank characters of the field are folded to uppercase and compared with the langinfo(5) items < < ... < An invalid field is treated as being less than string. For example, American month names are compared such that < < ... < An invalid field is treated as being less than all months. The option implies the option (see below). Field Separator Options The treatment of field separators can be altered using the options: Use char as the field separator character; char is not considered to be part of a field (although it can be included in a sort key). Each occurrence of char is significant (for example, <char><char> delimits an empty field). If is not spec- ified, <blank> characters will be used as default field separators; each maximal sequence of <blank> characters that follows a non-<blank> character is a field separator. Ignore leading blanks when determining the starting and ending positions of a restricted sort key. If the option is specified before the first option pos1 argument), it is applied to all options pos1 arguments). Otherwise, the option can be attached independently to each field_start or field_end option pos1 or pos2 argument; see below). Note that the option is only effective when restricted sort key specifica- tions are given. Restricted Sort Key The keydef argument defines a restricted sort key. The format of this definition is field_start[type][,field_end[type]] which defines a key field beginning at field_start and ending at field_end. The characters at positions field_start and field_end are included in the key field, providing that field_end does not precede field_start. A missing field_end means the end of the line. Fields and characters within fields are numbered starting with Note that this is different than the obsolete form of restricted sort keys, where numbering starts at See below. Specifying field_start and field_end involves the notion of a field, a minimal sequence of characters followed by a field separator or a new-line. By default, the first blank of a sequence of blanks acts as the field separator. All blanks in a sequence of blanks are considered to be part of the next field; for example, all blanks at the beginning of a line are considered to be part of the first field. The arguments field_start and field_end each have the form which are optionally followed by one or more of the type options or These modifiers have the functionality for this key only, that their command-line counterparts have for the entire record. A field_start position specified by is interpreted to mean the nth character in the mth field. A missing n means indi- cating the first character of the mth field. If the option is in effect, n is counted from the first non-blank charac- ter in the mth field. A field_end position specified by is interpreted to mean the nth character in the mth field. If n is missing, the mth field ends at the last character of the field. If the option is in effect, n is counted from the first non-<blank> character in the mth field. Multiple options are permitted and are significant in command line order. A maximum of 9 options can be given. If no option is specified, a default sort key of the entire line is used. When there are multiple sort keys, later keys are compared only after all earlier keys compare equal. Lines that otherwise compare equal are ordered with all bytes sig- nificant. If all the specified keys compare equal, the entire record is used as the final key. The option is intended to replace the obsolete pos1 pos2]] notation, using field_start and field_end respectively. The fully specified pos1 pos2]] form: +w.x-y.z is equivalent to: -k w+1.x+1,y.0 (if z == 0) -k w+1.x+1,y+1.z (if z > 0) Obsolete Restricted Sort Key The notation restricts a sort key to one beginning at pos1 and ending at pos2. The characters at positions pos1 and pos2 are included in the sort key (provided that pos2 does not precede pos1). A missing means the end of the line. Specifying pos1 and pos2 involves the notion of a field, a minimal sequence of characters followed by a field separator or a new-line. By default, the first blank (space or tab) of a sequence of blanks acts as the field separator. All blanks in a sequence of blanks are con- sidered to be part of the next field; for example, all blanks at the beginning of a line are considered to be part of the first field. pos1 and pos2 each have the form optionally followed by one or more of the flags A starting position specified by is interpreted to mean character n+1 in field m+1. A missing means indicating the first character of field m+1. If the flag is in effect, n is counted from the first non-blank in field m+1; refers to the first non-blank character in field m+1. A last position specified by is interpreted to mean the nth character (including separators) after the last character of the mth field. A missing means indicating the last character of the mth field. If the flag is in effect, n is counted from the last leading blank in field m+1; refers to the first non-blank in field m+1. EXTERNAL INFLUENCES
For information about the UNIX standard environment, see standards(5). Environment Variables determines the default ordering rules applied to the sort. determines the locale for interpretation of sequences of bytes of text data as characters (e.g., single- verses multibyte characters in arguments and input files) and the behavior of character classification for the and options. determines the definition of the radix and thousands separator characters for the option. determines the month names for the option. determines the language in which messages are displayed. determines the locale to use to override the values of all the other internationalization variables. determines the location of message catalogs for the processing of provides a default value for the internationalization variables that are unset or null. If is unset or null, the default value of "C" (see lang(5)) is used. If any of the internationalization variables contains an invalid setting, behaves as if all internationalization variables are set to "C". See environ(5). International Code Set Support Single- and multi-byte character code sets are supported. EXAMPLES
Sort the contents of with the second field as the sort key: Sort, in reverse order, the contents of and placing the output in and using the first two characters of the second field as the sort key: Sort, in reverse order, the contents of and using the first non-blank character of the fourth field as the sort key: Print the password file sorted by numeric user ID (the third colon-separated field): Print the lines of the presorted file suppressing all but the first occurrence of lines having the same third field: DIAGNOSTICS
exits with one of the following values: All input files were output successfully, or was specified and the input file was correctly presorted. Under the option, the file was not ordered as specified, or if the and options were both specified, two input lines were found with equal keys. This exit status is not returned if the option is not used. An error occurred such as when one or more input lines are too long. When the last line of an input file is missing a new-line character, appends one, prints a warning message, and continues. If an error occurs when accessing the tables that contain the collation rules for the specified language, prints a warning message and defaults to the locale. If a or option is specified for a language with multi-byte characters, prints a warning message and ignores the option. WARNINGS
Numbering of fields and characters within fields option) has changed to conform to the POSIX standard. Beginning at HP-UX Release 9.0, the option numbers fields and characters within fields, starting with Prior to HP-UX Release 9.0, numbering started at A field separator specified by the option is recognized only if it is a single-byte character. The character type classification categories and are not defined for multi-byte characters. For languages with multi-byte characters, all characters are significant in comparisons. For non-text input files, the behaviour is undefined. AUTHOR
was developed by OSF and HP. FILES
SEE ALSO
comm(1), join(1), uniq(1), environ(5), lang(5), standards(5). STANDARDS CONFORMANCE
sort(1)
All times are GMT -4. The time now is 08:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy