If condition satisfies print the column header


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If condition satisfies print the column header
# 8  
Old 05-06-2013
Here is my input file (first few line only., it has more than 500 lines)

Code:
 Average        10      15      20      25      30      35      40      45
 13.622 25 16 14 8 7 6 4 4
 12.695 15 9 7 6 4 4 4 3
 10.7837 10 6 4 4 2 2 1 1
 8.05027 6 3 3 1 1 1 1 1
 10.9237 9 6 4 4 2 2 2 1

I need my output should be like this:

Code:
                     P { margin-bottom: 0.08in; }    Average        10      15      20      25      30      35      40      45      new_col1      new_col2
  13.622 25 16 14 8 7 6 4 4   35  -
  12.695 15 9 7 6 4 4 4 3    25  45  
  10.7837 10 6 4 4 2 2 1 1   15  30
  8.05027 6 3 3 1 1 1 1 1   10  15
  10.9237 9 6 4 4 2 2 2 1   25  30

new_col1 should satisfy the first if condition and new_col2 should satisfy the second if condition (as i mentioned before).

Hope I have explained you the problem correctly.

Expecting your reply and thanks in advance.

-F

---------- Post updated at 09:16 AM ---------- Previous update was at 09:15 AM ----------

Here is my input file (first few line only., it has more than 500 lines)

Code:
Average        10      15      20      25      30      35      40      45
13.622 25 16 14 8 7 6 4 4
12.695 15 9 7 6 4 4 4 3
10.7837 10 6 4 4 2 2 1 1
 8.05027 6 3 3 1 1 1 1 1
10.9237 9 6 4 4 2 2 2 1

I need my output should be like this:

Code:
                     P { margin-bottom: 0.08in; }    Average        10      15      20      25      30      35      40      45      new_col1      new_col2
  13.622 25 16 14 8 7 6 4 4   35  -
  12.695 15 9 7 6 4 4 4 3    25  45  
  10.7837 10 6 4 4 2 2 1 1   15  30
  8.05027 6 3 3 1 1 1 1 1   10  15
  10.9237 9 6 4 4 2 2 2 1   25  30

new_col1 should satisfy the first if condition and new_col2 should satisfy the second if condition (as i mentioned before).

Hope I have explained you the problem correctly.

Expecting your reply and thanks in advance.

-F

---------- Post updated at 09:24 AM ---------- Previous update was at 09:16 AM ----------

Thanks hanson44 for your reply. But my input file and the output file i need is bit different. Could you please see my reply in the threat.

Warm regards
-F
# 9  
Old 05-06-2013
I think some of your expected output is incorrect. Anyway, try the following.

Code:
$ cat test.awk
NR==1 {split ($0, headers); print; next}
{
x=NF; $11=$12="-"
for (i=2; i<x; i++) {
  if ($i > 5 && $(i+1) < 5) {$11=headers[i]}
  if ($i > 3 && $(i+1) < 3) {$12=headers[i]}
  }
print
}

Code:
$ awk -f test.awk inputfile
Average        10      15      20      25      30      35      40      45
13.622 25 16 14 8 7 6 4 4  35 -
12.695 15 9 7 6 4 4 4 3  25 -
10.7837 10 6 4 4 2 2 1 1  15 25
8.05027 6 3 3 1 1 1 1 1  10 -
10.9237 9 6 4 4 2 2 2 1  15 25

This User Gave Thanks to hanson44 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

2. Shell Programming and Scripting

Print the column content based on the header

i have a input of csv file as below but the sequence of column get changed. I,e it is not necessary that name comes first then age and rest all, it may vary. name,age,marks,roll,section kevin,25,80,456,A Satch,23,56,789,B Meena,24,78,H245,C So i want to print that column entires which... (12 Replies)
Discussion started by: millan
12 Replies

3. Shell Programming and Scripting

Print column header

Hi, Below is the data in a tab limited file Here ACCENT is just a heading and the line starting with Pcode has the column headers, and there is a 2 lines space between the file header and column header. I want to print $2, that is Dealer Name I used the below code nawk... (1 Reply)
Discussion started by: kedar_laveti
1 Replies

4. Shell Programming and Scripting

Add column header and row header

Hi, I have an input like this 1 2 3 4 2 3 4 5 4 5 6 7 I would like to count the no. of columns and print a header with a prefix "Col". I would also like to count the no. of rows and print as first column with each line number with a prefix "Row" So, my output would be ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

write a matrix element, if the "if" condition satisfies

Hi All, I would like to check a condition in a nxm matrix and if the condition satisfies then, I need to write the matrix element in the output file. For example, I have a matrix like this: 1 2 3 4 5 2 3 4 5 1 3 4 5 1 ... (8 Replies)
Discussion started by: Fredrick
8 Replies

6. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

7. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

8. Shell Programming and Scripting

Filter the column and print the result based on condition

Hi all This is my output of the some SQL Query TABLESPACE_NAME FILE_NAME TOTALSPACE FREESPACE USEDSPACE Free ------------------------- ------------------------------------------------------- ---------- --------- ---------... (2 Replies)
Discussion started by: jhon
2 Replies

9. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

10. Shell Programming and Scripting

need to read a file and keep waiting till it satisfies some condition

In my script i am writing to a counter file the no of processes i had started, that is each time i start a process, i will increment the content of counter file and also when the process ends i will decrement the content of the file. after this i do some other activities, by now i want to... (1 Reply)
Discussion started by: senthilk615
1 Replies
Login or Register to Ask a Question