Assistance to use ls and GREP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assistance to use ls and GREP
# 1  
Old 09-27-2011
[solved] 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:
Code:
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.

I have attempted to do
Code:
ls -l BU/AD_*/ANTS*/SERVER/REJECT | grep "!TOTAL 0"

but this doesnt work


Thanks for your time

Last edited by vbe; 09-27-2011 at 12:21 PM.. Reason: please use code tags next time
# 2  
Old 09-27-2011
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:

Code:
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

This User Gave Thanks to ieth0 For This Post:
# 3  
Old 09-27-2011
sed is easier to perform multi-line matching.

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

This User Gave Thanks to MacMonster For This Post:
# 4  
Old 09-27-2011
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:
Code:
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:
Code:
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

Last edited by Scott; 09-27-2011 at 03:11 PM.. Reason: Use code tags, please...
# 5  
Old 09-27-2011
Simply pipe the output of "ls" to "sed". That is:

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

This User Gave Thanks to MacMonster For This Post:
# 6  
Old 09-27-2011
I tested this version and it works:
Code:
sed '/^BU/{N;N;/total 0/d;}'

# 7  
Old 09-27-2011
Quote:
Originally Posted by cdc01
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:
Code:
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:
Code:
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
However MacMonster solution maybe much better than this but i'll explain this again,
you'll need to create and run script.sh:

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Student needs grep command assistance

I am a student in a UNIX/Linux classes having hard time with grep looking for assistance on some home work I need to figure out some kind of grep command to get a out put that only gives me information from a sample file and only list the people with the first name John the sample file is named... (1 Reply)
Discussion started by: jetoutant
1 Replies

2. AIX

AIX 3.2.5 Assistance

I am looking for any help on locating a set of install disks or downloads of AIX 3.2.5 or earlier.. We recently lost our HDD running AIX 3.2.5 and need to reinstall but have no OS disk to complete.. Any help would be appreciated.. Thanks (4 Replies)
Discussion started by: rmsdp
4 Replies

3. Homework & Coursework Questions

Grep expressions homework, need assistance

Please dont delete, im listing my assignment and will be editing as i work on it. I am NOT looking for answers but help in understanding how to use grep 1. The problem statement, all variables and given/known data: For each question, list the command lines used in addition to any other details... (3 Replies)
Discussion started by: alindner
3 Replies

4. Shell Programming and Scripting

Need assistance with sed

Hi All, I need your assistance, I would like to replace all lines beginning with the word "begin" with the below text: Device | IPMB0-A | IPMB0-B Board Address |Sent SentErr %Errr |Sent SentErr ... (9 Replies)
Discussion started by: Dendany83
9 Replies

5. UNIX for Dummies Questions & Answers

Crontab assistance

I have a script that runs a SQL query and then emails me the results. It runs correctly alone. I am trying to set up a cron that will run the script every day at say 5:00. When the script runs alone, the results are emailed exactly like I want the, but when I set up a cronjob, I get a blank email.... (7 Replies)
Discussion started by: alpinescott
7 Replies

6. Shell Programming and Scripting

Grep Alerting - command or script assistance

Hello! I need some help with grep from various logs we use for monitoring transactions. The logs contain the following information (which is consistent in all of the files): 12:28:33.157 EWY D 1 (tcpip.c:282): tcpip.c: Unable to connect to x.x.x.x on port xxxx. (79) Connection refused ... (1 Reply)
Discussion started by: sbchecko
1 Replies

7. Shell Programming and Scripting

Awk Assistance

Hello A friend of mine posted this on another site that I follow. It is to advanced for me to figure out. If solved I will give credit where credit is due: NOTE: Does not have to be AWK. Any Language will work, Hi. I need a little assistant to write an awk script on linux that reads a file... (12 Replies)
Discussion started by: abacus
12 Replies

8. UNIX for Dummies Questions & Answers

While Loop assistance.

I am trying to make a simple while loop which reads in a text until the person types quit. And it's not working, and I know it's a rather simple problem I just can't seem to understand... Once again all assistance is greatly appreciated. #!/bin/sh astring="z" while astring!="quit" do read... (2 Replies)
Discussion started by: MaestroRage
2 Replies

9. Shell Programming and Scripting

I need an assistance

I have a school project to create a shell program same as calendar i must create a txt file with celebrations with vi i know this but the problem is i don't know awk and grep. The object of object is to create a program who read a date an appear the celebration. Can you help me please !!!... (1 Reply)
Discussion started by: mytilini boy
1 Replies

10. UNIX for Dummies Questions & Answers

Need Assistance

I have two questions I am struggling with... How do the programs p1, p2, and p3 need to handle their standard files so they can work like this: p1 | p2 | p3 ? What exactly is this command supposed to do? $ kill -QUIT %1 & This command below? $ sort -o emp.lst emp lst & Any... (1 Reply)
Discussion started by: yahoo14
1 Replies
Login or Register to Ask a Question