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?
Shell Scripting
Started by
Guest_NeedHelp_*
, Apr 05 2007 12:34 PM
9 replies to this topic
#1
Guest_NeedHelp_*
Posted 05 April 2007 - 12:34 PM
Guest_NeedHelp_*
|
|
|
#2
Guest_NeedHelp_*
Posted 10 April 2007 - 08:17 AM
Guest_NeedHelp_*
Bump, can anyone help?
#3
Posted 15 April 2007 - 01:05 AM
NeedHelp said:
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?
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?
awk -v RS="gggg" 'END{print (NR?NR-1:0)}' file #gggg is the search string
#4
Guest_Jordan_*
Posted 15 April 2007 - 09:04 AM
Guest_Jordan_*
You can also use grep for that or WC. Are you counting lines or characters?
So just do a normal shell if statement on a variable that contains those values.
grep -c // Line count wc -l // Line Count
So just do a normal shell if statement on a variable that contains those values.
#5
Posted 16 April 2007 - 04:45 AM
Jordan said:
You can also use grep for that or WC. Are you counting lines or characters?
So just do a normal shell if statement on a variable that contains those values.
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.
#6
Guest_Jordan_*
Posted 16 April 2007 - 06:21 AM
Guest_Jordan_*
grep -c will only count lines, hence the "// Line Count" comment.You can change the -l of wc to "wc -w" for word count.
#7
Posted 16 April 2007 - 06:30 AM
wc -w counts total words in a file. it will not work if OP only wants to count a certain word in a file.
#8
Guest_Jordan_*
Posted 16 April 2007 - 07:01 AM
Guest_Jordan_*
Sorry, I meant to type -m
-m, --chars print the character counts
-m, --chars print the character counts
#9
Posted 16 April 2007 - 07:41 AM
for example this sample file:
# more file this is a test line this is second line # wc -m file 40 file # wc -c file 40 filebut if OP wants to count how many times the word "line" occurs, both the above will not work, whether -m or -c. however:
# awk -v RS="line" 'END{print (NR?NR-1:0)}' file
2
#10
Guest_NeedHelp_*
Posted 16 April 2007 - 08:04 AM
Guest_NeedHelp_*
I needed the line count. I was able to accomplish it doing:
COUNT=`cat $1 | grep -c rtn` if [ "$COUNT" -eq 1 ]; then .....


Sign In
Create Account

Back to top










