LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem if/else statement with awk (https://www.linuxquestions.org/questions/linux-newbie-8/problem-if-else-statement-with-awk-4175441370/)

beca1234567 12-13-2012 06:06 PM

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)
CCC|DDD|33333|44444|...|($NF of record 2)
CCC|DDD|55555|66666|...|($NF of record 3)

For the lines with same 1st and 2nd fields, I write the 3rd up to 20th fields at the end of the previous line.
In order to obtain this:
Code:

AAA|BBB|11111|22222|...|($NF of record 1)
CCC|DDD|33333|44444|...|($NF of record 2)|55555|66666|...|($NF of record 3)

I use this command:
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 my terminal it works perfectly (output has 200 records).
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)
CCC|DDD|33333|44444|...|($NF of record 2)
|55555|66666|...|($NF of record 3)

Plus I tested the command with an input file with just 3 rows (like the example above), and it worked.

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

chrism01 12-13-2012 06:10 PM

Sounds a bit like an end-of-record issue; you're not using an MSWin editor by any chance?

beca1234567 12-13-2012 07:22 PM

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 !

chrism01 12-13-2012 07:47 PM

Glad to help :)


All times are GMT -5. The time now is 09:49 AM.