exit after extracting range if lines - awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exit after extracting range if lines - awk
# 1  
Old 09-18-2012
exit after extracting range if lines - awk

Hello,

I was wondering how is it possible if I use this command:

Code:
awk 'NR >= 998 && NR <= 1000' file.txt

To exit after parsing the 1000th line ( last line targeted) ???

I observed that when executing this command for a large file, if the range of lines is at the beginning of the file it is quickly extracted, but awk will parse the entire file, and only after reaching the end of file it exits.

Same question goes for sed :
Code:
sed -n 'xx,yy p' file

# 2  
Old 09-18-2012
Code:
awk 'NR>=998;NR==1000{exit}' file
sed -n '998,1000{;p;1000q;}' file

These 2 Users Gave Thanks to elixir_sinari For This Post:
# 3  
Old 09-18-2012
Quote:
Originally Posted by elixir_sinari
Code:
awk 'NR>=998;NR==1000{exit}' file
sed -n '998,1000{;p;1000q;}' file

Hello, yes this is what i was looking for.

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed/awk to delete a regex between range of lines

Hi Guys I am looking for a solution to one problem to remove parentheses in a range of lines. Input file module bist_logic_inst(a, ab , dhd, dhdh , djdj, hdh, djjd, jdj, dhd, dhp, dk ); input a; input ab; input dhd; input djdj; input dhd; output hdh; output djjd; output jdj;... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

awk to print out lines that do not fall between range in file

In the awk below I am trying to print out those lines in file2 that are no between $2 and $3 in file1. Both files are tab-delimeted and I think it's close but currently it is printeing out the matches. The --- are not part of the files they are just to show what lines match or fall into the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

Extracting range of characters if pattern matches

Im trying compare values between files and if they match I want to extract some characters in between those values for many files. They are in two directories and have the name filename but one ends in .enr. They look like this. cat bat.1.enr name,start,end bat.1,231, 234 and another... (5 Replies)
Discussion started by: verse123
5 Replies

4. Shell Programming and Scripting

Extracting lines from a file with sed and awk

My source file is structured with two words on each line word1 word2 word1 word2 I am using sed and awk to grab groups of specific lines line=`awk 'NR>=4 && NR<=7' file1`; echo $line line=` sed -n '1,5'p file1`; echo $line The resulting output is word1 word2 word1 word2 word1... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

5. UNIX for Dummies Questions & Answers

Extracting rows from a text file if the value of a column falls between a certain range

Hi, I have a file that looks like the following: 10 100080417 rs7915867 ILMN_1343295 12 6243093 7747537 10 100190264 rs2296431 ILMN_1343295 12 6643093 6647537 10 100719451 SNP94374 ILMN_1343295 12 6688093 7599537 ... (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

AWK script - extracting min and max values from selected lines

Hi guys! I'm new to scripting and I need to write a script in awk. Here is example of file on which I'm working ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00 ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00 ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies

7. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

8. Shell Programming and Scripting

Extracting lines by using awk

Hello, I have a file including some lines as follows person (1): a d t person (2): f h j person (3): z x v . . . (8 Replies)
Discussion started by: rpf
8 Replies

9. Shell Programming and Scripting

awk : extracting unique lines based on columns

Hi, snp.txt CHR_A SNP_A BP_A_st BP_A_End CHR_B BP_B SNP_B R2 p-SNP_A p-SNP_B 5 rs1988728 74904317 74904318 5 74960646 rs1427924 0.377333 0.000740085 0.013930081 5 ... (12 Replies)
Discussion started by: genehunter
12 Replies

10. UNIX for Dummies Questions & Answers

Extracting lines and saving - awk

Hi All, I am trying to extract lines bsed on pattern matching../mp straight-flow/ Extracted output should be saved in meta_string , but the code is not working in that manner,saving repeated lines. can anyone please suggest where am i going wrong. /mp straight-flow/ {... (6 Replies)
Discussion started by: madhaviece
6 Replies
Login or Register to Ask a Question