Visit Our UNIX and Linux User Community

print selected rows with awk

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print selected rows with awk
# 1  
Old 09-27-2007
print selected rows with awk

Hi everybody:

Could anybody tell me how I can print from a file a selected rows with awk. In my case I only want print in another file all the rows from NR=8 to NR=2459 and the increment each 8 times.

I tried to this:

awk '{for (i=8; i=2459; i+=8); NR==i}' file1 > file2

But doesn't work.
Thanks in advance. Smilie
# 2  
Old 09-27-2007
awk 'NR%8==0 && NR<=2459' file

# 3  
Old 09-27-2007
Not sure to understand the request, but may be:

awk 'NR==8,NR==2459{for(i=1;i<9;i++)print}' filename

# 4  
Old 09-27-2007
print selected rows with awk

Thanks for your reply, but with your help I print all rows from NR=8 to NR<=2459. And I would like that print it with an 8 increment. Smilie
# 5  
Old 09-27-2007
Thanks radoulov I tried your reply:
awk 'NR==8,NR==2459{for(i=1;i<9;i++)print}' filename

but the result is not as I like. Actually I would like that it prints only the rows with NR=8, 15, 22, 29 .... until 2459. Smilie
# 6  
Old 09-27-2007
awk 'NR==8,NR==2459{if(NR%7==1)print}' filename

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 7  
Old 09-27-2007
Thanks a lot, now it works correctly. SmilieSmilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #247
Difficulty: Easy
By 1973, the File Transfer Protocol (FTP) specification had been defined (RFC 354) and implemented, enabling file transfers over the ARPANET.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

2. UNIX for Beginners Questions & Answers

Reading and copying a selected rows

Dear All, I have a data file input.res like below. (Only six column shown here for example.) Sequence of first column starting from 1 to 148. Input file 1 Q0 9_August_2014_Entertainment2 0 20.14967806339729 BM25b1.0 1 Q0 13_October_2012_Page323 1 20.134224346765738 BM25b1.0 1 Q0... (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

3. Shell Programming and Scripting

Print rows with value zero or less than zero using awk

I tried this. It working fine for small tables. But it is giving values greater than zero in a big file with 8556 columns. Does any one know why ? awk 'FNR == 1{print;next}{for(i=8556;i<=NF;i++) if($i <= 0){print;next}}' input (5 Replies)
Discussion started by: quincyjones
5 Replies

4. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. UNIX for Dummies Questions & Answers

Awk: Print out overlapping chunks of file - rows 0-20,10-30,20-40 etc.

First time poster, but the forum has saved my bacon more times than... Lots. Anyway, I have a text file, and wanted to use Awk (or any other sensible program) to print out overlapping sections, or arbitrary length. To describe by example, for file 1 2 3 4 5 etc... I want the out put... (3 Replies)
Discussion started by: matfald
3 Replies

6. Shell Programming and Scripting

To extract selected rows

Hi, I have two files Input1.txt and Input2.txt and i need to have a output with selected lines starting with the values present in Input2.txt. For example: Input1.txt:... (5 Replies)
Discussion started by: bhas
5 Replies

7. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

8. Shell Programming and Scripting

shell script(Preferably awk or sed) to print selected number of columns from each row

Hi Experts, The question may look very silly by seeing the title, but please have a look at it clearly. I have a text file where the first 5 columns in each row were supposed to be attributes of a sample(like sample name, number, status etc) and the next 25 columns are parameters on which... (3 Replies)
Discussion started by: ks_reddy
3 Replies

9. Shell Programming and Scripting

return number of rows selected

Hi all, i am doing a perl script to read from a db. I am able to retrieve the rows, but i am unable to return the number of rows selected. i tried $selectedrows = $sth->numrows; i got the error msg: Can't locate object method "numrows" via package "DBI::st" i changed it to $selectedrows =... (7 Replies)
Discussion started by: new2ss
7 Replies

10. Shell Programming and Scripting

Cutting rows after a selected point

I have a log file from which I want to cut out the ten lines assoictiated to my search requirment (cust_ref #). The cust_ref number will be on te first line and the update information will be on the following ten lines (which dosen't linking data to grep on). Using a script I would like to... (4 Replies)
Discussion started by: nhatch
4 Replies

Featured Tech Videos