|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Uniq code in sorted order
Hi All, A small question: I have a file check.txt which looks like below: Code:
KRPROD2,2012-11-20 08:46:50:408,ODK3325688102 KRPROD2,2012-11-20 08:47:35:289,ODK3325688102 KRPROD2,2012-11-20 08:47:35:446,ODK3325688102 KRPROD2,2012-11-20 08:48:32:973,ODK3325689120 KRPROD2,2012-11-20 09:29:17:833,ODK3325689120 KRPROD2,2012-11-20 09:29:17:912,ODK3325689120 I want to get below output Code:
KRPROD2,2012-11-20 08:46:50:408,ODK3325688102 KRPROD2,2012-11-20 08:48:32:973,ODK3325689120 which is first instance of field3 in the file. Please help Last edited by Scrutinizer; 11-20-2012 at 12:19 AM.. Reason: code tags |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
Code:
awk -F, '!c[$3]++' file |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Thanks so much...it works.
Can you please help to explain as i m new bie in unix |
|
#4
|
|||
|
|||
|
Code:
awk -F, '!c[$3]++' file The -F, sets the field separator to a <comma>. The c[$3] is an array indexed by the contents of field 3. An unintialized variable in awk has value 0 (or empty string) depending on context). The first time it is evaluated !c[$3] (NOT)(value of c[$3]), is (NOT)zero which evaluates to TRUE. When the value is TRUE the action associated with this awk statement is performed. Since there is no action given, the default action (print the line) is performed. Then the ++ increments the value of c[$3] so that when this test is performed again on lines with the same 3rd field, the test !c[$3] will evaluate to FALSE because (NOT)(positive integer value) evaluates to zero. When the test evaluates to FALSE, the action associated with this line is not performed so subsequent lines with the same value in the 3rd field are not printed. This test is performed once for each line in the file in order from beginning to end. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Thanks for detailed explanation But when I am using below line/command its giving me error; Code:
ssh ${fmsServerUserName}@${fmsServerName} "awk -F, '!c[$3]++' 10_FMS_CRXtoFMS.csv" >> 10_FMS_CRXtoFMS.csv
Error:
awk: !c[]++
awk: ^ syntax error
awk: fatal: invalid subscript expressionPlease help Last edited by Scrutinizer; 11-20-2012 at 12:19 AM.. Reason: code tags |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Hi escape ! and $ chars. it will work. Code:
ssh ${fmsServerUserName}@${fmsServerName} "awk -F, '\!c[\$3]++' 10_FMS_CRXtoFMS.csv" >> 10_FMS_CRXtoFMS.csvLast edited by Scrutinizer; 11-20-2012 at 12:21 AM.. Reason: code tags |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Quote:
Then when you invoked awk, the script that it was given was '!c[]++' and you got the syntax error from awk. I don't use ssh much, but adding the backslash escape as shown in red in the following should get rid of the syntax error for you: Code:
ssh ${fmsServerUserName}@${fmsServerName} "awk -F, '!c[\$3]++' 10_FMS_CRXtoFMS.csv" >> 10_FMS_CRXtoFMS.csvIt isn't clear to me whether the two references to 10_FMS_CRXtoFMS.csv are two references to the same file or references to different files with the same name on different servers. If you are appending output to a file that you're using for input, bad things are very likely to happen.
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Read filenames in sorted order | nishantrk | Shell Programming and Scripting | 10 | 02-01-2012 06:24 AM |
| sort the files based on timestamp and execute sorted files in order | saidutta123 | Shell Programming and Scripting | 1 | 09-18-2011 02:47 PM |
| Sorted file | ksailesh | UNIX for Advanced & Expert Users | 3 | 11-17-2009 09:59 PM |
| executing code on files in the sorted order -help! | epi8 | Shell Programming and Scripting | 1 | 05-20-2008 03:30 AM |
| sorted processes | pro | Shell Programming and Scripting | 7 | 05-19-2007 03:52 AM |
|
|