LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-31-2004, 07:03 AM   #1
pantera
Member
 
Registered: May 2004
Posts: 80

Rep: Reputation: 15
word count


how to find and print only the first word from each line of a file using C
 
Old 08-31-2004, 08:14 AM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Good Lord, C is totally wrong choice for that.

Solution in 6 other languages:
awk:
Code:
awk '{print $1}' foo.txt
sed:
Code:
sed 's/^\([^ ]*\).*$/\1/' foo.txt
perl:
Code:
perl -pe '($_) = split(" ", $_);  $_=~s/$/\n/' foo.txt
or other perl implementation:
Code:
perl -pe '$_=~ s/^(\w+).*$/$1/;' foo.txt
tcl: (inside tclsh)
Code:
while {[ gets stdin line] >= 0} { puts [lindex [split $line " "] 0] }
shell script/coreutils:
Code:
cut -f1 -d' ' foo.txt
python:
Code:
python -c $'import sys;\ndef fword(x): print x.split(" ")[0].strip();\nmap(fword,sys.stdin.readlines());' < foo.txt
Do you still want to do that in C?
 
Old 08-31-2004, 08:23 AM   #3
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Open a file
then get a line in buffer using getline()------------------> see " man getline " for details.
then use strchar() to locate a space character --------------> see " man strchar()"
This will give you the index of first space charecter in that buffer.
strncpy ( first_word , buffer , index_of_space);
printf (first_word)
And then read the next line until the end of file. The code will be something like.

while( feof ( fileptr ) )
{
getline ( buffer, length , fileptr);
strncpy ( first_word , buffer , strchar( *buffer , " " ) );
printf (first_word);
}

There may be more efficient way to do that.
 
  


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
word count in a line pantera Programming 4 08-25-2004 02:14 PM
Should posts in general count on your post count? Joey.Dale General 16 01-27-2004 02:31 AM
Really basic OOo or Staroffice Q re: word count Calum Linux - Software 8 06-04-2003 06:57 AM
Word count in paragraph - Open Office, Sutekh Linux - Software 10 04-19-2003 11:27 PM
Viewers for Word, Word Perfect, etc. Claude Williams Linux - General 2 11-22-2001 05:01 AM


All times are GMT -5. The time now is 09:41 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration