Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Awk: Error with END

Shell Programming and Scripting


Tags
awk, end

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-11-2018   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 22 February 2018, 1:11 PM EST
Posts: 57
Thanks: 1
Thanked 1 Time in 1 Post
Awk: Error with END

Hello,

I can't understand why there's an error with End with simple code.

Data:


Code:
rs10458597 1:564621
rs2185539 1:566875
rs11240767 1:728951
rs12564807 1:734462
rs3131972 1:752721
rs3131969 1:754182
rs3131967 1:754334



Code:
awk ' {
if(!snp[$2]){
snp[$2] = $0
}
else{
snp[$2] = snp[$2] $0 " "
}

END
{
print "awa"
}

}
 ' new_id.txt

Error:



Code:
awk: cmd. line:9: END
awk: cmd. line:9: ^ syntax error

Please guide me fix this.
Sponsored Links
    #2  
Old Unix and Linux 01-11-2018   -   Original Discussion by genome
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 May 2018, 6:41 PM EDT
Location: Aachen, Germany
Posts: 12,751
Thanks: 415
Thanked 3,921 Times in 3,605 Posts
There's an error in the sequence of opening and closing braces.
Sponsored Links
    #3  
Old Unix and Linux 01-11-2018   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 22 February 2018, 1:11 PM EST
Posts: 57
Thanks: 1
Thanked 1 Time in 1 Post
Quote:
Originally Posted by RudiC View Post
There's an error in the sequence of opening and closing braces.
Thanks for your reply.



Code:
awk ' 

{
if(!snp[$2]){ snp[$2] = $0 
cnt[$2]++
arr[++SN]=$2
}
else{ snp[$2] = snp[$2] " " $0 " " 
cnt[$2]++
}
}
END { 
for( i in snp){
if(cnt[i] == 2){
print i,snp[i]
}
}
}


 ' new_id.txt

Fixed it. Linux
    #4  
Old Unix and Linux 01-11-2018   -   Original Discussion by genome
rdrtx1's Unix or Linux Image
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
Join Date: Sep 2012
Last Activity: 25 May 2018, 3:54 PM EDT
Location: Houston, Texas, USA
Posts: 1,142
Thanks: 0
Thanked 421 Times in 398 Posts
Braces out of place. The END section will not execute. try:


Code:
awk '
{ if(!snp[$2]) {
      snp[$2] = $0
   } else {
      snp[$2] = snp[$2] $0 " "
   }
}

END {
   print "awa"
}
 ' new_id.txt

Sponsored Links
    #5  
Old Unix and Linux 01-11-2018   -   Original Discussion by genome
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 25 May 2018, 6:53 PM EDT
Location: Simplicity
Posts: 4,084
Thanks: 351
Thanked 1,379 Times in 1,243 Posts
Yes, and the indention improves readability!

Normally you should test for existence if (!($2 in snp))
and not for a zero/empty value if (!snp[$2])
Sponsored Links
    #6  
Old Unix and Linux 01-13-2018   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 22 February 2018, 1:11 PM EST
Posts: 57
Thanks: 1
Thanked 1 Time in 1 Post
Quote:
Originally Posted by MadeInGermany View Post
Yes, and the indention improves readability!

Normally you should test for existence if (!($2 in snp))
and not for a zero/empty value if (!snp[$2])
MadeInGermany: Thanks for the tip. Linux
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk until the end of file alvinoo UNIX for Dummies Questions & Answers 2 06-18-2015 10:32 AM
IF section problem. syntax error: unexpected end of file error jedzio Shell Programming and Scripting 10 08-01-2014 09:03 AM
awk - end of line - Help! dmesserly Shell Programming and Scripting 3 04-03-2013 01:12 PM
End of file in AWK claw82 Shell Programming and Scripting 4 11-16-2009 07:12 AM



All times are GMT -4. The time now is 10:31 PM.