|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Writing echo and cat on the same line...
Hi,
At the moment, I have the following statements which show like this, but I want it all on one line. How can I do this? Thanks. ---------------------------- echo "There are" cat menu | wc -l echo "characters in the text" ---------------------------- There are 40 characters ---------------------------- |
|
#2
|
||||
|
||||
|
Code:
echo "There are" && cat menu | wc -l && echo "characters in the text" OR echo "There are" ; cat menu | wc -l ; echo "characters in the text" FYI, wc -l will list the number of lines in a text files, not the number of characters. wc -m will count characters. |
|
#3
|
|||
|
|||
|
More than one way of skinning a cat ...
Code:
echo "There are `wc -l menu` lines in the text" Of course, make sure that your current working directory has that file in it first! |
|
#4
|
|||
|
|||
|
i'm trying to get the user to enter a character, then the script should search for how many of that character exists in the file. I have the following code, but it doesn't work properly and it shows the wrong amount. For example, I want it to say, "There are 7 'e' characters in the text", or "There are 5 'n' characters in the text".
Thanks for your help. echo echo "Enter the Character to Search for:" read char grep -c $char text echo echo "Press any key to continue" read key |
|
#5
|
||||
|
||||
|
grep will only tell you how many lines in the file(s) contain the match string - not how many times in total.
A couple of 'points of style' ... your prompt for input, might be nicer if it was: echo "Enter the Character to Search for: \c" that will not send a newline/carriage return after the text. Also, you may want to consider some what would happen in the case of the user entering something you did not expect (if you see what I mean!) - such as a hyphen or other character (or, indeed string of characters that the shell would be happy to interpret for you). What you actually need is something that scans a string-length at a time for the entered search-string, totting up any matches. A very, very lateral way of doing this might be to count all the characters in the file with a before=`wc -m menu` command, and then to do something like: after=`cat menu | tr -d $char` | wc -m which would, if I am half-right, count the number of characters in the file with all the specified characters removed. Simple maths would then find out the number there were! ![]() Last edited by Ehlanna : March 15th, 2006 at 04:34 PM. Reason: syntax error correction! |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Writing echo and cat on the same line... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|