Help with awk
Hi Team
I have a file with below info Code:
COL1 | COL2 | COL3 tried this awk '!x[$1]++' file <-- is removing whole line |
You don't provide a very thorough description of your task, so that I have to make a few assumptions.
1. Input: Entries are collated, perhaps also sorted according to col1. That is, all A1 entries are kept together, all B1 entries etc. 2. Output: Essentially identical to the input, except that each col1 values appears only once. In this case, I would write an awk program that checks whether the value in col1 has changed. When it detects a change, it prints col1. Otherwise, it doesn't, but prints all other fields. Here is a possible fragment: Code:
$1 != previous_col1 { printf $1 } # value in col1 changed EDIT: Another solution is using the sub() function to replace the A1, B1 etc by a string of blanks. This way, you don't have to worry about re-creating the pretty formatting. |
Sorry for missing the detail info
I have a file which was provided , Col1/Field 1 will always have duplicate data the rest of the fields may/may not but am not worried of other columns, all I need is if there is duplicate data it need to be printed with empty space and the values of Field 1 will be ordered so no values repeats further down the rows. Hope this is clear. I tried your code - but I do see errors. - can you let me know where I made mistake $ cat /tmp/A1|awk '$1 != previous_col1 { printf $1 } { previous_col1 = $1 for (col=2;col<=$NF;col++) printf $col " " }' awk: syntax error at source line 1 context is $1 != previous_col1 { printf $1 } { previous_col1 = $1 >>> for <<< (col=2;col<=$NF;col++) printf $col " " } awk: illegal statement at source line 1 |
I have to say there is excellent documentation on awk. Is this homework?
|
no homework
writing a script struck at the end/final part. tried to do my best as said am a new bee so is asking for help on how to achieve |
Quote:
I agree that the awk user guide is pretty good, and that there are many tutorials out there that help you come up to this level of awk programming. Its worthwhile investing a few hours to learn this tool. |
It would help if you were to use [code] [/code] tags when posting scripts. There was an extraneous dollar sign changing how the NF field was being used in the for loop, and a missing output field separator:
Code:
#!/usr/bin/awk -f |
You can play with how to fix up the column alignments, but you can simply do:
Code:
awk '{if($1 == prev)$1 = "";else prev = $1}1' file |
Quote:
|
All times are GMT -5. The time now is 10:36 PM. |