Solved

Looping through directory for file and do a word count

Posted on 2009-05-04
5
687 Views
Last Modified: 2013-12-26
Hello All.

I am working on a script which basically loop through a directory for files and does a word count for each file and if the file has more than 1 row then writes that filename to a log file.

1. Loop thorugh a directory and pick up files
2. do a word count on each file
3. write the filename to a logfile only if the file has more than 1 record.

I have a written some code and it works but it is writing all files to logfile
for file in `ls`

do

 Count=`echo cat $file |  wc -l`

echo " my count is $Count"

if [ "$count" -gt 1 ];

then

echo $count

echo $file >> /home/log.txt

else

 echo "there is no file to write"

fi

done

Open in new window

0
Comment
Question by:antvu
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:mostart
ID: 24295835
watch your case. $Count is initialized with capital C and later you are using lower case c..
0
 

Author Comment

by:antvu
ID: 24295936
I  changed the case with upper case C and executed the script.. below is the result i got
my count is 1
there is no file to write
 my count is 1
there is no file to write
 my count is 1
there is no file to write
 my count is 1
there is no file to write
___________________________

I have 4 files in the directory , one of the files has 1 record and other 3 have more than 1 ..
0
 
LVL 8

Accepted Solution

by:
mostart earned 50 total points
ID: 24295967
also remove the "echo" in line 3.
0
 

Author Comment

by:antvu
ID: 24295991
Thanks mostart.. that worked.
I am closing the question now.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 24299015
Let's remove redundant code and shorten
#!/bin/bash

for file in *

do

  [ -f $file ] || continue  # Skip non-files

  lines=$(wc -l <$file)

  [ $lines -gt 1 ] && echo $file >>/home/log.txt

done

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now