Problem while counting number of fields in TAB delimited file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Problem while counting number of fields in TAB delimited file
# 1  
Old 06-13-2011
Problem while counting number of fields in TAB delimited file

I'm facing a strange problem, please help me out.
Here we go.

I want to count number of fields in particular file.
filename and delimiter character will be passed through parameter.

On command prompt if i type following i get 27 as output (which is correct)
Code:
cat customer.dat | head -1 | awk -F"\t" '{print NF}'

but when i type following it gives 32 as output

Code:
./get_col_lengths.sh customer.dat 'TAB'

Code:
data_file=$1
delimiter_char=$2
header_line=`cat $data_file | head -1`
if [ $delimiter_char = "TAB" ]
then
  col_cnt=`echo $header_line | awk -F"\t" -f '{print NF}'`
else
  col_cnt=`echo $header_line | awk -F"$delimiter_char" '{print NF}'`
fi

it is giving 32 because thr are timestamp (date time) fields in file. So my impression is it is not setting tab as delmiter

Last edited by Scott; 06-14-2011 at 02:25 AM.. Reason: Please use code tags
# 2  
Old 06-13-2011
Try to change quotes:
Code:
col_cnt=`echo $header_line | awk -F'\t' -f '{print NF}'`

# 3  
Old 06-14-2011
Quote:
Originally Posted by yazu
Try to change quotes:
Code:
col_cnt=`echo $header_line | awk -F'\t' -f '{print NF}'`

this too is not working Smilie
# 4  
Old 06-14-2011
why there is one more -f ?


Code:
col_cnt=`echo $header_line | awk -F"\t" -f '{print NF}'`

# 5  
Old 06-14-2011
Quote:
Originally Posted by itkamaraj
why there is one more -f ?


Code:
col_cnt=`echo $header_line | awk -F"\t" -f '{print NF}'`

sorry for the typo mistake.
here is the correct code which is not working.

data_file=$1
delimiter_char=$2
header_line=`cat $data_file | head -1`
if [ $delimiter_char = "TAB" ]
then
col_cnt=`echo $header_line | awk -F'/t' '{print NF}'`
else
col_cnt=`echo $header_line | awk -F"$delimiter_char" '{print NF}'`
fi
# 6  
Old 06-14-2011
i guess, the if statement is failing..

Code:
 
if [ $delimiter_char = "TAB" ]
then
echo "Inside the TAB condition"
col_cnt=`echo $header_line | awk -F'/t' '{print NF}'`
else
echo "Coming here for TAB?"
col_cnt=`echo $header_line | awk -F"$delimiter_char" '{print NF}'`
fi

execute the script as below

Code:
./get_col_lengths.sh customer.dat 'TAB'

see, which echo is printing

change the if condition as below and execute it once again

Code:
if [ "$delimiter_char" eq "TAB" ]

# 7  
Old 06-14-2011
Quote:
Originally Posted by itkamaraj
i guess, the if statement is failing..

Code:
 
if [ $delimiter_char = "TAB" ]
then
echo "Inside the TAB condition"
col_cnt=`echo $header_line | awk -F'/t' '{print NF}'`
else
echo "Coming here for TAB?"
col_cnt=`echo $header_line | awk -F"$delimiter_char" '{print NF}'`
fi

execute the script as below

Code:
./get_col_lengths.sh customer.dat 'TAB'

see, which echo is printing

change the if condition as below and execute it once again

Code:
if [ "$delimiter_char" eq "TAB" ]

No dear, "if" is working, tried that already
still i'm putting output here

./get_col_lengths.sh CUSTOMER.dat 'TAB'
No. of Records in CUSTOMER.dat : 200
No. of Columns in CUSTOMER.dat : 1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. UNIX for Dummies Questions & Answers

Replace period in a tab delimited file to a number

I have a file like this. It is tab delimited. Unfortunately, the missing data was filled in with a period "." (see the leading lines 1-5 columns) I want to substitute the periods for misisng data with an integer "-999". however, I do not want the global replace to change the other periods seen... (7 Replies)
Discussion started by: genehunter
7 Replies

3. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

4. Shell Programming and Scripting

How to make tab delimited file to space delimited?

Hi How to make tab delimited file to space delimited? in put file: ABC kgy jkh ghj ash kjl o/p file: ABC kgy jkh ghj ash kjl Use code tags, thanks. (1 Reply)
Discussion started by: jagdishrout
1 Replies

5. Shell Programming and Scripting

Help with converting Pipe delimited file to Tab Delimited

I have a file which was pipe delimited, I need to make it tab delimited. I tried with sed but no use cat file | sed 's/|//t/g' The above command substituted "/t" not tab in the place of pipe. Sample file: abc|123|2012-01-30|2012-04-28|xyz have to convert to: abc 123... (6 Replies)
Discussion started by: karumudi7
6 Replies

6. Shell Programming and Scripting

Print records which do not have expected number of fields in a comma delimited file

Hi, I have a comma (,) delimited file, in which few fields are enclosed with in double quotes " ". I have to print the records in the file which donot have expected number of field with the line number. File1 ==== name,desgnation,doj,project #header#... (7 Replies)
Discussion started by: machomaddy
7 Replies

7. UNIX for Dummies Questions & Answers

tab delimited file that is not tab delimited.

Hi Forum I have a tab delimited file that opens well in Openoffice calc (excel). But when I perform any operation in command line, it reads the file incorrectly. When I 'save As' the same file in office as tab delimited then it works fine. The file that I think is tab delimited is actually... (8 Replies)
Discussion started by: imlearning
8 Replies

8. UNIX for Advanced & Expert Users

merge two tab delimited file with exact same number of rows in unix/linux

Hi I have two tab delimited file with different number of columns but same number of rows. I need to combine these two files in such a way that row 1 in file 2 comes adjacent to row 1 in file 1. For example: The content of file1: field1 field2 field3 a1 a2 a3 b1 b2 b3... (2 Replies)
Discussion started by: mary271
2 Replies

9. Shell Programming and Scripting

Counting non empty fields and calculating with that number

Hello all, I have a problem with a skript of mine: My input has the following format 1,33296 transcript_id"ENSRNOT00000018629" 0 1,33296 0 0 transcript_id"ENSRNOT00000029014" 0 0,907392 transcript_id"ENSRNOT00000016905" 0,907392 0 transcript_id"ENSRNOT00000053370" 0 0... (0 Replies)
Discussion started by: DerSeb
0 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question