tags:

views:

62

answers:

3

This is the code i'm using to untar a file grep on the contents of the files within the tar and then delete the untared files. I dont have enough space to untar all files at once.

the issue i'm having is with the for f in `ls | grep -v *.gz line this is supposed to find the files that have come out of the tar and can be identified by not having a .tar.gz extension but it doesnt seem to pick them up?

Any help would be much appreciated

M

for i in *.tar.gz; 
do echo $i >>outtput1; 
     tar -xvvzf $i; mv $i ./processed/; 
     for f in `ls | grep -v *.gz`;  ----- this is the line that isn't working
     do echo $f  >> outtput1; 
     grep 93149249194 $f >> 
     outtput1; grep 788 $f >> outtput1; 
     rm -f $f; 
     done; 
done
+3  A: 

Try ls -1 | grep -v "\\.gz$". The -1 will make ls output one result per line. I've also fixed your regex for you in a few ways.

Although a better way to solve this whole thing is with find -exec.

Borealid
No need for -1. When you pipe ls output, normally the files are given one by one.
thegeek
@thegeek: why, so they are. Nifty.
Borealid
A: 

Change it to ls | grep -v "*.gz", you have to quote *.gz because otherwise it will just glob the files in the working directory and grep them.

Jesse Dhillon
A: 

Never use ls in scripts, and don't use grep to match file patterns. Use globbing and tests instead:

for f in *
do
    if [[ $f != *.gz ]]
    then
        ...
    fi
done
Philipp