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
# 8  
Old 06-14-2011
where is my echo statement in the output?

what shell you are using ?
# 9  
Old 06-14-2011
Quote:
Originally Posted by itkamaraj
where is my echo statement in the output?

what shell you are using ?
echo $SHELL
/bin/bash

Code 1:
echo $delimiter_char
if [ $delimiter_char eq "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

Output 1:
./get_col_lengths.sh CUSTOMER.dat 'TAB'
TAB
./get_col_lengths.sh: line 21: [: eq: binary operator expected
Coming here for TAB?
No. of Records in CUSTOMER.dat : 200
No. of Columns in CUSTOMER.dat : 1


Code 2:
echo $delimiter_char
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

Output 2:
./get_col_lengths.sh CUSTOMER.dat 'TAB'
TAB
Inside the TAB condition
No. of Records in CUSTOMER.dat : 200
No. of Columns in CUSTOMER.dat : 1
# 10  
Old 06-14-2011
use -eq

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

for your code

use == ( comparison )

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

Also post the first line (which you are trying to count)
# 11  
Old 06-14-2011
Quote:
Originally Posted by itkamaraj
use -eq

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

for your code

use == ( comparison )

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

Also post the first line (which you are trying to count)

if i use if [ $delimiter_char == "TAB" ]

./get_col_lengths.sh CUSTOMER.dat 'TAB'
TAB
Inside the TAB condition
No. of Records in CUSTOMER.dat : 200
No. of Columns in CUSTOMER.dat : 1

here is the first line of CUSTOMER.dat

MWCU5203691 2010-09-30 14:12:00.000000 GATE-IN PHDVOSW GATE-IN 2010-09-30 14:12:00.000000 EXP N 22489 49580 26889 59280 Y N MCC 688888 5RK 1011 MCC033271 5IZUNBEDJVU1O N 2010-09-30 09:13:42.408011 2010-10-25 00:32:21.615382 2010-10-01 08:22:47.268264 2010-12-02 05:56:18.000000 I
# 12  
Old 06-14-2011
don't store the header in the variable. (it is losing the tab )

use it like this

Code:
col_cnt=`nawk -F"\t" '{ if(NR==1) { print NF }}' $1`

Code:
bash-3.00$ cat /tmp/myfile   # tab seperated file
abcd    edfg    hijk
bash-3.00$ nawk  -F"\t" '{print $1}' /tmp/myfile
abcd
bash-3.00$ my_var=`cat /tmp/myfile | head -1`
bash-3.00$ echo $my_var  # variable loses the tab 
abcd edfg hijk
bash-3.00$ cat /tmp/myfile      # tab seperated file
abcd    edfg    hijk
bash-3.00$ echo $my_var | nawk  -F"\t" '{print $1}'   # nawk is not able to recognize the delimeter
abcd edfg hijk

This User Gave Thanks to itkamaraj For This Post:
# 13  
Old 06-14-2011
Quote:
Originally Posted by itkamaraj
don't store the header in the variable. (it is losing the tab )

use it like this

Code:
col_cnt=`nawk -F"\t" '{ if(NR==1) { print NF }}' $1`

Code:
bash-3.00$ cat /tmp/myfile   # tab seperated file
abcd    edfg    hijk
bash-3.00$ nawk  -F"\t" '{print $1}' /tmp/myfile
abcd
bash-3.00$ my_var=`cat /tmp/myfile | head -1`
bash-3.00$ echo $my_var  # variable loses the tab 
abcd edfg hijk
bash-3.00$ cat /tmp/myfile      # tab seperated file
abcd    edfg    hijk
bash-3.00$ echo $my_var | nawk  -F"\t" '{print $1}'   # nawk is not able to recognize the delimeter
abcd edfg hijk

Thanks a lot.
Its working now. Smilie
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