LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 03-13-2007, 11:39 AM   #1
baks
Member
 
Registered: Feb 2007
Posts: 41

Rep: Reputation: 15
BASH length of file


Hi, I have a problem and was wondering whether anyone can help ?

I have a file with a list of words all on one line separated by a delimiter (colon/ and at the moment I jusr assign 10 (the number of words in the file) to a variable but I need to be able to do this at runtime so that a piece of code would automatically be able to find out how many words are in the file.

Can anyone help ?

I have the following so far but im lost

FILENAME=MyWords
NUMBEROFWORDS=`cat $FILENAME | cut -d : -f | wc -w`
echo "$MAXWORDS"

Now i would like to know how many words they are in that file (all the words are on one line separated by a comma delimiter).

Last edited by baks; 03-13-2007 at 12:24 PM.
 
Old 03-13-2007, 01:02 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

I think 'awk' is the weapon of choice here.
Code:
cat "$FILENAME" | awk -F ',' '{print NF}'
should give you the number of words. 'NF' is the Number of Fields and the -F argument is the delimiter.

Dave
 
Old 03-13-2007, 01:16 PM   #3
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
ilikejam's response was good. Three comments.

First, ilikejam's response can be shortened to

Code:
awk -F ',' '{print NF}' $FILENAME
I think somewhere on the Internet there's a file containing 99 unnecessary uses of cat, and this would be one of them.

Second, your originally posed problem hinted in one place that the delimiter was colon, and in another place that it was comma. If it's really colon, then do this instead:

Code:
awk -F ':' '{print NF}' $FILENAME
Third, in your original example, you placed the word count in NUMBEROFWORDS, but when you displayed the result, you displayed MAXWORDS instead

Hope this helps.
 
Old 03-13-2007, 01:30 PM   #4
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ilikejam
Hi.

I think 'awk' is the weapon of choice here.
Code:
cat "$FILENAME" | awk -F ',' '{print NF}'
should give you the number of words. 'NF' is the Number of Fields and the -F argument is the delimiter.

Dave
I tried the following and it comes up with an error

FILENAME=MYWORDS
NUMBEROFWORDS=`cat "$FILENAME" | awk -F ':' '(print NF)'`
 
Old 03-13-2007, 01:33 PM   #5
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by wjevans_7d1@yahoo.co
ilikejam's response was good. Three comments.

First, ilikejam's response can be shortened to

Code:
awk -F ',' '{print NF}' $FILENAME
I think somewhere on the Internet there's a file containing 99 unnecessary uses of cat, and this would be one of them.

Second, your originally posed problem hinted in one place that the delimiter was colon, and in another place that it was comma. If it's really colon, then do this instead:

Code:
awk -F ':' '{print NF}' $FILENAME
Third, in your original example, you placed the word count in NUMBEROFWORDS, but when you displayed the result, you displayed MAXWORDS instead

Hope this helps.

Hi thanks for the reply, I tried the following and it comes up with an error:


FILENAME=MYWORDS
NUMBEROFWORDS=`awk -F ':' '(print NF)' $FILENAME`
echo "$NUMBEROFWORDS"



I also did the following but the answer keeps coming up as 1 even though I have 10 words separated by colons.


FILENAME=MYWORDS
NUMBEROFWORDS=`cat "$FILENAME" | awk -F ':' '{print NF}' | wc -w`
echo "$NUMBEROFWORDS"

Last edited by baks; 03-13-2007 at 01:38 PM.
 
Old 03-13-2007, 02:05 PM   #6
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
I did the following and it worked:

FILENAME=MYWORDS

NUMBEROFWORDS=`awk -F ':' '{print NF}' $FILENAME`

Thank you VERY VERY much.
 
Old 03-13-2007, 04:56 PM   #7
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by baks
Hi, I have a problem and was wondering whether anyone can help ?

I have a file with a list of words all on one line separated by a delimiter (colon/ and at the moment I jusr assign 10 (the number of words in the file) to a variable but I need to be able to do this at runtime so that a piece of code would automatically be able to find out how many words are in the file.

Can anyone help ?

I have the following so far but im lost

FILENAME=MyWords
NUMBEROFWORDS=`cat $FILENAME | cut -d : -f | wc -w`
echo "$MAXWORDS"

Now i would like to know how many words they are in that file (all the words are on one line separated by a comma delimiter).
You don't need any external command:

Code:
read line < FILENAME
IFS=:
set -f
set -- $line
NUMBEROFWORDS=$#
 
Old 03-13-2007, 04:58 PM   #8
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Hi, would anybody know how to determine whether a character entered is a letter or not.

For example:

echo "please enter a letter"
read LETTER
if [ $LETTER != ???]

I dont know what to put in there.

Any help would be appreciated.
 
Old 03-13-2007, 05:14 PM   #9
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by baks
Hi, would anybody know how to determine whether a character entered is a letter or not.

For example:

echo "please enter a letter"
read LETTER
if [ $LETTER != ???]

I dont know what to put in there.
Use case, not if:

Code:
case $LETTER in
   [A-Za-z]) ## LETTER contains a letter
            : do something here
            ;;
   *) ## Not a letter
      : do something else
       ;;
esac
If you need to consider non-ASCII letters (e.g., with accents), use [[:alpha:]] instead of [A-Za-z].
 
Old 03-13-2007, 07:22 PM   #10
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Thank you thank you thank you Ive managed it but I foresee many problems so I will be back.

Thanks a lot to everyone.
 
Old 03-13-2007, 08:09 PM   #11
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Referring to post number 9 is they a way of using an IF statement rather than a case statement ?

if [ $LETTERCHECK = [A-Za-z] ]
then
else
PLAYER=`expr $PLAYER +1`
fi

But the above code does not work
 
Old 03-13-2007, 08:28 PM   #12
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by baks
Referring to post number 9 is they a way of using an IF statement rather than a case statement ?

if [ $LETTERCHECK = [A-Za-z] ]
then
else
PLAYER=`expr $PLAYER +1`
All POSIX shells can do arithmetic without using an external command:

PLAYER=$(( $PLAYER + 1 ))

Quote:
fi

But the above code does not work
There is, but it uses non-standard syntax, [[ ... ]].

What's wrong with using case? It is efficient, and it is portable across all Bourne-type shells.
 
Old 03-13-2007, 08:36 PM   #13
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
I used the following but it keeps stating a syntax error:


if [ $LETTERCHECK = [[A-Za-z]] ]
then
echo ""
else
PLAYER= $(($PLAYER+1))
fi

I need to use the IF statement as its part of a project using different case and if statements and for this part I must use an IF statement.

Thank you
 
Old 03-13-2007, 09:30 PM   #14
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by baks
I used the following but it keeps stating a syntax error:


if [ $LETTERCHECK = [[A-Za-z]] ]
then
echo ""
else
PLAYER= $(($PLAYER+1))
fi

I need to use the IF statement as its part of a project using different case and if statements and for this part I must use an IF statement.
That sounds like homework.
Code:
if [[ $LETTER = /[a-zA-Z]/ ]]
then
  echo y
else
  echo n
fi
You should read the documentation for your shell.
 
Old 03-13-2007, 09:52 PM   #15
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Thank you very much, its not homework but like a side project for work.

Thanks
 
  


Reply



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
Setting Bash Screen Scrollback Length? (Shift + Page Up) juanbobo Linux - Software 1 06-14-2006 10:01 AM
Zero-length bash .history file! revmyo Linux - Security 1 12-03-2005 08:09 PM
Bash scripting: column-ize file of varying length strings Quantum0726 Programming 4 08-13-2005 06:19 PM
csv to fixed-length file roballen Programming 0 03-11-2004 03:12 AM
Can you create an empty file and specify the byte length? harryo Linux - General 3 02-25-2004 10:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:54 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
Open Source Consulting | Domain Registration