Script to find the number of tab delimiters in a line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script to find the number of tab delimiters in a line
# 1  
Old 09-27-2008
Script to find the number of tab delimiters in a line

Hi,
I need to find the number of tab delimiters in the first line of a file.So using
word=`head -1 files.txt`
I have extracted the first line of file into a variable word.It has 20 tab delimted columns.So can anyone help me in finding the number of delimiters?
I am using csh and I am a beginner.I feel like printf and awk are not working. Not sure
Also I would like to know how can I call this shell using Autosys Jobs by giving the file name as a parameter.Do I need to write the script something like a function?
As of now I am reading from the user as echoing and reading the filename.But how I will implement in Autosys?
Please help.
Thanks,
Poornima
# 2  
Old 09-29-2008
Few people here are good with csh, me included. There are good reasons to avoid it.
However, you can get the answer with awk. Try gawk or nawk if you have them.
Code:
head -1 file | awk -F'*' '{print $NF -1 }'

* where * -- the tab key

Last edited by jim mcnamara; 09-29-2008 at 11:36 AM.. Reason: gotgot to make FS == a tab
# 3  
Old 09-29-2008
Hi u can try this


This will give the count of Tab or delimeter
1. head -1 file | awk -F'<delimiter>' '{print NF -1}'

2. head -1 file | awk '{print NF -1}' as defult it takes space and tabs as delimeter

Thanks
# 4  
Old 09-29-2008
Hammer & Screwdriver Here is another approach

Code:
> cat infile2
joe     blue    1       15      99
harry   orange  5       13      2
steve   red     57      99      98
> head -1 infile2 | tr -d "\n" | tr "\t" "\n" | wc -l
4

Explained:
head -1 takes the top line
tr -d "\n" gets rid of extra line feeds
tr "\t" "n" converts tabs to line feeds
wc -l counts the number of lines (that line feeds caused)
# 5  
Old 09-29-2008
Thanks a lot gurus.
I used Joeyg's approach and it worked.
When I tried Jim's solution head -1 file | awk -F'|' '{print $NF -1 }', it is giving me -1.Just of curiosity..What does that mean?
Anyway thanks.It helped me..
# 6  
Old 09-29-2008
my bad: wrong field separator - I edited the post.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find encapsulating function name from line number?

I am looking at a log file which just tells me the filename and the line number inside that file that has the Error. What I am interested is knowing the encapsulating function. For example, here are the contents of the log file Error: foo.file on line wxy Error: foo.file... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

Count The Number Of Delimiters using awk or better

What to know the way to count the number of delimiters in each record by ignoring the escape delimiters. Sample Data: 12345678|ABN\|XYZ MED CHEM PTY. LTD.|C||100.00|22|AB"C\|Corp|"XYZ|CDEF"| I'm using awk -F'|' '{ print NF-1 }' command to find the number of delimiters. this command... (8 Replies)
Discussion started by: BrahmaNaiduA
8 Replies

4. Shell Programming and Scripting

How to find line number?

I have a data file (which has five columns) from which im finding column count of all the records and writing into separate file say "colcnt.txt". And I find one (or more) records have less column counts (i.e split records). I need to know which record(s) have that split scenario. Is there any way... (4 Replies)
Discussion started by: Prashanth B
4 Replies

5. Shell Programming and Scripting

Find line number

How to find a line number? I have a file: 1 5 8 9 10 15 Is there a simple method to find out on which line for example the 9 is written? (3 Replies)
Discussion started by: jds93
3 Replies

6. Shell Programming and Scripting

Concatinating the lines based on number of delimiters

Hi, I have a problem to concatenate the lines based on number of delimiters (if the delimiter count is 9 then concatenate all the fields & remove the new line char bw delimiters and then write the following data into second line) in a file. my input file content is Title| ID| Owner|... (4 Replies)
Discussion started by: bi.infa
4 Replies

7. Shell Programming and Scripting

to find the number from a line.

Hi, I need to write a script which will have a text string as a input and the output should find out the number in the text string and add one to it. Eg: Input => asfdosainovih1234lnsiohn Output => 1235 All the numbers in the text will be together and only one time in the line. ... (2 Replies)
Discussion started by: vikings.svnit
2 Replies

8. Shell Programming and Scripting

how to find the line number of a pattern of first appearance??

Hi, I am doin a project in shell script please answer the above question..... waiting........ (2 Replies)
Discussion started by: shivarajM
2 Replies

9. UNIX for Dummies Questions & Answers

How to count number of delimiters in a file name

I have a list of files with names as "FULL_abcd_xyz_timestamp.txt" and "FULL_xx_abcd_xyz_timestamp.txt". I am writing a script with a 'for loop' to take each file, strip the "FULL" and "timestamp" from the file name and do some actions on the contains of the file. So I need to know the number of... (4 Replies)
Discussion started by: ayanbiswas
4 Replies

10. Shell Programming and Scripting

simple script to find the number of "tab"s...but,...

------------------------------ $x=" hi"; $tabspace=0; while ($x =~ /\t/g ) { $tabspace++; } print $tabspace; --------------------------------- 1.)when i tried it without "g" ($x = ~/\t/ )... when i run the script it utilizes around 95% cpu and system hangs and i did "End process"... (0 Replies)
Discussion started by: sekar sundaram
0 Replies
Login or Register to Ask a Question