Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Finding difference in 1st field for rows of data Post 302767765 by co21ss on Thursday 7th of February 2013 11:52:11 AM
Old 02-07-2013
Finding difference in 1st field for rows of data

I have a file that has multiple lines, of grouped data, that typically all have the same values in the 1st field, however, I would like to search the 1st field for any differences and set a flag to use in an "if" statement to run some other routine.

An example of the typical file is below, which is a comma separated file:

Code:
1st field     2nd field     3rd field
   "20"  ,       "file1"     ,   "xxxx"
   "20"  ,       "file1"     ,   "xxxx"
   "20"  ,       "file1"     ,   "xxxx"
   "20"  ,       "file1"     ,   "xxxx"
   "20"  ,       "file2"     ,   "yyyy"
   "20"  ,       "file2"     ,   "yyyy"
   "20"  ,       "file2"     ,   "yyyy"
   "20"  ,       "file2"     ,   "yyyy"
   "20"  ,       "file3"     ,   "zzzz"
   "20"  ,       "file3"     ,   "zzzz"
   "20"  ,       "file3"     ,   "zzzz"
   "20"  ,       "file3"     ,   "zzzz"

I want to set a flag for each data set of the 3rd field, if the file has differences in the 1st field like below:
Code:
1st field     2nd field     3rd field
   "20"  ,       "file1"     ,   "xxxx"
   "1"   ,       "file1"     ,   "xxxx"
   "20"  ,       "file1"     ,   "xxxx"
   "20"  ,       "file1"     ,   "xxxx"
   "1"  ,       "file2"     ,   "yyyy"
   "20"  ,       "file2"     ,   "yyyy"
   "20"  ,       "file2"     ,   "yyyy"
   "20"  ,       "file2"     ,   "yyyy"
   "20"  ,       "file3"     ,   "zzzz"
   "1"  ,       "file3"     ,   "zzzz"
   "20"  ,       "file3"     ,   "zzzz"
   "20"  ,       "file3"     ,   "zzzz"

One method I've tried so far is to strip out the lines where the 3rd field is all the same, and then try to run through each line and check the 1st field for differences.

I've tried the following, but can't get it to work.
Code:
 awk -F ',' '{f = $1} {for (i=1;i<=NR;i++) if ($1 != $f) {printf "%s\n", "FAIL"} }' tmpfile > fail.log


Last edited by co21ss; 02-07-2013 at 02:29 PM.. Reason: Add awk method used
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Difference between two rows

Dears, I have a list as follows, 2 4 8 If I want to find the difference between two consecutive rows. Then I have to store the specific rows in two variables and then find the difference. Could someone tell how this can be done. Regards, (7 Replies)
Discussion started by: JimJim
7 Replies

2. Shell Programming and Scripting

Finding different rows

I have two files. They are prety much the same, except a few lines. I would like to get the differences between the two file, but only those differences and nothing more. "diff" and "sdiff" cannot seem to do it. "sdiff -s" gets close, but I still get "less than" or "greater than" signs in the... (2 Replies)
Discussion started by: dombi
2 Replies

3. Shell Programming and Scripting

Extract difference of two columns from different rows

Hello guys, Please help me to solve this problem. I have tried some awk commands but couldn't succeed. I have a tab delimited file where each record is separated by ------ and 4th column of each record is same. <INPUT FILE> ------ peon 53931587 53931821 ... (12 Replies)
Discussion started by: sam_2921
12 Replies

4. Shell Programming and Scripting

Difference between corresponding elements of successive rows

Hi, I have a file in the following format a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3 a4 b4 c4 d4 I need a script to find the difference between corresponding values of successive rows. So the output would have one less row than the input file and should look like: a2-a1 b2-b1 c2-c1 d2-d1... (4 Replies)
Discussion started by: sajal.bhatia
4 Replies

5. UNIX for Dummies Questions & Answers

Suggestion to convert data in rows to data in columns

Hello everyone! I have a huge dataset looking like this: nameX nameX 0 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ............... nameY nameY 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... nameB nameB 0 1 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... (can be several thousands of codes) and I need... (8 Replies)
Discussion started by: kush
8 Replies

6. Shell Programming and Scripting

finding difference between two files

Hi, I have two files one with 12486 lines second one with 13116 As per the comparsion between two files the count have 630 difference I used diff command to find the difference between two files but it's not understandable could any one suggest any command to get 630 records in a new... (4 Replies)
Discussion started by: thelakbe
4 Replies

7. Shell Programming and Scripting

Summing up rows data regarding 1st column

Dear all, I have one file like LABEL A B C D E F G H I J K L M N G02100 64651.3 25630.7 8225.21 51238 267324 268005 234001 52410.9 18598.2 10611 10754.7 122535 267170 36631.4 G02100 12030.3 8260.15 8569.91 ... (4 Replies)
Discussion started by: AAWT
4 Replies

8. Shell Programming and Scripting

Need help in finding in date difference

Hi, My date is coming as STARTDATE=Sun Jul 15 00:34:23 2012 ENDDATE=Sun Jul 15 00:50:04 2012I want difference between these two dates,anyone's helps will be appriciated. Thanks Prasoon (3 Replies)
Discussion started by: prasson_ibm
3 Replies

9. Shell Programming and Scripting

Finding first difference between two files

Hi! I'd like to know if it is possible for a command to find the first difference between two large files, output that line from both file and stop, so no need to continue after that to save some computation time. I don't think looping through it will be efficient enough but that's the only... (6 Replies)
Discussion started by: Mojing
6 Replies

10. Shell Programming and Scripting

Finding difference in 2 different timestamps

Legends, I have a requirement to run the script exactly after one hour of completion of dependent script. Eg: Script B should run after one hour on the completion of Script A. I got the time stamps using following variables. these scripts runs in autosys > DATE=`date +%H:%M` >... (4 Replies)
Discussion started by: sdosanjh
4 Replies
sort5(1)						      General Commands Manual							  sort5(1)

Name
       sort5 - internationalized System 5 sort and/or merge files

Syntax
       sort5 [-cmu] [-ooutput] [-ykmem] [-zrecsz] [-X] [-dfiMnr] [-btx] [+pos1 [-pos2]] [files]

Description
       The  command  sorts lines of the named files together and writes the result on the standard output.  The standard input is read if a hyphen
       (-) is used as a file name or if no input files are named.

       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,  and  ordering is determined by the collating sequence specified by the LC_COLLATE locale. The LC_COLLATE locale is controlled by the
       settings of either the LANG or LC_COLLATE environment variables. See for more information.

Options
       The following options alter the default behavior:

       -c   Checks that the input file is sorted according to the ordering rules; gives no output unless the file is out of order.

       -m   Merges only; the input files are already sorted.

       -u   Suppresses all but one in each set of lines having equal keys.

       -ooutput
	    Specifies the name of an output file to use instead of the standard output.  The file may be the same as one of  the  inputs.   Blanks
	    between -o and output are optional.

       -ykmem
	    Specifies  the  number  of	kilobytes  of  memory  to use when sorting a file.  If this option is omitted, sort5 begins using a system
	    default memory size, and continues to use more space as needed.  If kmem is specified, sort5 starts using that number of kilobytes	of
	    memory.   If  the administrative minimum or maximum is violated, the value of the corresponding minimum or maximum is used.  Thus, -y0
	    is guaranteed to start with minimum memory.  By convention, -y (with no argument) starts with maximum memory.

       -zrecsz
	    Records the size of the longest line read in the sort phase so buffers can be allocated during the merge phase.  If the sort phase	is
	    omitted using either the -c or -m options, a system default size is used.  Lines longer than the buffer size cause to terminate abnor-
	    mally.  Supplying the actual number of bytes (or some larger value) in the longest line to be merged prevents abnormal termination.

       -X   Sorts using tags. Upon input each key is converted to a tag value which is sorted efficiently. This option makes international sorting
	    faster but it consumes more memory since both key and tag must be stored.

       The following options override the default ordering rules:

       -d   Specifies Dictionary order.  Only letters, digits and blanks (spaces and tabs) are significant in comparisons.

       -f   Folds lower case letters into upper case.

       -i   Ignores characters outside the ASCII range 040-0176 in non-numeric comparisons.

       -n   Sorts  an  initial	numeric  string, consisting of optional blanks, optional minus sign, and zero or more digits with optional decimal
	    point, by arithmetic value.  The -n option implies the -b option, which tells the command to ignore leading  blanks  when  determining
	    the starting and ending positions of a restricted sort key.

       -r   Reverses the sense of comparisons.

       When ordering options appear before restricted sort key specifications, the requested ordering rules are applied globally to all sort keys.
       When attached to a specific sort key (described below), the specified ordering options override all global ordering options for that key.

       The notation +pos1 -pos2 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 -pos2 means the end of the line.

       Specifying  pos1  and pos2 involves the notion of a field, that is 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.	The blank can be either a space  or  a	tab.   All
       blanks  in a sequence of blanks are interpreted as a part of the next field; for example, all blanks at the beginning of a line are consid-
       ered to be part of the first field.  The treatment of field separators is altered using the following options:

       -tx  Uses x as the field separator character.  Although it may be included in a sort key, x is not considered part of a field.  Each occur-
	    rence of x is significant (for example, xx delimits an empty field).

       -b   Ignores  leading  blanks  when  determining the starting and ending positions of a restricted sort key.  If the -b option is specified
	    before the first +pos1 argument, it is applied to all +pos1 arguments.  Otherwise, the b flag may be attached  independently  to  each
	    +pos1 or -pos2 argument.

       Pos1  and  pos2	each  have  the form m.n optionally followed by one or more of the flags bdfinr.  A starting position specified by +m.n is
       interpreted to mean the n+1st character in the m+1st field.  A missing .n means .0, indicating the first character of the m+1st field.	If
       the  b  flag  is  in  effect n is counted from the first non-blank in the m+1st field; +m.0b refers to the first non-blank character in the
       m+1st field.

       A last position specified by -m.n is interpreted to mean the nth character (including separators) after the last  character  of	the  m	th
       field.  A missing .n means .0, indicating the last character of the mth field.  If the b flag is in effect n is counted from the last lead-
       ing blank in the m+1st field; -m.1b refers to the first non-blank in the m+1st field.

       When there are multiple sort keys, later keys are compared only after all earlier keys are found to be equal.  Lines that otherwise compare
       equal are ordered with all bytes significant.

Examples
       Sort the contents of infile with the second field as the sort key:

	      sort5 +1 -2 infile

       Sort,  in  reverse  order,  the	contents of infile1 and infile2, placing the output in outfile and using the first character of the second
       field as the sort key:

	      sort5 -r -o outfile +1.0 -1.2 infile1 infile2

       Sort, in reverse order, the contents of infile1 and infile2 using the first non-blank character of the second field as the sort key:

	      sort5 -r +1.0b -1.1b infile1 infile2

       Print the password file sorted by the numeric user ID (the third colon-separated field):

	      sort5 -t: +2n -3 /etc/passwd

       Print the lines of the already sorted file infile, suppressing all but the first occurrence of lines  having  the  same	third  field  (the
       options -um with just one input file make the choice of a unique representative from a set of equal lines predictable):

	      sort5 -um +2 -3 infile

Diagnostics
       Comments  and exits with non-zero status for various trouble conditions (for example, when input lines are too long), and for disorder dis-
       covered under the -c option.

       When the last line of an input file is missing a new-line character, sort5 appends one, prints a warning message, and continues.

Files
       /usr/tmp/stm???

See Also
       comm(1), join(1), uniq(1), setlocale(3int), strcoll(3int)

																	  sort5(1)
All times are GMT -4. The time now is 08:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy