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
# 8  
Old 05-28-2013
Sorry, not well awake this morning, answering without testing Smilie

The following works fine now :

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

# 9  
Old 05-28-2013
I'm a little confused as to why most of the scripts that have been proposed in this thread look for a column containing the heading on any line that contains the heading (instead of just looking at the first line). I'm also a little surprised that none of them report an error if the requested column header is not found. This isn't a one-liner, but I hope you'll find it useful:
Code:
#!/bin/ksh
IAm=${0##*/}
if [ $# -ne 2 ]
then    printf "Usage: %s input_file column_heading\n" "$IAm" >&2
        exit 1
fi
ef=_"$IAm$$"
if [ ! -f "$1" ] || [ ! -r "$1" ]
then
    printf "%s: input_file \"%s\" must be a readable regular file.\n" \
                "$IAm" "$1" >&2
        exit 2
fi
if ! awk -F, -v ch="$2" -v IAm="$IAm" -v ef="$ef" '
FNR == 1 {
        for(i = 1; i <= NF; i++)
                if($i == ch) break
        if(i > NF) {
                printf("%s: column_heading %s not found\n", IAm, ch) > ef
                exit 3
        }
        next
}
{       print $i}' "$1"
then    cat "$ef" >&2
        rm -f "$ef"
        exit 3
fi

As always, if you're using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.

I use the Korn shell, but any shell that recognizes the variable expansions defined by the POSIX standards and the Single UNIX Specifications (e.g., bash) will also work.

To use it, save the above script in a file (e.g., getf), make it executable:
Code:
chmod +x getf

and invoke it with two arguments (a pathname of your input file and the column heading for the field you want to get out of the file). For example, if the input provided in the 1st message in this thread is stored in a file named input.csv, the command:
Code:
getf input.csv marks

produces the output:
Code:
80
56
78


Last edited by Don Cragun; 05-28-2013 at 01:15 PM.. Reason: Missing quotes and misplaced then.
# 10  
Old 05-28-2013
I tried the above two ways...but i m getting below error.

awk: syntax error near line 1
awk: bailing out near line 1
# 11  
Old 05-28-2013
Quote:
Originally Posted by millan
I tried the above two ways...but i m getting below error.

awk: syntax error near line 1
awk: bailing out near line 1
What OS are you using?

Did you notice the comment in my last posting: As always, if you're using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
# 12  
Old 05-28-2013
A simple Perl workaround using a 2 pass reading the file.
The line "name,xxx,xxx" may be anywhere in the file (top, middle, tail)
The 1st pass detects the index of the requested field, the 2nd one displays the corresponding field on the other lines :
Code:
#!/usr/bin/perl -w
use strict;

my $cur_dir = $ENV{PWD};
my $filename = "$cur_dir/$ARGV[0]";

my ($record,@fields,$index);

open(FILE,"<$filename") or die"open: $!";
while( defined( $record = <FILE> ) ) {
  chomp $record;
  if ( $record =~ $ARGV[1] ) {
    @fields=split(/,/,$record);
    ($index) = grep { $fields[$_] eq $ARGV[1] } 0..$#fields;
    last
  }
}
close(FILE);

open(FILE,"<$filename") or die"open: $!";
while( defined( $record = <FILE> ) ) {
  chomp $record;
  if ( $record !~ $ARGV[1] ) {
    @fields=split(/,/,$record);
    print "$fields[$index]\n";
  }
}
close(FILE);

examples :
Code:
%./file018.pl file018 name
kevin
Satch
Meena

Code:
%./file018.pl file018 section
A
B
C

# 13  
Old 05-28-2013
Quote:
Originally Posted by millan
I tried the above two ways...but i m getting below error.

awk: syntax error near line 1
awk: bailing out near line 1
Now that I'm awake this morning, I do see that there was a copy and paste error that misplaced a "then" in my script. I fixed that in place in message #9 in this thread a few minutes ago. But, given the error message you reported, I'm still assuming that you're using a Solaris system and need to change the line that starts with "awk" to instead start with "/usr/xpg4/bin/awk".
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