problem if/else statement with awk
Hi all !
I tried to get some help on another forum but no success. I have a large pipe delimited file (20 fields/3,000 records) that looks like that: Code:
AAA|BBB|11111|22222|...|($NF of record 1) In order to obtain this: Code:
AAA|BBB|11111|22222|...|($NF of record 1) Code:
gawk 'BEGIN{FS=OFS="|"}{a[$1$2]=(a[$1$2]?a[$1$2]:$1 FS $2) FS $3 FS $4 FS $5 FS $6 FS $7 FS $8 FS $9 FS $10 FS $11 FS $12 FS $13 FS $14 FS $15 FS $16 FS $17 FS $18 FS $19 FS $20}END{for(i in a) print a[i]}' input When I redirect the command to an output file and count the number of lines (with wc -l), it also gives me 200 records. BUT when I open the file with my usual text editor, the duplicated 1st and 2nd fields just disappeared and the remaining fields are written in a new line (giving me the same number of lines as the input): Code:
AAA|BBB|11111|22222|...|($NF of record 1) Have you already got this problem?!!! Is something wrong with my command? Thanks for your help. Remark: - Both terminal and text editor use Unicode (UTF-8) - It worked properly when I print from 3rd up to 19th field |
Sounds a bit like an end-of-record issue; you're not using an MSWin editor by any chance?
|
Yes, you are right.
The text editor was faulty (it never did that before, it is weird). I realised that when I noticed there was a special character at the end of the last field in the last record. I switched to TextEdit and it works now. Thanks chrism01 ! |
Glad to help :)
|
All times are GMT -5. The time now is 09:49 AM. |