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 1 Week Ago   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 17 January 2018, 11:35 AM EST
Posts: 51
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 1 Week Ago   -   Original Discussion by genome
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 January 2018, 3:28 AM EST
Location: Aachen, Germany
Posts: 11,966
Thanks: 354
Thanked 3,688 Times in 3,386 Posts
There's an error in the sequence of opening and closing braces.
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 17 January 2018, 11:35 AM EST
Posts: 51
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 1 Week Ago   -   Original Discussion by genome
rdrtx1's Unix or Linux Image
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 21 January 2018, 10:35 PM EST
Location: Houston, Texas, USA
Posts: 1,021
Thanks: 0
Thanked 352 Times in 333 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 1 Week Ago   -   Original Discussion by genome
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 22 January 2018, 3:00 AM EST
Location: Simplicity
Posts: 3,947
Thanks: 335
Thanked 1,322 Times in 1,192 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 1 Week Ago   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 17 January 2018, 11:35 AM EST
Posts: 51
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 11:32 AM
IF section problem. syntax error: unexpected end of file error jedzio Shell Programming and Scripting 10 08-01-2014 10:03 AM
awk - end of line - Help! dmesserly Shell Programming and Scripting 3 04-03-2013 02:12 PM
End of file in AWK claw82 Shell Programming and Scripting 4 11-16-2009 08:12 AM



All times are GMT -4. The time now is 04:29 AM.