Capturing column headers in an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing column headers in an array
# 1  
Old 07-22-2016
Capturing column headers in an array

Hello,

I am processing a tab delimited text file and need to grab all of the column headers in an array.

The input looks like,
Code:
num     Name            PCA_A1     PCA_A2       PCA_A3
0       compound_00     -3.5054     -1.1207     -2.4372
1       compound_01     -2.2641     0.4287      -1.6120
2       compound_02     -2.7516     -0.1016     -2.1137
3       compound_03     -1.3053     1.8495      -1.0224
4       compound_04     -1.1845     -0.3377     -2.9453
5       compound_05     -2.9492     -0.8277     -2.7023
6       compound_06     -0.6327     1.8127      -1.1693
7       compound_07     -0.2988     1.3539      -1.6114
8       compound_08     2.6872     -1.3726      -5.9732
9       compound_09     -1.4546     -0.8284     -3.5016

I captured the first line of the input with,
header_row=$(sed -n 1p "$input_file")

then I parsed "header_row" on tab,
IFS='\t' read -a column_headers <<< "$header_row"

If I print the size of the array, I get 1 and not 5.
Code:
# derive number of columns
number_of_columns=${#column_headers[@]}

echo "number_of_columns"
echo $number_of_columns

If I print the first array element, I get everything,

Code:
echo ${column_headers[0]}
num Name PCA_A1 PCA_A2 PCA_A3

I have tried parsing on space instead of tab in case the output of sed did not preserve the tabs,
IFS=' ' read -a column_headers <<< "$header_row"

but that gives me the same results. I am still ending up with the header row as a single string, not parsed into the elements of an array.

Any suggestions as to what I am doing wrong?

LMHmedchem
# 2  
Old 07-22-2016
IFS='\t' doesn't do what you think it does.

IFS=' ' won't work if the delimiters are tabs.

Why not leave IFS alone so it can accept any whitespace?

read does not need awk/sed/cut/etc's help to read from a file, either:

Code:
read -a column_headers < filename

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-22-2016
Quote:
Originally Posted by Corona688
IFS='\t' doesn't do what you think it does.

IFS=' ' won't work if the delimiters are tabs.

Why not leave IFS alone so it can accept any whitespace?

read does not need awk/sed/cut/etc's help to read from a file, either:

Code:
read -a column_headers < filename

Thanks, that sorted it out.

The reason I added the sed line was I only wanted the first row. Is there a way to use read to just get the first row?

LMHmedchem
# 4  
Old 07-22-2016
It's already doing that. It doesn't need sed's help.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 07-22-2016
Quote:
Originally Posted by Corona688
It's already doing that. It doesn't need sed's help.
Alright, that simplifies my script some. What is the reason for read using "<<<" to read from a bash variable but only "<" to read from a file?

LMHmedchem
# 6  
Old 07-22-2016
The > and < file redirections have been around since the first UNIX shells were created. The Bourne shell added << and <<- here-documents. Recent bash and ksh and a few other shells have added <<< short-cut here-document redirections.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Row bind multiple csv files having different column headers

All, I guess by this time someone asked this kind of question, but sorry I am unable to find after a deep search. Here is my request I have many files out of which 2 sample files provided below. File-1 (with A,B as column headers) A,B 1,2 File-2 (with C, D as column headers) C,D 4,5 I... (7 Replies)
Discussion started by: ks_reddy
7 Replies

2. Shell Programming and Scripting

Merge csvs with column headers

hello gurus, Somebody must have done this before, I couldn't find anything. Please redirect me if this was solved before, and if not please help. To the problem now, I have multiple csv files (about 1000) which I need to concatenate by column header. The final file should have a superset... (4 Replies)
Discussion started by: abh.kumar
4 Replies

3. Shell Programming and Scripting

Sar -u generates multiple column headers in csv file

Hi All, The below sar -u command generates multiple column headers in csv file Expected output should print column headers only once in the csv file shell script: $cat sar_cpu_EBS.sh #!/bin/bash while ; do sar -u 15 1 | awk '/^/ {print $1,$2,$4,$6,$7}' | tr -s ' ' ',' >>... (6 Replies)
Discussion started by: a1_win
6 Replies

4. Shell Programming and Scripting

Merge column headers and transpose

Hello Everyone! I am new on this forum and this is my first post. I wish to apologize for my, not canonical, English. I would like to solve this problem but I have no clue of how do it!I will be grateful if someone could help me! I have a table like this: gene TF1 TF2 TF3 TF4 gene1 1 2 3 4... (5 Replies)
Discussion started by: giuliangiuseppe
5 Replies

5. Shell Programming and Scripting

Transpose field names from column headers to values in one column

Hi All, I'm looking for a script which can transpose field names from column headers to values in one column. for example, the input is: IDa;IDb;IDc;PARAM1;PARAM2;PARAM3; a;b;c;p1val;p2val;p3val; d;e;f;p4val;p5val;p6val; g;h;i;p7val;p8val;p9val; into the output like this: ... (6 Replies)
Discussion started by: popesk
6 Replies

6. Shell Programming and Scripting

Matching words based on column headers

Hi , Pls help on this. Input file: NAME1 BSC1 TEXT ID 1 MAINSFAIL TEXT ID 2 DGON TEXT ID 3 lOADONDG NAME2 BSC2 TEXT ID 1 DGON TEXT ID 3 lOADONG (1 Reply)
Discussion started by: bha148
1 Replies

7. UNIX for Dummies Questions & Answers

Sort by Column Headers

Hi All, I am new to UNIX can you please help me to sort a file with different columns my file looks like this $ cat gaut.txt UID PID PPID PGID SID C STIME TTY TIME CMD liveuser 3008 2892 3008 3008 0 11:58 ? 00:00:00 gnome-session liveuser 3019 1 ... (8 Replies)
Discussion started by: cgk1983
8 Replies

8. Shell Programming and Scripting

Excel Column Headers

cat ABC.log | egrep "Error 500" >> /tmp/Logs.log egrep "<Mango>.*<.Mango>" Logs.log | sed -e "s/^.*<Mango/<Mango/" | cut -f2 -d">"| cut -f1 -d"<" >> /tmp/temp1.xls egrep "<Apple>.*<.Apple>" Logs.log | sed -e "s/^.*<Apple/<Apple/" | cut -f2 -d">"| cut -f1 -d"<" >> /tmp/temp2.xls print Heading1,... (1 Reply)
Discussion started by: pk_eee
1 Replies

9. Shell Programming and Scripting

Removing Headers and a Column

I have a text file in unix with a layout like this Column 1 - 1-12 Column 2 - 13-39 Column 3 - 40-58 Column 4 - 59-85 Column 5 - 86-120 Columbn 6 - 121-131 The file also has a header on the first 6 lines of each page. Each page is 51 lines long. So I want to remove the header from each... (30 Replies)
Discussion started by: DerangedNick
30 Replies

10. Shell Programming and Scripting

Capturing the values of column in one parameter

Hi, I am trying to capture the values of a column in a parameter..here is what I wanted to do... 1,2,3,4 2,3,4,1 3,4,1,2 4,1,2,3 is there any way that I could get the values of column values into one parameter?? Here is what I want... COL1=1,2,3,4 COL2=2,3,4,1 ... (5 Replies)
Discussion started by: mgirinath
5 Replies
Login or Register to Ask a Question