Word Count error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Word Count error
# 1  
Old 06-17-2014
Word Count error

I need to read a trigger file whose name can be:
ABC!DEF@2014.txt
or
ABC!DEF@2014,2015.txt

and then carry out functions on those inputs.

Currently I am doing:
Code:
YEAREXPORT {

FILE= xyz.txt

ls ABC* -l > ${FILE}
	
if [ ! -f ${FILE} ]; then
		log_err "Trigger File ABC* does not exist!"
fi
	

while read LINE
do
	 log_msg "Read line ${LINE}"
	
	#Check if Two years have entered
	COUNTCOMMA =$(echo "${LINE}"  | grep -o "," | wc -l)	
	
	if [${COUNTCOMMA} == 0]; then
	      log_msg "Its zero"
	else 
	      log_msg "Its not zero"
	fi

done < ${FILE}
}

Now while the Read line shows up in the log. The countcomma part is not executing, I just get this:
Code:
Read line -rwx------+ 1 piruser Domain Users 0 2014-06-13 19:33 ABC!DEF@2014,2015.txt
YearExport:21: 1 not found

What am I doing incorrectly here?
# 2  
Old 06-17-2014
change these and re-try
Code:
COUNTCOMMA =$(echo...... --> COUNTCOMMA=$(echo........

Code:
if [${COUNTCOMMA} == 0]; --> if [ ${COUNTCOMMA} = 0 ];

# 3  
Old 06-17-2014
Quote:
Originally Posted by ygemici
change these and re-try
Code:
COUNTCOMMA =$(echo...... --> COUNTCOMMA=$(echo........

Code:
if [${COUNTCOMMA} == 0]; --> if [ ${COUNTCOMMA} = 0 ];

I changed my code to:
Code:
YEAREXPORT {

FILE= xyz.txt

ls ABC* -l > ${FILE}
	
if [ ! -f ${FILE} ]; then
		log_err "Trigger File ABC* does not exist!"
fi
	

while read LINE
do
	 log_msg "Read line ${LINE}"
	
	#Check if Two years have entered
	COUNTCOMMA=`echo "${LINE}"  | grep -o "," | wc -l)`
	
	if [${COUNTCOMMA}=0]; then
	      log_msg "Its zero"
	else 
	      log_msg "Its not zero"
	fi

done < ${FILE}
}

So while the COUNTCOMMA now works, I keep getting bad pattern error for the if condition.
# 4  
Old 06-17-2014
what is your shell and os ?
and can u write full code ?

your code must be like these
Code:
YEAREXPORT {

FILE= xyz.txt

ls ABC* -l > ${FILE}
	
if [ ! -f ${FILE} ]; then
		log_err "Trigger File ABC* does not exist!"
fi
	

while read LINE
do
	 log_msg "Read line ${LINE}"
	
	#Check if Two years have entered
	COUNTCOMMA=`echo "${LINE}"  | grep -o "," | wc -l`
	
	if [ ${COUNTCOMMA}=0 ]; then
	      log_msg "Its zero"
	else 
	      log_msg "Its not zero"
	fi

done < ${FILE}
}

# 5  
Old 06-17-2014
Additionally, just to highlight :
Code:
COUNTCOMMA=`echo "${LINE}"  | grep -o "," | wc -l)`

Opening parenthesis missing. Also there is no need to put those unless you have used $(...) but in that case you dont need `...`

Code:
if [${COUNTCOMMA}=0]; then

You have not put spaces around the opening and closing square bracket as ygemici suggested. Also, Make sure put spaces around = or == whatever you use.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. UNIX for Dummies Questions & Answers

word count with grep

Hi, It is very interesting to learn the unix, i just struck with a doubt like i have below content in my file xyz xyz xyz xyz i just want know the word count by using grep -wc 'xyz' <filename>, but it is giving 3 instead of 4.So i understood that it is showing matched line numbers count... (2 Replies)
Discussion started by: vmachava
2 Replies

3. Shell Programming and Scripting

if, word count

Hi, I need to count the lines of a file stack.html and if the amount lines i want to do something. At this moment, I have if ; then ... This is not working. Any ideas? Thanks! (3 Replies)
Discussion started by: azertyazerty
3 Replies

4. UNIX for Advanced & Expert Users

Word count

Script that lists all words used in one or more files and displays their count (pattern /\W+/ to split the lines of the input file into words can b used).. It should display list in format word:count...gets Filename as an cmd line argument! eg: $perl test doc (where doc is d file we are going to... (4 Replies)
Discussion started by: aadi_uni
4 Replies

5. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

6. Fedora

word count wc

could someone explain this please. echo aaaa|wc -c 5 echo aaaa|wc -m 5 But I'd expect the count to be 4 Its SunOS 5.8 Thanks in Advance. (5 Replies)
Discussion started by: chaandana
5 Replies

7. UNIX for Dummies Questions & Answers

Word Count

Hi All, Please let me know how to get the count of a particular word in a file. Example. I am looking for count of word 'result' in a file abc.xml. Thanks, Shankar (10 Replies)
Discussion started by: s_chowhan
10 Replies

8. Shell Programming and Scripting

specified word count

hi iam trying to do a specified word count on file called text i have a few ideas but don't get the result i want do any one have a idea please help i have this at the moment cat text echo "Please enter the word you are looking for:" read string echo "the word < $string > occurs in... (5 Replies)
Discussion started by: bhaviknp
5 Replies

9. UNIX for Dummies Questions & Answers

Word count problem

I have a text file that has 5719 rows when I open it up in a text editor. When I do a wc -l in Unix however, it says that I have 5718 rows. What could be causing this difference? (1 Reply)
Discussion started by: ssmith001
1 Replies

10. UNIX for Dummies Questions & Answers

count word

hi, given a file i need to get the first line and secodn line and count each of the line whether the length of first line and second line is the same i don;t know how to get the length of the line...seems like use 'wc' cannot do it... please advice (1 Reply)
Discussion started by: ariuscy
1 Replies
Login or Register to Ask a Question