UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 15th, 2006, 06:49 AM
kev269 kev269 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 37 kev269 User rank is Private First Class (20 - 50 Reputation Level)kev269 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 34 m 51 sec
Reputation Power: 4
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
----------------------------

Reply With Quote
  #2  
Old March 15th, 2006, 07:47 AM
stanleypane's Avatar
stanleypane stanleypane is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Baltimore, MD
Posts: 208 stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 7 h 4 m 19 sec
Reputation Power: 21
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.

Reply With Quote
  #3  
Old March 15th, 2006, 08:51 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 712 SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 22 h 13 m 34 sec
Reputation Power: 315
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!

Reply With Quote
  #4  
Old March 15th, 2006, 01:16 PM
kev269 kev269 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 37 kev269 User rank is Private First Class (20 - 50 Reputation Level)kev269 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 34 m 51 sec
Reputation Power: 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

Reply With Quote
  #5  
Old March 15th, 2006, 04:32 PM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 814 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 10 m 43 sec
Reputation Power: 243
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Writing echo and cat on the same line...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway