Count Command Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count Command Help
# 1  
Old 09-25-2014
Count Command Help

Hi Everyone,

I'm new to unix scripting. Hoping you guys can help me out. I am trying to create a script that will do a count command for each file (in this example *.dat) within a predefined directory and display only the filename without the full path.

This is what I got so far, but it doesn't exactly work the way I want it to.

Code:
#!/bin/sh

for X in /data/test/input/*.dat; do
echo `basename $X` >> dat_results.out
echo "wc -l $X" >> dat_results.out

done

Any input would be greatly appreciated.

Thanks,

Last edited by Scrutinizer; 09-25-2014 at 09:09 PM.. Reason: code tags
# 2  
Old 09-25-2014
Is this a homework assignment?
# 3  
Old 09-25-2014
Quote:
Originally Posted by Don Cragun
Is this a homework assignment?
No, this is not. It is voluntarily work related and a learning experience. I don't want to manually run the command for each file.
# 4  
Old 09-25-2014
On the 3rd line there are back ticks (command substitution), whereas on the 4th line there are double quotes, so changing that should fix your problem..

In addition you do not actually need them here since instead of
Code:
echo 'command' >> file

you could use
Code:
command >> file

If you ever need command substitution it would be preferable to use $( ... ) instead of ` ... ` unless your shell is the classic Bourne shell rather than a Posix Shell.

Also, you can do a file redirect for the entire loop, so instead of
Code:
rm somefile
for ...
do
   command1 >> somefile
   command2 >> somefile
done

you can use:
Code:
for ...
do
   command1 
   command2
done > somefile


Last edited by Scrutinizer; 09-25-2014 at 09:28 PM..
# 5  
Old 09-25-2014
Code:
for X in /data/test/input/*.dat
do
basename $X
wc -l $X
done > dat_results.out

give me this result

Code:
test1.dat
7 /data/test/input/test1.dat
test2.dat
7 /data/test/input/test2.dat
test3.dat
7 /data/test/input/test3.dat

I want the result to look this
Code:
test1.dat
7


Thanks

Last edited by kadawtomtom978; 09-25-2014 at 09:46 PM..
# 6  
Old 09-25-2014
Try
Code:
wc -l < "$X"

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 09-25-2014
Quote:
Originally Posted by Scrutinizer
Try
Code:
wc -l < "$X"

Lols, that was a simple fix. Thanks a bunch!Smilie

Also, like I mentioned earlier. I'm very new to this. How did your code fix my issue?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get count of multiple word in single command

Hello Experts, I have a log file that contains 4 different type of exception : 1- Exception 2- Fatal 3- Error 4- Exec My requirement is to find count of each type of exception, i tried using combination of -E and -C but that doesn't seems to be working : grep -ec 'Exception' -ec... (4 Replies)
Discussion started by: mukulverma2408
4 Replies

2. Shell Programming and Scripting

Count aggregated command output using wc -l

Hi all, I need to run a bunch of commands seperated by ; but then count the total lines of output. Pipes and redirection don't seem to be helping me. Anyone know how I can do this properly: ls /usr/bin/nslookup ; ls /usr/sbin/lsof ; ls /sbin/ifconfig ; ls /usr/sbin/dmidecode ; ls... (2 Replies)
Discussion started by: dan-e
2 Replies

3. Shell Programming and Scripting

Grep command to count characters

I have a file that I need to count the number of "Y"s in the file, but just the "Y"s that fall between certain columns. Like: file1: 1234567890123456789012345 BBBBBBYBBBBYBBYBBYBBYBBB I want to count the 'Y's between 17 and 21 = 2 "Y"s Thanks (12 Replies)
Discussion started by: jbt828
12 Replies

4. UNIX for Dummies Questions & Answers

count and content in the same command

Hi, I have small query and a understanding, could you please clarify the query and correct my understanding..? I am very big log file If i need to take a count and contained lines for a particular pattern/word i am using the grep command twice like the below. grep -c "OutOfMemoryException"... (4 Replies)
Discussion started by: senthilkumar_ak
4 Replies

5. UNIX for Dummies Questions & Answers

Word Count (WC) command

I have created a file "pat".This file contains the text "abcdefghi". Now i want to count the characters in this file "pat". I gave wc -c | pat and it returns me exact count "9". Now when i tried like "echo pat | wc -m" It returns '4'. as for as i understand this command "echo pat | wc -m" should... (7 Replies)
Discussion started by: mdali_vl
7 Replies

6. UNIX for Dummies Questions & Answers

Record count problem using sed command

Hi, I have a script which removes 2 header records and 1 trailer record in a list of files. The commands doing the actions are sed '1,2d' $file > tempfile1.dat sed '$d' < tempfile1.dat > $output.txt Its working fine for all records except a file having size=1445509814 and number of... (2 Replies)
Discussion started by: ayanbiswas
2 Replies

7. UNIX for Dummies Questions & Answers

Problem count with wc-l command

Hi All, I'm trying to count how many line in my *.txt file. My *.txt file has 1937 lines. The problem is : when I use wc -l command the result is 1936. Again, I did some test. I create a new file with 100 lines with text editor (Notepad ++, Ultra edit). And when I count it again with wc... (2 Replies)
Discussion started by: kunimi
2 Replies

8. UNIX for Dummies Questions & Answers

command to get count in unzipped folder

Hi All, Is there is a way to do this in a single unix command I have unzipped some files into a directory .. I want to check the count of the files in the directory and also i need to check whether the files is having read permision or not.. I know it can be done by a script isthere... (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

9. Shell Programming and Scripting

byte count and cut command

1. Is there a way to count the number of bytes of a variable? example: abc@yahoo.com is 13 bytes 2. Cut command only allows one byte for delimiter example: cut -f1 -d'.' delimited by period. Is there a way to have two or more characters in the delimiter field? thanks in adavance. :) (4 Replies)
Discussion started by: hemangjani
4 Replies

10. Shell Programming and Scripting

command to count files in dir

hi plz let me know the command to count the files in directory. thanks in advance -Bali Reddy (4 Replies)
Discussion started by: balireddy_77
4 Replies
Login or Register to Ask a Question