Visit Our UNIX and Linux User Community


How to print multiple required columns dynamically in a file using the header name?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to print multiple required columns dynamically in a file using the header name?
# 1  
Old 08-16-2019
How to print multiple required columns dynamically in a file using the header name?

Hi All,

i am trying to print required multiple columns dynamically from a fie.
But i am able to print only one column at a time.
i am new to shell script, please help me on this issue.

i am using below script
Code:
awk -v COLT=$1 '
        NR==1 {
                for (i=1; i<=NF; i++) {
                        if ($i==COLT) {
                                title=i;
                                print $i;
                        }
                }
        }
        NR>1 {
                if (i=title) {
                        print $i;
                }
        }
' file1

while executing i am using file name with column header name in the file. i am getting the required output for one column .
How to add second column details in order to get multiple columns based on header name.
Code:
../test2 Avail
Avail
22G
1.9G
1.9G
1.9G
1.9G
780M
379M
379M
0
379M

Desired output:

Code:
Used Avail
15G 22G
0 1.9G
0 1.9G
18M 1.9G
0 1.9G
235M 780M
4.0K 379M
40K 379M
4.3G 0
0 379M


Last edited by vgersh99; 08-16-2019 at 06:28 PM.. Reason: Code tags, please!
# 2  
Old 08-16-2019
Examples:

Cols as listed in file:
Code:
awk -v COLT="$*" '
BEGIN {
   c=split(COLT, cols);
   for (i=1; i<=c; i++) print_cols[cols[i]]=cols[i];
}

{
   line="";
   for (i=1; i<=NF; i++) {
      if (NR==1 && $i in print_cols) out_col[i]=i;
      if (i in out_col) line=line $i FS;
   }
   if (line) print line;
}

' file1

Cols as listed in arguments:
Code:
awk -v COLT="$*" '
BEGIN {
   c=split(COLT, cols);
}

NR==1 { for (j=1; j<=c; j++) {for (i=1; i<=NF; i++) if ($i==cols[j]) out_col[cc++]=i }}

{
   line="";
   for (i=0; i<cc; i++) {
      line=line $(out_col[i]) FS;
   }
   if (line) print line;
}

' file1


Last edited by rdrtx1; 08-16-2019 at 04:05 PM..
# 3  
Old 08-16-2019
Keep it simple:
Code:
df | awk -vCOLS="3 4" 'BEGIN {NC = split (COLS, C)} {for (i=1; i<=NC; i++) printf "%s\t", $C[i]; print _}'
Used       Available    
5926800    6123416    
 239864    9750808    
2808048    6403748    
7621396    1573996


Previous Thread | Next Thread
Test Your Knowledge in Computers #976
Difficulty: Medium
In April 2014, Linus Torvalds banned Kay Sievers from submitting patches to the Linux kernel for failing to deal with bugs that caused systemd to negatively interact with the kernel.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to apply functions to multiple columns dynamically

Hello, I have a requirement to apply hashing algorithm on flat file on one or more columns dynamically based on header sample input file ID|NAME|AGE|GENDER 10|ABC|30|M 20|DEF|20|F say if i want multiple columns based on the header example id,name or id,age or name,gender and hash and... (13 Replies)
Discussion started by: mkathi
13 Replies

2. UNIX for Beginners Questions & Answers

Count multiple columns and print original file

Hello, I have two tab files with headers File1: with 4 columns header1 header2 header3 header4 44 a bb 1 57 c ab 4 64 d d 5 File2: with 26 columns header1.. header5 header6 header7 ... header 22...header26 id1 44 a bb id2 57 ... (6 Replies)
Discussion started by: nans
6 Replies

3. Shell Programming and Scripting

Find columns in a file based on header and print to new file

Hello, I have to fish out some specific columns from a file based on the header value. I have the list of columns I need in a different file. I thought I could read in the list of headers I need, # file with header names of required columns in required order headers_file=$2 # read contents... (11 Replies)
Discussion started by: LMHmedchem
11 Replies

4. Shell Programming and Scripting

Awk: is it possible to print into multiple columns?

Hi guys, I have hundreds file like this, here I only show two of them: file 1 feco4_s_BB95.log ZE_1=-1717.5206260 feco4_t_BB95.log ZE_1=-1717.5169250 feco5_s_BB95.log ZE_1=-1830.9322060... (11 Replies)
Discussion started by: liuzhencc
11 Replies

5. Shell Programming and Scripting

awk dynamically append columns into file

Hi, I have a large data file, want to separate it into 100 part and export one specific field as a file; then I want to append each part's column into one file. How to realize that? 1 2 3 1 2 3 4 2 2 3 4 3 3 3 4 4 3 4 5 5 3 4 5 6 I want the last column of the data file, e.g divide it... (5 Replies)
Discussion started by: wanliushao
5 Replies

6. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

7. Linux

Find and print in multiple columns

Hi all, My input file is : 0 13400000 sil 13400000 14400000 a 14400000 14900000 dh 14900000 15300000 a 15300000 16500000 R 16500000 17000000 k 17000000 17300000 u 17300000 17600000 th 17600000 17900000 sil 17900000 18400000 th 18400000 18900000 a 18900000 19600000 g 19600000 19900000... (1 Reply)
Discussion started by: girlofgenuine
1 Replies

8. Shell Programming and Scripting

Required help to print diff columns for 2 patterns using awk

Hi All, I need help for below scenario : I have a principals.xml_24092012backup file : cat principals.xml_24092012backup </user> <user username="eramire" password="2D393C01720749256303D204826A374D9AE9ABABBF8A"> <roleMapping rolename="VIEW_EVERYTHING"/> </user> ... (2 Replies)
Discussion started by: kiran_j
2 Replies

9. Shell Programming and Scripting

Need awk help to print specific columns with as string in a header

awk experts, I have a big file of 4000 columns with header. Would like to print the columns with string value of "Commands" in header. File has "," separator. This file is on ESX host with Bash. Thanks, Arv (21 Replies)
Discussion started by: arv_cds
21 Replies

10. Shell Programming and Scripting

Averaging Data From Multiple Columns, Using Header if Possible

Hi, I have a file with multiple tab delimited columns and I would like to have the average of each column: Iteration Tree No Lh HMean 1000 1 -78.834717 -78.834717 1100 1 -77.991031 -78.624046 1200 1 -79.416055 -78.761861 1300 1 -79.280494 -78.968099 1400 1 -82.846275 -80.808696 ... (4 Replies)
Discussion started by: mikey11415
4 Replies

Featured Tech Videos