Need help of counting no of column of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help of counting no of column of a file
# 1  
Old 07-24-2015
Need help of counting no of column of a file

Hi All ,

I got stuck on the below scenario.If anyone can help me ,that will be really helpful.

I have a target hdfs file layout.I need to know the no of column in that file.
Code:
Target_RECRD_layout
{
    ABC_ID EN NOTNULLABLE,
    ABC_COUNTRY CHARACTER ENCODING ASCII NOTNULLABLE,
    FILLER1 CHARACTER ENCODING ASCII NOTNULLABLE,
    ABC_SOURCE CHARACTER ENCODING ASCII NOTNULLABLE,
    ABC_SOURCE_NUM EN NOTNULLABLE,
    FILLER2 CHARACTER ENCODING ASCII NOTNULLABLE,
    ABC_CODE EN NOTNULLABLE,
    ABC_CODE_ALPHA CHARACTER ENCODING ASCII NOTNULLABLE,
    ABC_DATA CHARACTER ENCODING ASCII NOTNULLABLE,
    ABC_DATA_ALPHA CHARACTER ENCODING ASCII NOTNULLABLE
   }

In the above layout 1st column represents the column name.We have 10 columns in the above layout.Can any one tell me how can I get the column count from the above file layout through unix command.Thanks.
# 2  
Old 07-24-2015
Hello STCET22,

Following may help you in same.
Code:
 awk '(NR>1 && $0 !~ /\{/ && $0 !~ /\}/){if(NF){A[++o]=$1}} END{print "Column_Number" OFS "Column_Name";for(i=1;i<=o;i++){print i OFS A[i]}}' Input_file

OR Non-one liner form of solution.
Code:
awk '
       (NR>1 && $0 !~ /\{/ && $0 !~ /\}/){
                                          if(NF)
                                         {
                                          A[++o]=$1
                                         }
                                         }
                                      END{
                                          print "Column_Number" OFS "Column_Name";
                        for(i=1;i<=o;i++){
                                          print i OFS A[i]
                                         }
                                         }
    ' Input_file

Output will be as follows.
Code:
1 ABC_ID
2 ABC_COUNTRY
3 FILLER1
4 ABC_SOURCE
5 ABC_SOURCE_NUM
6 FILLER2
7 ABC_CODE
8 ABC_CODE_ALPHA
9 ABC_DATA
10 ABC_DATA_ALPHA

In case you need only column count then please use following.
Code:
 awk '(NR>1 && $0 !~ /\{/ && $0 !~ /\}/){if(NF){A++}} END{print A}'  Input_file
 10

Thanks,
R. Singh
# 3  
Old 07-24-2015
Given the file structure is exactly as posted above, this might help:
Code:
awk 'NF>1{CNT++} END {print CNT}' file
10

# 4  
Old 07-24-2015
Hi RudiC ,

Thanks a lot for your help.I am not very much familiar with awk part.If you kindly explain the below block of code ,that will be really helpful for my understanding.Thanks.

Code:
awk 'NF>1{CNT++} END {print CNT}' file


RavinderSingh13 ,

After executing the below code ,I m getting result as below.But I dont need row no 1 in my result.Kindly check this and if possble pls explain me for my understanding.Thnks.
Code:
Column_Number Column_Name
1 Target_RECRD_layout
2 ABC_ID
3 ABC_COUNTRY
4 FILLER1
5 ABC_SOURCE
6 ABC_SOURCE_NUM
7 FILLER2
8 ABC_CODE
9 ABC_CODE_ALPHA
10 ABC_DATA
11 ABC_DATA_ALPHA

# 5  
Old 07-24-2015
Hello STCET22,

Following are the explanations for all codes above posted in this thread.
Code:
 awk '                                                                               
       (NR>1 && $0 !~ /\{/ && $0 !~ /\}/){                                             ###### Checking condition if line number is greater than 1 and line doesn't have characters { AND } in it then perform below.
                                          if(NF)                                       ###### If number of fields are NOT empty.
                                         {
                                          A[++o]=$1                                    ###### Creating an array named A whose index is a variable whose value is increasing each time this operation is called.
                                         }
                                         }
                                      END{
                                          print "Column_Number" OFS "Column_Name";     ###### printing the strings (which you doesn't want in output) Column_Number and Column_Name
                        for(i=1;i<=o;i++){                                             ###### Initating a for loop which will run from variab;e i's value from 1 to till o's value.
                                          print i OFS A[i]                             ###### printing the index of array a AND value of A by A[i]
                                         }
                                         }
    ' Input_file                                                                       ###### mentioning input_file name here.
  
  
 
awk '(NR>1 && $0 !~ /\{/ && $0 !~ /\}/){           ###### Checking condition if line number is greater than 1 and line doesn't contain strings { AND } in them.
if(NF){                                            ###### Checking condition if number of field is NOT NULL.
A++}}                                              ###### Incrementing a variable named A's value each time above condition is TRUE.
END{print A}'                                      ###### At last printing the value of variable A, which will provide the columns count in input file.
Input_file                                         ###### mentioning input_file name here.

RudiC's code:
Code:
awk 'NF>1                                          ###### If Number of fields are greater than 1 in a line then perform following actions.
{CNT++}                                            ###### Incrementing a variable named CNT's value each time above condition is TRUE.
END {print CNT}'                                   ###### At last printing the value of variable CNT, which will provide the columns count in input file.
file                                               ###### mentioning input_file name here.

Hope this helps you.

Thanks,
R. Singh
# 6  
Old 07-24-2015
I have read this thread several times, and my thinking is that row one is only the schema for the rest of the file, and STCET22 wants to know the number of fields in each of the data rows.
In order to determine the number of fields per row, we need to know the field separator (assuming that the fields are not fixed length), and the record separator.
The likely most successful way of answering the original question is to write a program in the same language that created the file, and count null or blank fields.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting a consecutive number in column 2

Hi, I have a input file which contains following data 0 1 0 2 0 3 0 4 0 8 0 9 0 11 1 1 1 2 1 6 1 7 1 8 1 9 2 1 2 11 2 12 (12 Replies)
Discussion started by: Ryan Kim
12 Replies

2. Shell Programming and Scripting

Counting the number of element in each column

Hello, I have a file as follows: ENSGALG00000000189 ENSGALG00000000189 ENSGALG00000000189 ENSGALG00000000215 ENSGALG00000000215 ENSGALG00000000218 ... (5 Replies)
Discussion started by: Homa
5 Replies

3. Shell Programming and Scripting

Counting specific column and add result in output

Hi all, I have a quick question: I have a 4 column tab-separated file. I want to count the number of times each unique value in column 2 appears and add that number in a 5th column. I have the following input file: waterline-n below-sheath-v 14.8097 A dock-n below-sheath-v ... (4 Replies)
Discussion started by: owwow14
4 Replies

4. Shell Programming and Scripting

Counting no of spl character occurance column wise

Hi i have a file delimited with ","as below and i need to handle scenario like col1,col2 fields have special character '|', i need count of special character value column wise as given in col3 and col4. pls help me to reslove this. Source file name,col1,col2,col3,col4 one,2,3 two,2|3,2|3... (2 Replies)
Discussion started by: Ganesh L
2 Replies

5. Shell Programming and Scripting

Help with awk in counting characters based on a column

Hello, I am using Awk in UBUNTU 12.04. I have a file as follows with 2172 rows and 44707 columns. ABO and GPO are the names of my populations. ABO_1 1 2 ABO_1 1 2 ABO_2 1 1 ABO_2 1 2 GPO_1 1 1 GPO_1 2 2 GPO_2 1 0 GPO_2 2 0I want to count the number of 1s and 2s in... (7 Replies)
Discussion started by: Homa
7 Replies

6. Shell Programming and Scripting

Counting occurences in column

Hi guys! I have a problem writing script that would convert this input into this output: I have an awk script that counts occurences of a sign in a column, but don't know how to change it so that I would give me desired output. script awk '{count++}END{for(j in count)... (2 Replies)
Discussion started by: grincz
2 Replies

7. Shell Programming and Scripting

Collapsing and counting by key column in a sorted file

Hi I have a tab separated file with reads mappings of more than 2 million reads> the file is sorted by ID and looks like the following: SeqID Seq FreqSeq PosSeq HWI-EA332_0036:5:100:10131:16361#ATGC/1 GACTTGAGGTCTCCCCCGCA 1 TZRTMR_40497:317:+... (4 Replies)
Discussion started by: ramouz87
4 Replies

8. Shell Programming and Scripting

counting lines containing two column field values with awk

Hello everybody, I'm trying to count the number of consecutive lines in a text file which have two distinctive column field values. These lines may appear in several line blocks within the file, but I only want a single block to be counted. This was my first approach to tackle the problem (I'm... (6 Replies)
Discussion started by: origamisven
6 Replies

9. Shell Programming and Scripting

Counting rows line by line from a specific column using Awk

Dear UNIX community, I would like to to count characters from a specific row and have them displayed line-by-line. I have a file called testAwk2.csv which contain the following data: rabbit penguin goat giraffe emu ostrich I would like to count in the middle row individually... (4 Replies)
Discussion started by: vnayak
4 Replies

10. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies
Login or Register to Ask a Question