Assistance to use ls and GREP

Hi

We have several folders and subfolders in a heirarchy, representing clients and files we send to them. Files that are not processed are placed in reject folder. I am able to run a LS that will scan all these folders, and ones that show entries (rejected files) are seen here. I am looking for a way ( | grep perhaps) that will only show the folder structures that have contents.

Here is my LS command:

ls -l BU/AD_*/ANTS*/SERVER/REJECT 
I then get a list (there are like 200 clients) that looks like this:
 
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
BU/BACAR/TASRO0034/SERVER/REJECT:
total 0
 
BU/BACAR/TASRO0048/SERVER/REJECT:
total 0
 
BU/BACAR/TASRO0109/SERVER/REJECT:
total 0
 
BU/BACAR/TASRO0110/SERVER/REJECT:
total 0
 
BU/BACAR/TASRO0111/SERVER/REJECT:
total 0
 
BU/BAFINANCE/TASRO0005/SERVER/REJECT:
total 0
 
BU/BAFINANCE/TASRO0006/SERVER/REJECT:
total 0
 
BU/BAFINANCE/TASRO0038/SERVER/REJECT:
total 0
 
BU/BAFINANCE/TASRO0050/SERVER/REJECT:
total 0
 
BU/BAFINANCE/TASRO0051/SERVER/REJECT:
total 96
-rw-r-----    1 xxxxxx xxxxxx          201 Aug 02 12:55 
testfile.asdasd 20110802_125518
-rw-r-----    1 xxxxxx xxxxxx         201 Aug 20 03:06 testfile.asdasd.20110820_030619
-rw-r-----    1 xxxxxx xxxxxx          5628 Aug 02 12:55 aircancr.points.20110802_125538
-rw-r-----    1 xxxxxx xxxxxx         3819 Aug 19 01:44 testfile.asdasd.20110819_014423
 
<<<<<<<<<<<<<<<<<
<<<<<<<<<<<<<<<<<
 

So i am looking for a way (| GREP or other at the end of the ls) to omit the folders that have no entries.[/FONT]

I have attempted to do

ls -l BU/AD_*/ANTS*/SERVER/REJECT | grep "!TOTAL 0" 

but this doesnt work

Thanks for your time

your "total x" result isnt in thesame line of your file path thats why you cant use grep -v to exclude lines with "total 0" in it.

so you need to grep "total 0" after your path line.

so if you assume that you have the result in file A , create a script:

for i in $(grep -n BU A);do
line="`echo $i|cut -d":" -f1`"
let line+=1
if [ "`cat 1 |head -$line|tail -1`" != "total 0" ];then
echo $i|cut -d":" -f2
fi
done
1 Like

sed is easier to perform multi-line matching.

sed  '/^.*:$/{N;N;/\ntotal 0\n$/d;}'
1 Like

I am a little new to this.

I have tried the first part of the script and substituted my full path (
BU/AD_*/ANTS*/SERVER/REJECT) to where you typed BU. it looks like this:

for i in $(grep -n grep -n BU/AD_*/ANTS*/SERVER/REJECT A);do
  line="`echo $i|cut -d":" -f1`"
  let line+=1
  if [ "`cat 1 |head -$line|tail -1`" != "total 0" ];then
    echo $i|cut -d":" -f2
  fi
done

i then made a .sh file and ran bash (filename). it geve me errors like this:

bash greprejects.sh
bash greprejects.sh
greprejects.sh: line 1: {rtf1ansiansicpg1252deff0deflang1033{fonttbl{f0fswissfcharset0: command not found
greprejects.sh: line 1: }{f1fromanfprq2fcharset0: command not found
greprejects.sh: line 1: }{f2fromanfcharset0: command not found
greprejects.sh: line 1: }{f3fswissfcharset0: command not found
greprejects.sh: line 1: }}: command not found
greprejects.sh: line 2: {*generator: command not found

Any idea what is the matter?

I would conversely try the second one, but I am not sure where in the script to put the sed line.

Thanks
JAMIE

Simply pipe the output of "ls" to "sed". That is:

ls -l BU/AD_*/ANTS*/SERVER/REJECT | sed '/^.*:$/{N;N;/\ntotal 0\n$/d;}'
1 Like

I tested this version and it works:

sed '/^BU/{N;N;/total 0/d;}'

However MacMonster solution maybe much better than this but i'll explain this again,
you'll need to create and run script.sh:

ls -l BU/AD_*/ANTS*/SERVER/REJECT > A
for i in $(grep -n "^BU" A);do
  line="`echo $i|cut -d":" -f1`"
  let line+=1
  if [ "`cat A |head -$line|tail -1`" != "total 0" ];then
    echo $i|cut -d":" -f2
  fi
done
rm -f A

i must thank MacMonster for his help. I

I consider this matter closed. Iam not sure how to 'close; the thread, but I am satisfied.