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?
# 8  
Old 07-28-2015
Adapting/correcting your script from post #1:
Code:
awk '
BEGIN            {print "<tr>\n"}
NR>=2 && NR<=4   {print  "<td>"$1"</td><td>"$2"</td><td>"$3"</td>\n"}
END              {print "</tr>" }
' xyz.csv

# 9  
Old 07-28-2015
Moderator's Comments:
Mod Comment Not sure how this thread got here, but it is ill-placed. Going to move it to "Unix for Dummies".


bakunin
# 10  
Old 07-28-2015
Is this a homework assignment? Homework must be posted in the Homework & Coursework Questions Forum and must contain a completely filled output template as described in the Rules for Homework & Coursework Questions Forum. If this is not homework, please explain why you need to gather seemingly random lines from a file! Until we get a clear answer to the above questions, you won't get any more suggestions for a solution to your problem.

I will, however, make a few comments on what we have seen in this thread so far...

Now that I have added CODE tags to post #7 in this thread, we can see:
  1. what you are calling field 1 is seen by awk as fields 1 and 2,
  2. what you are calling field 3 is an empty string in line 3,
  3. you said you want fields 1, 2, and 3; but what you really want is fields 1, 2, and 4 (which in awk will be fields 1 and 2, 3, and 4 or 5 (depending on whether there is an empty field), and
  4. the sample data you said you want as your output doesn't come close to what you said you want as your output.

In your sample input file, the "3rd field" is empty on one line. Is that the only field that can be empty? Besides your "1st field" can any other fields contain <space> or <tab> characters (other than as field separators)?

And, even with the above realizations, I still don't know what you want. Are you trying to create HTML output or are you trying to create aligned text output?
 
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