|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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`
fiThe above script is good for checking one file but I couldn't check multiple files.
Last edited by Scrutinizer; 12-11-2012 at 04:10 AM.. Reason: Removed font codes, added code tags |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
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
|
|||
|
|||
|
Thanks for the script. It worked!
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|