Lost Password?


Go Back   CodeCall Programming Forum > Software Development > General Programming

General Programming Non language specific, Assembly, Linux/Unix, Mac and anything not covered in other topics. Talk about Programming Theory here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-05-2007, 04:34 PM
NeedHelp NeedHelp is offline
Programming God
 
Join Date: May 2006
Posts: 527
Rep Power: 13
NeedHelp is on a distinguished road
Default Shell Scripting

I need to do an IF statement and if there is a count of two do one thing and a count of one do another. My question is, is there a count function? My function basically looks like this:

cat filename | grep ..... | {print 1}

If there are two results in I need the 2nd one, otherwise the first one. Any ideas?
__________________
I Need Help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-10-2007, 12:17 PM
NeedHelp NeedHelp is offline
Programming God
 
Join Date: May 2006
Posts: 527
Rep Power: 13
NeedHelp is on a distinguished road
Default

Bump, can anyone help?
__________________
I Need Help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-15-2007, 05:05 AM
ghostdog74 ghostdog74 is offline
Newbie
 
Join Date: Apr 2007
Posts: 6
Rep Power: 0
ghostdog74 is on a distinguished road
Default

Quote:
Originally Posted by NeedHelp View Post
I need to do an IF statement and if there is a count of two do one thing and a count of one do another. My question is, is there a count function? My function basically looks like this:

cat filename | grep ..... | {print 1}

If there are two results in I need the 2nd one, otherwise the first one. Any ideas?
if i understand you, you wanted to count a word occurence in a file?
Code:
awk -v RS="gggg" 'END{print (NR?NR-1:0)}' file #gggg is the search string
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-15-2007, 01:04 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,224
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

You can also use grep for that or WC. Are you counting lines or characters?

Code:
grep -c // Line count
wc -l // Line Count
So just do a normal shell if statement on a variable that contains those values.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-16-2007, 08:45 AM
ghostdog74 ghostdog74 is offline
Newbie
 
Join Date: Apr 2007
Posts: 6
Rep Power: 0
ghostdog74 is on a distinguished road
Default

Quote:
Originally Posted by Jordan View Post
You can also use grep for that or WC. Are you counting lines or characters?

Code:
grep -c // Line count
wc -l // Line Count
So just do a normal shell if statement on a variable that contains those values.
depending on OP's requirement, if he is counting words, grep -c would not be accurate if 2 or more words occur on a line. grep -c will count as 1, because it returns matching number of lines which the word is found.also wc -l counts number of lines, not words.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 04-16-2007, 10:21 AM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,224
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

grep -c will only count lines, hence the "// Line Count" comment.You can change the -l of wc to "wc -w" for word count.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-16-2007, 10:30 AM
ghostdog74 ghostdog74 is offline
Newbie
 
Join Date: Apr 2007
Posts: 6
Rep Power: 0
ghostdog74 is on a distinguished road
Default

wc -w counts total words in a file. it will not work if OP only wants to count a certain word in a file.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-16-2007, 11:01 AM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,224
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

Sorry, I meant to type -m

-m, --chars print the character counts
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-16-2007, 11:41 AM
ghostdog74 ghostdog74 is offline
Newbie
 
Join Date: Apr 2007
Posts: 6
Rep Power: 0
ghostdog74 is on a distinguished road
Default

for example this sample file:
Code:
# more file
this is a test line
this is second line
# wc -m file
40 file
# wc -c file
40 file
but if OP wants to count how many times the word "line" occurs, both the above will not work, whether -m or -c. however:
Code:
# awk -v RS="line" 'END{print (NR?NR-1:0)}' file
2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-16-2007, 12:04 PM
NeedHelp NeedHelp is offline
Programming God
 
Join Date: May 2006
Posts: 527
Rep Power: 13
NeedHelp is on a distinguished road
Default

I needed the line count. I was able to accomplish it doing:

Code:
COUNT=`cat $1 | grep -c  rtn`

if [ "$COUNT" -eq 1 ]; then
.....
__________________
I Need Help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[PHP] Faking Shell Access Through PHP pranky PHP Tutorials 2 03-29-2007 07:28 AM
Shell Scripting Problem Chan General Programming 3 03-22-2007 12:59 PM
Learning Shell Script Cosmet General Programming 0 11-14-2006 09:16 AM
Shell: MySQL Database Backup Jordan Tutorials 0 08-15-2006 04:18 PM
Shell Programming Void General Programming 3 07-08-2006 12:47 PM


All times are GMT -5. The time now is 01:40 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 98%

Ads