|
||||||
| General Programming Non language specific, Assembly, Linux/Unix, Mac and anything not covered in other topics. Talk about Programming Theory here. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 |
| Sponsored Links |
|
|
|
|||
|
Quote:
Code:
awk -v RS="gggg" 'END{print (NR?NR-1:0)}' file #gggg is the search string
|
|
|||||
|
You can also use grep for that or WC. Are you counting lines or characters?
Code:
grep -c // Line count wc -l // Line 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! |
|
|||
|
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.
|
| Sponsored Links |
|
|
|
|||||
|
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! |
|
|||||
|
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! |
|
|||
|
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 Code:
# awk -v RS="line" 'END{print (NR?NR-1:0)}' file
2
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| 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 |
Goal: 100,000 Posts
Complete: 98%