Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-11-2012
Registered User
 
Join Date: Sep 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Help with the For loop shell script

Hi,

I have multiple files in a directory. Each file will have a header.I have to check if any of the files has 0 rows other than the header then I have to delete the files.
Here “ Empty file” in my case means a file has header information but no data. I have to delete such files.
If the file count =1 then delete the files else cp the files from one location to another.

Code:
#!/bin/bash
cd filepath
rc=`awk '{n++} END {print n}' file1.csv`
if [ $rc -ne 1 ]; then
`cp /file1.csv file1path/file2.csv`
exit $rc 
else 
`rm -rf file1.csv`    
fi

The above script is good for checking one file but I couldn't check multiple files.

Moderator's Comments:
Please use code tags for code and data

Last edited by Scrutinizer; 12-11-2012 at 04:10 AM.. Reason: Removed font codes, added code tags
Sponsored Links
    #2  
Old 12-11-2012
spacebar's Avatar
Registered User
 
Join Date: Oct 2009
Location: spaceBAR Central
Posts: 298
Thanks: 0
Thanked 58 Times in 58 Posts
Try this:

Code:
#!/bin/bash
cd filepath
for x in `ls -1 *.csv`
do
  line_count=`wc -l $x`
  if [[ $line_count -gt 1 ]] then
    cp $x target_path/$x
  else
    rm -rf $x
  fi
done

Sponsored Links
    #3  
Old 12-11-2012
Registered User
 
Join Date: Sep 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks for the script. It worked!
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with IF statement with loop Shell Script zero3ree Shell Programming and Scripting 4 09-25-2010 07:05 PM
Shell script using loop alisha Shell Programming and Scripting 23 07-20-2010 05:39 AM
Loop in shell script vin_pll Shell Programming and Scripting 3 11-17-2009 03:44 AM
Help with loop in a shell script elifchen Shell Programming and Scripting 2 09-24-2009 09:48 AM
If then else loop in Shell script pankajkrmishra Shell Programming and Scripting 4 07-31-2006 09:40 AM



All times are GMT -4. The time now is 09:03 AM.