LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-09-2012, 09:10 AM   #1
sunveer
Member
 
Registered: Jul 2012
Posts: 139

Rep: Reputation: Disabled
Count Values in a field


I have to count the number of values in a field of a file.

for eg. if there is a line in a file

abc:xyz:d

then, I want to the count the number of values in second field. The result must be therefore 3.
 
Old 09-09-2012, 09:59 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
I guess this thread should go into the programming subsection.
In what language are you trying to do it?
Have you assembled something till now?
 
Old 09-10-2012, 01:52 AM   #3
sunveer
Member
 
Registered: Jul 2012
Posts: 139

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by 414N View Post
I guess this thread should go into the programming subsection.
In what language are you trying to do it?
Have you assembled something till now?
I want a command to do that like awk performs certain manipulations. I think awk will do but don't know how to get it work for this.
 
Old 09-10-2012, 02:19 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
So you simply want the total number of characters in the second field on each line?

There are many ways to do something like this, and it might help if you gave us some context to work with. i.e. is this for use in a larger script? If so, show the code you have already so we can help with an integrated solution.

But to give you a simple awk version:

Code:
awk -F: '{ print length( $2 ) }' infile.txt
http://www.gnu.org/software/gawk/man...-Functions.htm

If you need something more complex, you'll have to provide more details.


Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

Last edited by David the H.; 09-10-2012 at 02:22 AM. Reason: expanded
 
Old 09-10-2012, 06:07 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Another suggestion using bash only. This assumes all lines in the file have the same format as mentioned (multiple fields separated by colon):
Code:
while IFS=":" read one two other; do echo ${#two}; done < filename
Inside the loop you can put everything you need to process the retrieved value. Obviously this strongly depends on your requirement and which is the scripting language that fits it at best.

Last edited by colucix; 09-10-2012 at 06:09 AM. Reason: english
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Count occurrence of character in field and print in a new field Trd300 Linux - Newbie 5 03-21-2012 08:57 PM
want to send mail if all values in second field are zero saurabhmehan Linux - Newbie 3 09-01-2010 07:53 AM
how to grep for only the values of a specific field hchoonbeng Linux - Newbie 3 11-19-2008 09:20 AM
count values in java? rabbit2345 Programming 1 02-28-2008 07:57 AM
storing multiple values within one field in mysql antken Programming 8 12-15-2002 11:08 PM


All times are GMT -5. The time now is 07:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration