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
# 1  
Old 05-06-2013
If condition satisfies print the column header

Dear All,

Good Day. I would like to solve the following issue and got some strange results, if anyone could help me in this regard, you are most welcome.

Here is the problem:

I have a file like

Header Value1 Value2 Value3 Value4 Value5 Value6 ... Value9
12.144 6 5 5 4 3 2 ...
10.2567 10 8 6 4 4 4 ...
22.2 2 2 1 1 1 1 ...
.................................
.................................

The above-mentioned file have more than 500 rows and 10 columns.

I would like to print a new additional columns (say for example column11 and column12), where the values from column2 to column9 can satisfy the following conditions:

if ($i > 5 && $(i+1) < 5) ------ print the column header (for example, in the first row case, Value 4) should be printed in column11 and at the same time,

if ($i > 3 && $(i+1) < 3) ------ print the column header (for example, in the first row case, Value 6) should be printed in column12 (in the output file with the same format).

I tried the following code, but i couldn't get the same number of rows. here is my code:
Code:
awk '{for(i=2; i<=NF; i++) {if($i > 5 && $(i+1) < 5) print $1=(i*5); else print ""}}' inputfile > outputfile

Expecting your reply and thanks in advance.

Warm regards
Fredrick.
# 2  
Old 05-06-2013
Awk variables dont need $ with them.
# 3  
Old 05-06-2013
Sorry, Could you please explain me.
# 4  
Old 05-06-2013
Quote:
Originally Posted by Fredrick
Sorry, Could you please explain me.
Sorry I mis-read the question Smilie

---------- Post updated at 12:23 PM ---------- Previous update was at 12:18 PM ----------

If you want to compare or check column 2 and column 9 then use $2 and $9 to get the values from those columns. I am not sure whats the logic behind getting the value 4 and 6 as you mentioned in your post..

Your code is comparing successive columns and replacing first column with column * 5.. else you printed blank line.
# 5  
Old 05-06-2013
Code:
awk '{for(i=2; i<=NF; i++) {if($i > 5 && $(i+1) < 5) print $1=(i*5); else print ""}}' inputfile > outputfile

Yes the above-code print i*5; instead of that I would like to print only the header values in the new columns, if the conditions satisfied.
I need your help in this regard.

Expecting your reply and thanks in advance.

-F
# 6  
Old 05-06-2013
Could you please post small piece of actual input file and the output you want? I am unable to understand your requirements with below
Quote:
I would like to print a new additional columns (say for example column11 and column12), where the values from column2 to column9 can satisfy the following conditions:

if ($i > 5 && $(i+1) < 5) ------ print the column header (for example, in the first row case, Value 4) should be printed in column11 and at the same time,

if ($i > 3 && $(i+1) < 3) ------ print the column header (for example, in the first row case, Value 6) should be printed in column12 (in the output file with the same format).

# 7  
Old 05-06-2013
If I understand what you are trying to do, the following does it:
Code:
$ cat inputfile
12.144 6 5 5 4 3 2 9 9 9
10.2567 10 8 6 4 4 4 9 9 9
22.2 4 2 1 1 1 1 9 9 9

Code:
$ cat test.awk
{
x=NF; $11=$12=0
for (i=2; i<x; i++) {
  if ($i > 5 && $(i+1) < 5) {$11=i}
  if ($i > 3 && $(i+1) < 3) {$12=i}
  }
print
}

Code:
$ awk -f test.awk inputfile
12.144 6 5 5 4 3 2 9 9 9 0 0
10.2567 10 8 6 4 4 4 9 9 9 4 0
22.2 4 2 1 1 1 1 9 9 9 0 2

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