Print the column content based on the header


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print the column content based on the header
# 1  
Old 05-27-2013
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.
Code:
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 contains 'name' at the top.
i,e i want the output should be

Kevin
Satch
Meena

Last edited by vbe; 05-27-2013 at 10:22 AM.. Reason: please use code tags nextime
# 2  
Old 05-27-2013
Code:
awk -F"," ' /name/ { for (i=1;i<=NF;i++) if ($i=="name") idx=i} $0 !~ /name/ { print $idx} ' file

This User Gave Thanks to Fundix For This Post:
# 3  
Old 05-27-2013
Code

Not tested but this should work
Code:
 
l=wc -l
l=`echo $l -1 |bc`
for name in `tail -$l $file|awk '{print $1}'|`
do
 tmp=`echo name|grep -Po '^[a-z]+$'`
 if [ "$tmp" = "$name" ]
  echo $name
 fi
done

way is simple. i tried reading file using tail (except the first line) and passed to awk. read the first column and using grep i tried matching it with a name formate which will contain charecters in range "a-z". it if matches -o opetion will return the matched part.
if it's name then it should return the complete name and tmp and name variable should match.
Hope i'm clear
# 4  
Old 05-28-2013
Hey Fundix,Thank you ..it worked fine.

But i want to keep name in a variable

Code:
var="name"
awk -F"," ' /$var/ { for (i=1;i<=NF;i++) if ($i=="$var") idx=i} $0 !~ /$var/ { print $idx} ' filename

But it is not giving the output..Could you pls tell me know how i will use a variable here.

Last edited by Franklin52; 05-28-2013 at 03:50 AM.. Reason: Please use code tags
# 5  
Old 05-28-2013
Try:
Code:
awk -F, '$0 ~ ",?" var ",?" {for(i=1;i<=NF;i++) if($i == var) idx=i; next}
{print $idx}' var=name filename

# 6  
Old 05-28-2013
Quote:
Originally Posted by millan
Hey Fundix,Thank you ..it worked fine.

But i want to keep name in a variable

var="name"
awk -F"," ' /$var/ { for (i=1;i<=NF;i++) if ($i=="$var") idx=i} $0 !~ /$var/ { print $idx} ' filename

But it is not giving the output..Could you pls tell me know how i will use a variable here.
You can use -v to pass vars to awk :

Code:
awk -v var="name" -F"," ' /var/ { for (i=1;i<=NF;i++) if ($i==var) idx=i} $0 !~ /var/ { print $idx} ' file

# 7  
Old 05-28-2013
Quote:
Originally Posted by Fundix
You can use -v to pass vars to awk :

Code:
awk -v var="name" -F"," ' /var/ { for (i=1;i<=NF;i++) if ($i==var) idx=i} $0 !~ /var/ { print $idx} ' file

That will not work. It will try to match (or not) the string "var".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum of a column as new column based on header in a script

Hello, I am trying to store sum of a column as a new column inside a file but have to find the column names dynamically I/p c1,c2,c3,c4,c5 10,20,30,40,50 20,30,40,50,60 If i want to find sum only column c1, c3 and output it as c6,c7 O/p c1,c2,c3,c4,c5,c6,c7 10,20,30,40,50,30,70... (6 Replies)
Discussion started by: mkathi
6 Replies

2. Shell Programming and Scripting

Break a line content and print as column

Hi, I have urls in my input file like this (1 Reply)
Discussion started by: tmonk1
1 Replies

3. Shell Programming and Scripting

Help to print the line that share exactly same with column one content

Input file : AAAG TC AACCCT AACCCT AACCCT AACCCT TCTG TCTG TCTG AC AC TCTG TCTG AC AC AC AC AC AGTG AC AGTG TCC Desired output file : AACCCT AACCCT AACCCT AACCCT AC AC I would like to print out the line that share exactly same as the first column data content. Column one data... (4 Replies)
Discussion started by: perl_beginner
4 Replies

4. Shell Programming and Scripting

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 ... (8 Replies)
Discussion started by: Fredrick
8 Replies

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

6. Shell Programming and Scripting

Help with analysis data based on particular column content

Input file: Total_counts 1306726155 100% Number_of_count_true 855020282 Number_of_count_true_1 160014283 Number_of_count_true_2 44002825 Number_of_count_true_3 18098424 Number_of_count_true_4 24693745 Number_of_count_false 115421870 Number_of_count_true 51048447 Total_number_of_false ... (2 Replies)
Discussion started by: perl_beginner
2 Replies

7. Shell Programming and Scripting

multiplication of two files based on the content of the first column

Hi, This is something that probably it is more difficult to explain than to do. I have two files e.g. FILE1 A15 8.3102E+00 3.2000E-04 A15 8.5688E+00 4.3000E-05 B13 5.1100E-01 1.9960E+00 B16 5.1100E-01 2.3000E-03 B16 8.6770E-01 1.0000E-07 B16 9.8693E-01 3.4000E-05... (4 Replies)
Discussion started by: f_o_555
4 Replies

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

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

10. Shell Programming and Scripting

Help with rename header content based on reference file problem

I got long list of reference file >data_tmp_number_22 >data_tmp_number_12 >data_tmp_number_20 . . Input file: >sample_data_1 Math, 5, USA, tmp SDFEWRWERWERWRWER FSFDSFSDFSDGSDGSD >sample_data_2 Math, 15, UK, tmp FDSFSDFF >sample_data_3 Math, 50, USA, tmp ARQERREQR . . Desired... (7 Replies)
Discussion started by: perl_beginner
7 Replies
Login or Register to Ask a Question