LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-13-2012, 06:06 PM   #1
beca1234567
LQ Newbie
 
Registered: Dec 2012
Posts: 2

Rep: Reputation: Disabled
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
 
Old 12-13-2012, 06:10 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Sounds a bit like an end-of-record issue; you're not using an MSWin editor by any chance?
 
1 members found this post helpful.
Old 12-13-2012, 07:22 PM   #3
beca1234567
LQ Newbie
 
Registered: Dec 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
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 !
 
Old 12-13-2012, 07:47 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Glad to help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk else if statement dazdaz Programming 2 05-19-2012 05:56 AM
[SOLVED] Getting -nan in output of awk statement sparker1970 Linux - Newbie 3 04-06-2012 10:45 AM
[SOLVED] Problem with if statement in awk in AIX sukhdip AIX 3 01-31-2012 02:28 AM
[SOLVED] variable substitution in awk statement emmalg Linux - Software 12 07-02-2009 08:39 AM
how to keep Field Separator in AWK when using a sub statement tmcguinness Programming 4 02-09-2009 02:24 PM


All times are GMT -5. The time now is 11:13 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration