The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
First occurence of character in a file dhanamurthy Shell Programming and Scripting 6 05-13-2008 12:56 AM
counting the occurence of particular characters Audra UNIX for Dummies Questions & Answers 2 03-28-2008 10:01 AM
first occurence and line number mercuryshipzz Shell Programming and Scripting 9 01-22-2008 07:42 PM
Counting position of a character rochitsharma UNIX for Advanced & Expert Users 3 11-27-2007 06:26 PM
Counting occurence of a coma dbrundrett Shell Programming and Scripting 3 01-22-2004 05:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-26-2008
hey_mak hey_mak is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 3
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 04:44 AM.. Reason: added code tags
  #2 (permalink)  
Old 06-26-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Code:
awk -F\| '$0=NF-1FS$0' file
Use nawk or /usr/xpg4/bin/awk on Solaris.
  #3 (permalink)  
Old 06-26-2008
hey_mak hey_mak is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 3
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 (permalink)  
Old 06-26-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Code:
awk -F\| 'NF!=3&&$0=NF-1FS$0' file
  #5 (permalink)  
Old 06-26-2008
hey_mak hey_mak is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 3
Hi Radoulov, Thank you very much for your help.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0