Results 1 to 6 of 6
Hi guys, basically i'm writing a script which cuts values from a file and counts the number of unique values. The problem I'm having is that basically it shows in ...
- 05-01-2010 #1Just Joined!
- Join Date
- Apr 2010
- Posts
- 6
problem cutting values
Hi guys, basically i'm writing a script which cuts values from a file and counts the number of unique values. The problem I'm having is that basically it shows in two different tables one above the other, when I want it in one.
the text file looks like this
123456 mr smith 6 purchases oct 2007
000012 mr jones 2 purchases nov 2000
the code i have cuts it works fine and looks like grep $VALUE cut -d f1
this is the code i'm using to count the unique files;
file | awk '{print $1}' | sort | uniq | wc -l
i can't seem to be able to put the cut code with the sorting code together without it either printing the unique characters of the entire text only, or just printing out the data with the number underneath e.g.
123456 mr jones 6 purchases oct 2007
000012 mr smith 2 purchases nov 2000
12
i've tried putting the sorting part as a variable e.g. UNIQUE = file | awk '{print $1}' | sort | uniq | wc -l then put awk print '{print $1, $2, $3, $UNIQUE}' but that hasn't worked either
any help would be great cheers
- 05-01-2010 #2
It works? Seems to me it shouldn't

You mean cat file | awk ..., or something alike, of course. Right?
Sorry, but I can't really understand what you expect.
Print an example of what you want the result to be.
Really? You have to be more carefully with your copy and paste
Doesn't seems to make any sense. On the other hand, awk is a powerful but complex tool.
Regards
Luis
- 05-02-2010 #3Just Joined!
- Join Date
- Apr 2010
- Posts
- 6
ok sorry about that, basically the outcome i want is to have a script which extracts specific data from a file then displays the unique characters from column 1 e.g. i write oct 2008
123456 mr smith 2 purchases oct 2008 3 unique customer numbers
000012 mr jones 4 purchases oct 2008
321456 mr bloggs 3 purchases oct 2008
123456 mr smith 1 purchase oct 2008
extracting the data isn't a problem i use cut something like
grep `echo $INPUT | cut -d " " -f 1`. for each column
i then use awk to display the outcome which looks like
cust id name purchases date
123456 jones 1 oct 2008
to get the unique column i've been trying to use
filename| awk ' {print $2}' | sort | uniq | wc -l
i've tried merging them together like
grep 'echo $INPUT | cut -d " " -f 1` filename| awk '{print $1}' | sort | uniq | wc -l
but that just prints the unique number and nothing else. i think what goes wrong is the searching for the unique values part needs to be seperate from the cutting part, but when i try to do that they end up not in the same table
- 05-02-2010 #4
- 05-02-2010 #5Just Joined!
- Join Date
- Apr 2010
- Posts
- 6
ok i have this
customer_id name date no.purchases
123456 mr jones oct 08 1
000001 mr bloggs oct 08 2
000002 mr smith oct 08 1
123456 mr jones oct 08 3
i want it so i can display the unique customer ids so like
customer_id name date no.purchases unique ids
123456 mr jones oct 08 1 3
000001 mr bloggs oct 08 2
000002 mr smith oct 08 1
123456 mr jones oct 08 3
- 05-02-2010 #6
I think I see now. You want to append to the first line of the list of purchases of a month the number of clients that did purchases that month.
With the tools you're working a title line like
October - 3 clients did purchases
followed by the list will be much simpler
Or two lists. One with the total for each month and the purchase list you already have.
If you really must do it that way, this will probably work
$ awk '{
if (NR==1)
print $0,unique_id
else
print
}' unique_id=$(sort file |uniq -w 7 |wc -l) file
As you see, you'll have to go twice through the file.
Replace file with your filename
and check
The GNU Awk User's Guide
Again, I think it's not the best way to do it, but I don't know it all about your needs
Hope it helps, get back with any doubts
Luis


Reply With Quote
