Some Awk Getline help?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Some Awk Getline help?
# 8  
Old 06-02-2010
Perhaps you are looking for something a bit like this? This would probably limit the searching:
Code:
awk '$3!=f{close(f);f=$3;n=0}{while(!p && getline x<f) if(++n==$2){p=1};print x;p=0}' subset.txt

This will open and close only those files that are specified in subset.txt and look for the right line number and print the record it finds on that line. For efficiency reasons subset.txt would best be sorted..

Output:
Code:
rs10001 900 900 100 100
rs10004 999 121 232 434


Last edited by Scrutinizer; 06-02-2010 at 04:12 PM.. Reason: "!p" and "getline x<f" meeded to be reversed
# 9  
Old 06-02-2010
Create a file with the desired serial# with the name file:
Code:
$ cat file
rs10001
rs10004

Now you can run this command in the directory with the files:
Code:
awk  '
NR==FNR{
  a[$1]=$1
  next
}
a[$1] {row[$1]=$2; file[$1]=$3}
END{
  for(i in file){
    c=0
    while((getline < file[i]) > 0) {
      ++c
      if(c==row[i]) {
        print
      }
    }
  }
}' file masterfile

# 10  
Old 06-02-2010
hmm..it's interesting but it is bringing up a syntax error but doesnt say exactly... I cut and paste directly. Any thoughts?

Thanks

---------- Post updated at 03:56 PM ---------- Previous update was at 03:34 PM ----------

It doesn't like the while (!p &&.....)
it keeps saying that p: event not found
# 11  
Old 06-02-2010
That is a bash message. It typically means that quoting is not done right and bash interprets the "!" . Did you use the single quotes?
# 12  
Old 06-02-2010
I tried single and double quotes around the entire awk statement and they both returned the same error. Should I put the quotes elsewhere?

here is the code I tried:

Code:
awk '$3!=f{close(f);f=$3;n=0}{while(!p && getline x<f ) if(++n==$2){p=1};print x; p=0   }' testfile

Incidentally, what intuitively is meant by while (!p && getline x<f)

I understand the second part to mean "as long getline returns an actual line from file 'f' "
but I'm not understanding where the 'p' comes in. I see the downstream code for 'p' but it still isn't sinking in.



Last edited by Scott; 06-02-2010 at 06:23 PM.. Reason: Please use code tags
# 13  
Old 06-02-2010
Strange. Are you on Solaris? If so use nawk or /usr/xpg4/bin/awk instead of standard awk.

It means while the variable p is empty or zero AND reading a line was successful (in other words no EOF was encountered). If the variable p equals 0 then !p equals 1 (i.e. "TRUE")
This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 06-02-2010
it worked great and fast! But instead of !p, I used p==0. Do you see any nasty side effects of doing that workaround?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk with if, getline, and another if

Howdy Folks, It seems like it is always awk that confuses the heck out of me and I even have books and examples. I have this line: awk '{if (/clientIP/)(SRV = $NF); if ($2 ~ /BUNDLE-GIM/) getline; if ($2 ~ /r100595/) {print SRV,"BUNDLE-GIM",$2}}' post.txt to parse this text: <api... (4 Replies)
Discussion started by: port43
4 Replies

2. Shell Programming and Scripting

awk getline

Hi, I have an awk script with the following function in it . function cmd( c ) { while( ( c | getline foo) > 0 ){ return foo ; close( c ); } } c =... (4 Replies)
Discussion started by: MetaMan
4 Replies

3. Shell Programming and Scripting

awk getline problem

Hello, I want to print out the DNA sequence entries (tens of thousand!) that are longer than certain value (i=200) from a file (FASTA file) as: >S94D_ctg_8004 Average coverage: 402.95 ATAATGCCTGTGAATATGACATGTGTTCCTGTTTCTACATCAGACTACTATTCTTGCATA... (12 Replies)
Discussion started by: yifangt
12 Replies

4. Shell Programming and Scripting

awk getline t file

I want to import a textfile with getline into var t which has several lines. How do import all lines, since it only imports the last line: while < ((getline t "textfile") > 0) (7 Replies)
Discussion started by: sdf
7 Replies

5. Shell Programming and Scripting

awk getline question

Hi there, I have an ifconfig output and i want to write a script that determines whether there is a line "groupname ipmp" on a particular interface here is my example ifconfig -a output lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 inet 127.0.0.1... (2 Replies)
Discussion started by: rethink
2 Replies

6. Shell Programming and Scripting

Using getline in awk

I am using awk and want to use getline from a file like below getline x < file However file consists of two columns and I only want to store $2 Any way I can do this? ---------- Post updated at 06:54 AM ---------- Previous update was at 06:45 AM ---------- Done something like this.... (1 Reply)
Discussion started by: kristinu
1 Replies

7. Shell Programming and Scripting

awk getline

How do you make the getline function return to the original line? The example below should make it clear where I am currently going wrong. Thanks AWK SCRIPT: ------------- awk -F '-' '{ tmpLine = "EMPTY" print "CURRENT LINE :"$0 getline tmpLine print "NEXT LINE :"tmpLine }'... (1 Reply)
Discussion started by: garethsays
1 Replies

8. Shell Programming and Scripting

awk and system getline

Hello, Need some help here. I have this script (test.sh): #!/bin/sh var=$1 (( var = 2 * var )) echo $var Now I want to call this script from awk with one argument and then capture the result in a variable, something like: echo 40 | awk ' { x = $1; "test.sh " x | getline y; print y }... (1 Reply)
Discussion started by: fbg
1 Replies

9. Shell Programming and Scripting

awk getline help maybe?

hello collegues, I am attempting to use awk to search file1 (serverlist.csv) from each row with file2 (supported.txt). If the is no entry exists in serverlist then output to a file called notsupp.out if there is an entry output to supp.out I can do this with basic shell scripting however... (0 Replies)
Discussion started by: chlawren
0 Replies

10. Shell Programming and Scripting

awk:Problem with getline

$ echo |awk ' BEGIN {"date" | getline current_time;close("date");print "Report printed on " current_time}' Report printed on Thu May 11 14:57:29 METDST 2006 This example works fine but how can i print all the output when is longer... (3 Replies)
Discussion started by: Klashxx
3 Replies
Login or Register to Ask a Question