this is the code that i am using in an attempt to get all the unique values that occur in the fourth
field of each record in a file that i call from the command line.
#! /bin/awk -f
BEGIN {RS = "\n"; FS = " "}
{
array123[$4] = 1
#assign a bogus value to the "key" of the 4th field
for (i in array123) {print i}
# print out each unique value
}
END {
print "processing done"
}
sample data file :
123 abc 987 widget1 873
138 deabc 987 widget221 983
1298 ajc 987 widget8 8731
249 abk 963 widget23 93
123 abc 917 widget1 983
120 abc 97 widget23 8573
the output i am getting is each of the widget numbers in the above data file, when what i want is
each of the unique values (widget1, widget221, widget8, and widget23).
suggestions?
thanks
joe
ps. this works : awk -f program_file_name data_file_name | sort | uniq
but i would prefer to do all of it in awk so i can understand what i am doing wrong
|
|
0
|
|
|
|
Reply
|
Joseph
|
6/11/2004 5:24:26 PM |
|
On Fri, 11 Jun 2004 11:24:26 -0600, this message was posted :
> this is the code that i am using in an attempt to get all the unique values that occur in the
> fourth field of each record in a file that i call from the command line.
>
> #! /bin/awk -f
>
> BEGIN {RS = "\n"; FS = " "}
>
> {
> array123[$4] = 1
> #assign a bogus value to the "key" of the 4th field
>
> for (i in array123) {print i}
> # print out each unique value
> }
>
> END {
> print "processing done"
> }
>
> sample data file :
>
> 123 abc 987 widget1 873
> 138 deabc 987 widget221 983
> 1298 ajc 987 widget8 8731
> 249 abk 963 widget23 93
> 123 abc 917 widget1 983
> 120 abc 97 widget23 8573
>
>
> the output i am getting is each of the widget numbers in the above data file, when what i want is
> each of the unique values (widget1, widget221, widget8, and widget23).
>
> suggestions?
>
> thanks
>
> joe
>
> ps. this works : awk -f program_file_name data_file_name | sort | uniq but i would prefer to do
> all of it in awk so i can understand what i am doing wrong
never mind.
i figured it out.
should have done the printing of unique values inside the END{} instead of the main part of the
code.
joe
|
|
0
|
|
|
|
Reply
|
Joseph
|
6/11/2004 6:01:11 PM
|
|
Joseph Paish wrote:
> this is the code that i am using in an attempt to get all the unique values that occur in the fourth
> field of each record in a file that i call from the command line.
<snip>
> ps. this works : awk -f program_file_name data_file_name | sort | uniq
> but i would prefer to do all of it in awk so i can understand what i am doing wrong
No need for all of that, just this would work:
cut -d' ' -f4 data_file_name | sort -u
Regards,
Ed.
|
|
0
|
|
|
|
Reply
|
Ed
|
6/11/2004 6:13:22 PM
|
|
In article <lVlyc.1164$0A.9011@localhost>,
Joseph Paish <jpaish@freenet.edmonton.ab.ca> wrote:
>this is the code that i am using in an attempt to get all the unique
>values that occur in the fourth
>field of each record in a file that i call from the command line.
>
>#! /bin/awk -f
>
>BEGIN {RS = "\n"; FS = " "}
>
>{
> array123[$4] = 1
> #assign a bogus value to the "key" of the 4th field
>
> for (i in array123) {print i}
> # print out each unique value
>}
>
>END {
>print "processing done"
>}
>
>sample data file :
>
>123 abc 987 widget1 873
>138 deabc 987 widget221 983
>1298 ajc 987 widget8 8731
>249 abk 963 widget23 93
>123 abc 917 widget1 983
>120 abc 97 widget23 8573
>
>
>the output i am getting is each of the widget numbers in the above data
>file, when what i want is
>each of the unique values (widget1, widget221, widget8, and widget23).
awk 'a[$4]++==0 {print $4}' infile
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
|
|
0
|
|
|
|
Reply
|
demas
|
6/11/2004 6:35:05 PM
|
|
On Fri, 11 Jun 2004 12:01:11 -0600, Joseph Paish wrote:
> On Fri, 11 Jun 2004 11:24:26 -0600, this message was posted :
>> this is the code that i am using in an attempt to get all the unique values that occur in the
>> fourth field of each record in a file that i call from the command line.
>>
>> #! /bin/awk -f
[snipped script & data]
> never mind.
> i figured it out.
> should have done the printing of unique values inside the END{} instead of the main part of the
> code.
I suppose, if you want to (re)sort it in some particular way. I thought
Charles Demas solution was really neat! (1st glance I thought it was wrong)
--
Juhan Leemet
Logicognosis, Inc.
|
|
0
|
|
|
|
Reply
|
Juhan
|
10/14/2004 10:07:53 PM
|
|
|
4 Replies
1427 Views
(page loaded in 0.101 seconds)
Similiar Articles: Lookup: Howto display one field and store the value of another ...Sum the value of all records in one field - comp.databases ... Do a search in the posts of this newsgroup and you sure will find ... printing unique values in 4th field ... removing duplicate records based on specific field. - comp.unix ...You could do it in Perl (assuming the fourth field ... ane' $h{ $F[ 3 ] } = $_ }{ print ... using self-join - comp.databases ..... unique values in a specific field ... Printing from field 14 to end of record - comp.lang.awkHi, How do I print from from a certain field to ... 5 )); >> >>should print all fields after the 4th. My requirements were that the 5th >>field ... that to load the values ... Print Table of values - comp.soft-sys.math.scilab... value of all records in one field - comp.databases ... Print Table of values ... Generating unique string identifier - comp.soft-sys.sas Print Table of values - comp.soft-sys ... Combine Values Vertically - Please Help - comp.soft-sys.sas ...I am trying to get the 4th observation to to be the sum ... awk commands - comp.lang.awk... 1; i <=count; i++) print ... Relationship - finding one field based on value in ... Creating a unique alpha number serial ID - comp.databases ...Is there a format to print time values with leading zeros? - comp ... Creating a ... Create a field with a unique serial number unless you already have a unique ... Generating unique string identifier - comp.soft-sys.sasI'm trying to generate unique string identifier ... The fact is that MD5 returns a bit field, which is ... Print Table of values - comp.soft-sys.math.scilab... plot(x,y,'rx ... CSV files with some double-quoted fields with commas inside - comp ...Hi, Some CSV (comma-separated values) files have the ... formats that are referred to as "CSV", not one unique ... caterr = a string to replace "cat>&2" for printing to ... Saving the last observation of duplicate records - comp.soft-sys ...... do the sort with no duprec ie by accountnumber field ... for. > > Patricia The last ... if I have 4 unique ID's ... How best to detect duplicate values in a column? - comp ... change value and concatenate - comp.lang.awk... FS="\",\""; > OFS=","; > > } > > { > print $1 ... After the 4th field, positions are shifted down by two ... Need to concatenate numeric values into one char value ... Awk to print distinct col values - The UNIX and Linux ForumsWe don't use the actual count of occurrences, just the keys (distinct values in the second field) in the final print, but that's obviously easy to change if you want to ... Find, hide, or eliminate duplicate data - Access - Office.comFourth Coffee: Julian Price: Calle Smith 2: Mexico City: 56789 (7) 555-2126: Coho ... Set a field to contain only unique values. To implement this rule, do one of the following: 7/21/2012 9:45:12 PM
|