How to print specific range using awk?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to print specific range using awk?
# 1  
Old 07-28-2015
How to print specific range using awk?

I want to print specific range of rows and then its columns using awk command.

lets say if a file contain 15 line then i need to print line 5 to 9.

Code:
awk '{print;for( NR>=5&&NR<=9); do;  print  "<tr>\n<td>"$1"</td><td>"$2"</td><td>"$3"</td>\n</tr" done}' xyz.csv

not sure what's wrong with above code or its completely wrong Smilie
# 2  
Old 07-28-2015
Code:
awk 'FNR>=5 && FNR<=9' myFile

# 3  
Old 07-28-2015
but i need to print column too at the same time....
your code will print all the column not specific columns...
# 4  
Old 07-28-2015
You only mentioned that you want specific lines but you did not tell us how they are identified. How should vgersh99 should have known that? So your turn again or just add the criteria with another &&.
# 5  
Old 07-28-2015
Hello amyt1234,

Could you please try following and let me know if that helps you.
Code:
 awk '(FNR>=5 && FNR<=9 && $0 ~ /15/)' Input_file

NOTE: If there are more terms and conditions then kindly do let us know with complete details.


Thanks,
R. Singh

Last edited by RavinderSingh13; 07-28-2015 at 12:20 PM..
# 6  
Old 07-28-2015
Quote:
Originally Posted by amyt1234
but i need to print column too at the same time....
your code will print all the column not specific columns...
Feel free to modify the suggestion to fit your exact needs......
# 7  
Old 07-28-2015
Sorry for not explaining correctly.


Suppose input is :

5 rows in a file with 4 column

Code:
2015/07/28 00:00:00                       177                       445370                         21232                                                                                                                                
2015/07/27 00:00:00                       116                       841327                         26669                                                                                                                                
2015/07/26 00:00:00                       165                                                       6497                                                                                                                                
2015/07/25 00:00:00                       162                       463795                          5607                                                                                                                                
2015/07/24 00:00:00                       170                       882899                         21671


then output should be:

row 2 to row 4
and column 1,2 and 3

Code:
2015/07/27 00:00:00         116              26669                                                                                                            
2015/07/26 00:00:00         165               6497                                                                                                            
2015/07/25 00:00:00         162               5607

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules. Without CODE tags, sequences of whitespace characters in your sample input and output are converted to a single space making it hard (or impossible) for people trying to help you understand or even see that some rows are missing data in some columns and to understand that you are treating the date and time fields as a single field instead of two fields.

Last edited by Don Cragun; 07-28-2015 at 01:29 PM.. Reason: Add CODE tags.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk to remove specific column to one range

I need to remove specific column to one range source file 3 1 000123456 2 2 000123569 3 3 000123564 12 000123156 15 000125648 128 000125648 Output required 3 000123456 2 000123569 3 000123564 12 000123156 15 000125648 128 000125648 (6 Replies)
Discussion started by: ranjancom2000
6 Replies

2. Shell Programming and Scripting

awk to print text in field if match and range is met

In the awk below I am trying to match the value in $4 of file1 with the split value from $4 in file2. I store the value of $4 in file1 in A and the split value (using the _ for the split) in array. I then strore the value in $2 as min, the value in $3 as max, and the value in $1 as chr. If A is... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. 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

4. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

5. Shell Programming and Scripting

How to print Specific keyword, by using awk?

How to print Specific keyword, by using awk.? prime:root:I want output. 78 1457 10000 10000 5985 307 10000 10000 10000 10000 3760 692 6656 157 696 (4 Replies)
Discussion started by: ooilinlove
4 Replies

6. Shell Programming and Scripting

awk to print range of fields

Hi file.in and file.out are in csv format. the code I have now is, cat file.in | awk -F"," '!($1$2$3$4$5$6$7$8 in a){a;print $0}' > file.out Here, I am printing entire line using $0. however, I want to print $1 to $150 and it should be in csv format. Cut -d is not good in performace.... (3 Replies)
Discussion started by: krishnix
3 Replies

7. Shell Programming and Scripting

AWK Script - Print a column - within a Row Range

Hi, Please read the whole thread. I have been working on this script below. It works fine, feel free to copy and test with the INPUT File below as well. example: PACKET DATA PROTOCOL CONTEXT DATA APNID PDPADD EQOSID VPAA PDPCH PDPTY PDPID 10 ... (6 Replies)
Discussion started by: panapty
6 Replies

8. Shell Programming and Scripting

How to print first matching range in awk?

following is input - <Schema> <schema_name>admin</schema_name> <Version>1.1</Version> <schema_name>admin</schema_name> <Version>1.2</Version> </Schema> ... (12 Replies)
Discussion started by: thearpit
12 Replies

9. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

10. Shell Programming and Scripting

How to print specific lines with awk

Hi! How can I print out a specific range of rows, like "cat file | awk NR==5,NR==9", but in the END-statement? I have a small awk-script that finds specific rows in a file and saves the line number in an array, like this: awk ' BEGIN { count=0} /ZZZZ/ { list=NR ... (10 Replies)
Discussion started by: Bugenhagen
10 Replies
Login or Register to Ask a Question