2

i have n number of file, in these files a specific column named "thrudate" is given at different column number in every files.

i Just want to extract the value of this column from all files in one go. So i tried with using awk. Here i'm considering only one file, and extracting the values of thrudate

awk -F, -v header=1,head="" '{for(j=1;j<=2;j++){if($header==1){for(i=1;i<=$NF;i++){if($i=="thrudate"){$head=$i;$header=0;break}}} elif($header==0){print $0}}}' file | head -10 

How i have approached:

  • used find command to find all the similar files and then executing the second step for every file
  • loop all fields in first row, checking the column name with header values as 1 (initialized it to 1 to check first row only), once it matched with 'thrudate', i set header as 0, then break from this loop.
  • once i get the column number then print it for every row.

1 Answer 1

4

You can use the following awk script:

print_col.awk:

# Find the column number in the first line of a file FNR==1{ for(n=1;n<=NF;n++) { if($n == header) { next } } } # Print that column on all other lines { print $n } 

Then use find to execute this script on every file:

find ... -exec awk -v header="foo" -f print_col.awk {} + 

In comments you've asked for a version that could print multiple columns based on their header names. You may use the following script for that:

print_cols.awk:

BEGIN { # Parse headers into an assoc array h split(header, a, ",") for(i in a) { h[a[i]]=1 } } # Find the column numbers in the first line of a file FNR==1{ split("", cols) # This will re-init cols for(i=1;i<=NF;i++) { if($i in h) { cols[i]=1 } } next } # Print those columns on all other lines { res = "" for(i=1;i<=NF;i++) { if(i in cols) { s = res ? OFS : "" res = res "" s "" $i } } if (res) { print res } } 

Call it like this:

find ... -exec awk -v header="foo,bar,test" -f print_cols.awk {} + 
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, this is damn correct, just wanted to know if we can do this to fetch 2 columns?
I've added that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.