LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   BASH length of file (http://www.linuxquestions.org/questions/programming-9/bash-length-of-file-537109/)

baks 03-13-2007 11:39 AM

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).

ilikejam 03-13-2007 01:02 PM

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

wjevans_7d1@yahoo.co 03-13-2007 01:16 PM

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.

baks 03-13-2007 01:30 PM

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)'`

baks 03-13-2007 01:33 PM

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"

baks 03-13-2007 02:05 PM

I did the following and it worked:

FILENAME=MYWORDS

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

Thank you VERY VERY much.

cfaj 03-13-2007 04:56 PM

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=$#


baks 03-13-2007 04:58 PM

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.

cfaj 03-13-2007 05:14 PM

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].

baks 03-13-2007 07:22 PM

Thank you thank you thank you Ive managed it but I foresee many problems so I will be back.

Thanks a lot to everyone.

baks 03-13-2007 08:09 PM

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

cfaj 03-13-2007 08:28 PM

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.

baks 03-13-2007 08:36 PM

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

cfaj 03-13-2007 09:30 PM

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.

baks 03-13-2007 09:52 PM

Thank you very much, its not homework but like a side project for work.

Thanks


All times are GMT -5. The time now is 05:09 AM.