Awk: BEGIN: prints nothing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: BEGIN: prints nothing
# 1  
Old 12-06-2017
Awk: BEGIN: prints nothing

My code fails to do anything if I've BEGIN block in it:

Run the awk script as:

Code:
awk -f ~/bin/sum_dupli_gene.awk make_gene_probe.txt

Code:
#!/usr/bin/awk -f



BEGIN {
    print ARGV[1]
#--loads of stuff
}

END{
#more stuff
}

If I remove BEGIN my code works perfectly. Can someone help me what am I doing wrong?

Last edited by genome; 12-06-2017 at 07:26 PM.. Reason: awk
# 2  
Old 12-06-2017
You have an awk script. Is it called ~/bin/sum_dupli_gene.awk make_gene_probe.txt? That looks more like the argument to -f that you'd pass into the script.

Are you sure you don't want something like:
Code:
/path-to/my/awk-script ~/bin/sum_dupli_gene.awk make_gene_probe.txt

# 3  
Old 12-06-2017
Thanks for the reply.

If I don't use -f I get error:
Code:
awk  ~/bin/sum_dupli_gene.awk make_gene_probe.txt

Code:
awk: 1: unexpected character '.'

I'm unable to fix this error.

If I don't use awk and -f then nothing gets printed

~/bin/sum_dupli_gene.awk make_gene_probe.txt
# 4  
Old 12-06-2017
Ah, sorry, I misread your post, thought that was all a path.

You don't need to invoke it with awk as you have a 'shebang' line, but if you do, yes, you will need the -f.

You're accessing the script via the full path, but the input file as a relative path. Is the input file in the current directory?

Also, which OS are you using?
# 5  
Old 12-06-2017
I am on linux
Code:
which awk

/usr/bin/awk

---------- Post updated at 06:52 PM ---------- Previous update was at 06:52 PM ----------

Yes, the input is in current directory.
Code works fine if I remove BEGIN.


Linux version 4.9.0-4-amd64 (debian-kernel@lists.debian.org) (gcc version 6.3.0 20170516 (Debian 6.3.0-18) ) #1 SMP Debian 4.9.51-1 (2017-09-28)
# 6  
Old 12-06-2017
All I can see in the BEGIN section is print ARGV[1]. There's nothing wrong with that. Are you saying it does not work just with that? "#--loads of stuff" doesn't really help us if we can't see that "stuff". Can you be more specific about what you mean by "doesn't work"?
# 7  
Old 12-06-2017
I run script as:

Code:
 ~/bin/sum_dupli_gene.awk make_gene_probe.txt

I removed ARGV[1]. The code for loads of stuff looks like:

Code:
#!/usr/bin/awk -f

#awk -F'\t' -v OFS='\t' '

BEGIN {
     if(NR==1){

	 header="AffyID"

	 for(j=2;j<=NF;j++){
	     header=header"\t"$j
	 }
     }
     #---fix headers
     
     else {
	 #--start with row second
	 
	 if($2 in gene_arr) {
	     for(k=3;k<=NF;k++){
		 #starting with 3rd column. will keep sum array with 3rd too
		 
		 sum[gene_arr[$2],k]= $k + sum[gene_arr[$2],k]
	     }
	 }
	 else {
	     #--if gene not in array
	     gene_arr[$2]=NR
	     #starting with 3rd column. will keep sum array with 3rd too
	     for (k=3;k<=NF;k++){
		 sum[NR,k] = $k;
	     }
	 }
	 
     }
 }
 #--processing ends for columns and headers
 #-------------------
 END{
     print header
     
     for (key in gene_arr){
	 
	 printf key"\t"key"\t"

	 for(z=3;z<=NF;z++){
	     #starting with 3rd column. will keep sum array with 3rd too
	     if(z==NF){ #if last columns
		 printf sum[gene_arr[key],z]
	     }

	     else{
		 printf sum[gene_arr[key],z]"\t"
	     }
	 }
	 printf "\n"
     }
 }
#--------------------------
 
#  < make_gene_probe.txt

By doesn't work I mean it prints blank lines. '\n' That's it. No print header or anything.

Last edited by genome; 12-06-2017 at 08:03 PM.. Reason: more information
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk prints only last line

data.txt: NEWTEXTS="frq=63,std=-0.00533584,time=Mar-21-(09:15:03)-2016,epoch=1458576903,avg=64.2059,212.698 frq=197,std=0.587585,time=Mar-21-(09:16:02)-2016,epoch=1458576962,avg=64.2059,483.756 frq=178,std=0.503514,time=Mar-21-(09:46:02)-2016,epoch=1458578762,avg=64.2059,500... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Shell Programming and Scripting

awk prints unwanted new lines

Hello, I need a little help with the following: I'm using AWK to read input from a comma-seperated value file, and only printing certain fields like so: awk -F "," '{print $1,$3,$6}' /list.csv | tail -1 Which outputs the following: server1 APPID OS I run into a problem... (8 Replies)
Discussion started by: LinuxRacr
8 Replies

3. UNIX for Dummies Questions & Answers

awk search with begin

Hi, I have written below script to begin if the line has n #!/bin/ksh /usr/xpg4/bin/awk {/ n / 'BEGIN {X = "01"; X = "02"; X = "03"; X = "04"; X = "05"; X = "06"; X = "07"; X = "08"; X ="09"; X = "10"; X = "11"; X = "12"; };} NR > 1 {print $1 "\t" $5 "," X "," $6 " " $7}'} input.txt |... (9 Replies)
Discussion started by: stew
9 Replies

4. Shell Programming and Scripting

BEGIN and END format in awk

I'm new to awk, trying to understand the basics. I'm trying to reset the counter everytime the program gets a new file to check. I figured in the BEGIN part it would work, but it doesn't. #!/bin/awk -f BEGIN {counter=0} { sum=0 for ( i=1; i<=NF;... (1 Reply)
Discussion started by: guitarist684
1 Replies

5. Shell Programming and Scripting

awk system() prints extra 0 -- why?

Here's the command I'm running: # echo "hi" | awk '{etime = system("hostname") ; close("hostname") ; print etime""}' And here's the ouput: server.domain.tld 0 Why in the world is that second line, the one that's just "0", there? Many thanks in advance. (2 Replies)
Discussion started by: treesloth
2 Replies

6. Shell Programming and Scripting

awk BEGIN problem

awk 'BEGIN { print "line one\nline two\nline three" }' After ./awktest.sh Usage: awk -f progfile file ... Usage: awk 'program' file ... POSIX options: GNU long options: -f progfile --file=progfile -F fs --field-separator=fs -v var=val ... (7 Replies)
Discussion started by: cola
7 Replies

7. Shell Programming and Scripting

awk getting stuck after BEGIN

I am beginner in awk awk 'BEGIN{for(i=1;(getline<"opnoise")>0;i++) arr=$1}{print arr}' In the above script, opnoise is a file, I am reading it into an array and then printing the value corresponding to index 20. Well this is not my real objective, but I have posted this example to describe... (1 Reply)
Discussion started by: akshaykr2
1 Replies

8. Shell Programming and Scripting

Alias to awk BEGIN statement

I'd like to define an alias to awk's begin statement since I use awk with different delimiters all the time and it is tiresome to type awk '{OFS="\t";FS="\t"}{BLAH BLAH}' every time. The problem is that bash won't let me make an alias with an open quote, which is necessary for the BEGIN alias to... (3 Replies)
Discussion started by: baconbasher
3 Replies

9. Shell Programming and Scripting

awk / grep / Prints all columns except the first one

I have the a file with the following data. File Content. 1249 snf06.3.0 sw version 1.1 code MD5 192F MD4 MD3 1248 sns06.3.0 sw version 1.1 code MD5 192F MD12 1250 sns06.3.0 sw version 1.1 code MD5 192F0\ MD8 1241 sns06.3.0 sw code MD5 19 1243 sn06.3.0 sw version 1.1 code MD5 19 12... (17 Replies)
Discussion started by: knijjar
17 Replies

10. Shell Programming and Scripting

Another question on awk - Begin statement

Hi, I've a question on awk. In English I want to: (a) open a file, (b) search through the file for records where length of field15 > 20 characters and (c) print out some fields in the record. I've written the following and it works OK. The trouble is this will ALWAYS write out the column... (5 Replies)
Discussion started by: eff_cee
5 Replies
Login or Register to Ask a Question