Counting occurence of a particular character on each line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Counting occurence of a particular character on each line
# 1  
Old 06-26-2008
Counting occurence of a particular character on each line

Hi,
I have the following data in a flat file:
Code:
abcd_efgh_ijkl_20080522.dat|20080602222508|1357
abcd_efgh_ijkl_20080522.dat|20080602222508|1357
abcd_efgh_ijkl_20080522.dat|20080602222508|1357

I need to check the no. of occurence of "|" (pipe) on each line and the output should look like below:
Code:
2|abcd_efgh_ijkl_20080522.dat|20080602222508|1357
2|abcd_efgh_ijkl_20080522.dat|20080602222508|1357
2|abcd_efgh_ijkl_20080522.dat|20080602222508|1357

where the first line is the no. of occurence of Pipe on each line.

Can anyone help me on this?
Thanks in advance

Last edited by Yogesh Sawant; 06-26-2008 at 05:44 AM.. Reason: added code tags
# 2  
Old 06-26-2008
Code:
awk -F\| '$0=NF-1FS$0' file

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 06-26-2008
Hi Radoulov,
Thanks for your reply. It works the way I want.

It might happen the flat file may have irregular no. of columns like below:
abcd_efgh_ijkl_20080522.dat|20080602222508|1357
abcd_efgh_ijkl_20080522.dat|20080602222508|1357
abcd_efgh_ijkl_20080522.dat|20080602222508|1357
abcd_efgh_ijkl_20080522.dat|20080602222508|1357|1357|1357
abcd_efgh_ijkl_20080522.dat|20080602222508|1357|1357
abcd_efgh_ijkl_20080522.dat|20080602222508

If I run your command, it gives me correct output as:
2|abcd_efgh_ijkl_20080522.dat|20080602222508|1357
2|abcd_efgh_ijkl_20080522.dat|20080602222508|1357
2|abcd_efgh_ijkl_20080522.dat|20080602222508|1357
4|abcd_efgh_ijkl_20080522.dat|20080602222508|1357|1357|1357
3|abcd_efgh_ijkl_20080522.dat|20080602222508|1357|1357
1|abcd_efgh_ijkl_20080522.dat|20080602222508

The flat file should contain fixed number of columns, i.e. always 3, but it may also have the possibility of having the above irregular columns.

So I need to find the lines which are having irregular no. of columns, i.e. having <> 3.
Last output required is:
4|abcd_efgh_ijkl_20080522.dat|20080602222508|1357|1357|1357
3|abcd_efgh_ijkl_20080522.dat|20080602222508|1357|1357
1|abcd_efgh_ijkl_20080522.dat|20080602222508

I hope I am able to provide proper details.
Can I get the solution to the above problem?
Thanks in advance
# 4  
Old 06-26-2008
Code:
awk -F\| 'NF!=3&&$0=NF-1FS$0' file

# 5  
Old 06-26-2008
Hi Radoulov, Thank you very much for your help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - remove begin of line up to the third and including occurence of character

hello. How to remove all characters in a line from first character ( a $ ) until and including the third occurrence of that character ( $ ). Any help is welcome. (10 Replies)
Discussion started by: jcdole
10 Replies

2. Shell Programming and Scripting

Splitting based on occurence of a Character at fixed position

I have a requirement where i need to split a file based on occurence of a character which is present at a fixed position. Description is as below: 1. The file will be more than 1 Lakh records. 2. Each line will be of fixed length of 987 characters. 3. At position 28 in each line either 'C' or... (9 Replies)
Discussion started by: Neelkanth
9 Replies

3. Shell Programming and Scripting

How to identify the occurence of a pattern between a unique character?

hi, is it possible to find the number of occurences of a pattern between two paranthesis. for e.g i have a file as below. >>{ >>hi >>GoodMorning >>how are you? >>} >>is it good, >>tell me yes, if it is good In the above file, its clear the occurence of word "Good"... (17 Replies)
Discussion started by: divak
17 Replies

4. UNIX for Dummies Questions & Answers

counting the occurence of a word

In a file I have to count a particular word. like i need apache how many times. I tried this $ tr "\011" "\012\012"<foo1 | tr -cd "" |sort\uniq -c but I got result like this 32 apache 18 dns 12 doctor Please sugest me (4 Replies)
Discussion started by: pranabrana
4 Replies

5. Shell Programming and Scripting

Find index of last occurence of a character within a string

I need to find the index of last '|' (highlighted in bold) in awk : |ifOraDatabase.Lastservererr<>0then|iferr_flag<>0then|end if Please suggest a way... Thanks (5 Replies)
Discussion started by: joyan321
5 Replies

6. UNIX for Dummies Questions & Answers

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution. (5 Replies)
Discussion started by: sumit207
5 Replies

7. UNIX for Advanced & Expert Users

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution.:) (1 Reply)
Discussion started by: sumit207
1 Replies

8. Shell Programming and Scripting

First occurence of character in a file

Hi All I have the following contents in a file say in a file name called 'FILE1' *********** Start of the file ************** SANDIO000456GROJ8900 SANDIO2338923GRJH900 *********** End of the file ******************* I want to cut the first line which has the characters.... (6 Replies)
Discussion started by: dhanamurthy
6 Replies

9. UNIX for Dummies Questions & Answers

counting the occurence of particular characters

I want to list the occurence of particular characters in a line. my file looks like this a,b,c,d e,f,g h,y:e,g,y s f;g,s,w and I want to count how many commas are in each line so the file in the end looks like this: a,b,c,d 3 e,f,g 2 h,y:e,g,y s 3 f;g,s,w ... (2 Replies)
Discussion started by: Audra
2 Replies

10. Shell Programming and Scripting

Counting occurence of a coma

I have a .csv file that I would like to count on each line the occurences of the coma. Each line should have the same amount, but as an integrity check I would like to include in my script this count. What is the best way of doing this? (3 Replies)
Discussion started by: dbrundrett
3 Replies
Login or Register to Ask a Question