LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script using grep to filter 10 tar files (https://www.linuxquestions.org/questions/linux-newbie-8/script-using-grep-to-filter-10-tar-files-4175459796/)

amejiag 04-27-2013 11:57 AM

script using grep to filter 10 tar files
 
I have to make a script using grep filtering a pattern in a 10 files that have been compressed tar format, this is what I have done until now.

#!/bin/sh

cd ../Data

ls *.tar #list file with extention tar

tar – xzvf *.tar > ../Result/file01.tar #decompress tar file and send to directory Result as
file01.tar

for i = 1; $i <=10; $i=++; #for i as the first file until the 10 th file find a pattern
from tar files in Data and send to Result directory as fileresult.out

do

grep “pattern” Data/*.tar > Result/fileresult.out
echo “\n”

done

Also I have to improve this script

#!/bin/sh

# ../Data/subject01.fa ../Data/query01.fa ../Data/subject02.fa ../Data/query02.fa


cd ../Data

formatdb -i subject01.fa -p F
formatdb -i subject02.fa -p F


cd ../Code

blastall -p blastn -d $1 -i $2 -F F -e 1e-5 -o ../Results/blast01.raw -m 9
blastall -p blastn -d $3 -i $4 -F F -e 1e-5 -o ../Results/blast02.raw -m 9


for i in ../Results/b*.raw;

do

echo $i >> ../Results/filtro100.out
grep 100.00 $i >> ../Results/filtro100.out
echo "\n" >> ../Results/filtro100.out

done

I modified as:

#!/bin/sh

# ../Data/subject01.fa ../Data/query01.fa ../Data/subject02.fa ../Data/query02.fa

cd ../Data

formatdb -i subject01.fa -p F -i subject02.fa -p F

cd ../Code



blastall -p blastn -d ../Data/subject01.fa -i ../Data/query01.fa -d ../Data/subject02.fa -i ../Data/query02.fa -F F -e 1e-5 -o ../Results/blast01.raw -m 9 ../Results/blast02.raw -m 9 #without blast02.raw it run as only blast01.raw

#blastall -p blastn -d -F F -e 1e-5 -o ../Results/blast02.raw -m 9
cat ../Results/blast01.raw ../Results/blast02.raw > ../Results/prueba.raw

for i in ../Results/p*.raw;
do

echo $i >> ../Results/filtro100.out
grep 100.00 $i >> ../Results/filtro100.out
echo "\n" >> ../Results/filtro100.out

done

when I run this script come out this error
[blastall] ERROR: Arguments must start with '-' (the offending argument #19 was: '../Results/blast02.raw')

as I what to incluide not only filter 100 I change the last part as

do

echo $i >> ../Results/filtro100.out
grep ["$i" -le 100.00] || ["$i" -ge 90.00] $i >> ../Results/filtro100.out
echo "\n" >> ../Results/filtro100.out

done

but is send an error, how I can to fix this and it is something that I can improve of this script

Thanks you for the help

TB0ne 04-27-2013 02:55 PM

Quote:

Originally Posted by amejiag (Post 4940104)
My name is Adriana, and I have to make a script using grep filtering a pattern in a 10 files where are compress with tar, I hope to heard from you soon. thanks your very much

We are not going to write scripts for you, but we will be glad to HELP you. So, post what you've written/tried so far, and where you're stuck. Otherwise, you can read the man pages on the commands, and look up any of the VERY easily-found bash scripting tutorials on Google, and start there. Also, be aware that we aren't going to give you answers to homework questions, either.

chrism01 04-28-2013 07:07 AM

In addition, tar is not a compression tool; it just collects all input into one big file archive.
If you created with with the z (gzip) or j (bzip2) flag, then it will be compressed as well.
If the archive is compressed, you will need to uncompress it first.

shivaa 04-28-2013 07:52 AM

What you've done so far and let's know if you're stuck somewhere. Following would be helpful for you:
http://tldp.org/LDP/abs/html/
http://linux.die.net/man/1/tar
http://linux.die.net/man/1/gzip

lleb 04-28-2013 10:35 AM

Quote:

Originally Posted by amejiag (Post 4940104)
My name is Adriana, and I have to make a script using grep filtering a pattern in a 10 files where are compress with tar, I hope to heard from you soon. thanks your very much

sounds an awful lot like homework to me.

as stated post what you have done, we will be glad to help you fix your errors, but not do you homework for you as that is against forum rules.

amejiag 04-28-2013 05:26 PM

Quote:

Originally Posted by lleb (Post 4940551)
sounds an awful lot like homework to me.

as stated post what you have done, we will be glad to help you fix your errors, but not do you homework for you as that is against forum rules.

Sorry about this impression, It is my fault about my message, but I do not try that someone do the "homework" for my, how I can learn from it if I do not do an effort. Thanks you for take your time and answer my message

unSpawn 04-28-2013 07:51 PM

Quote:

Originally Posted by amejiag (Post 4940734)
(..) I do not try that someone do the "homework" for my, how I can learn from it if I do not do an effort.

Then best start by posting what you tried.

Here's some BASH scripting guides should you need them:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.gnu.org/software/bash/man...ode/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls

*Note the Advanced BASH Scripting Guide is not the first link to read (and for good reasons).

amejiag 04-29-2013 09:27 PM

grep a patter in 10 file comprised in a tar file
 
I made this script

#!/bin/sh

cd ../Data_taller_eval/Data_preg2.tar

#list file with extention tar

tar –xzvf Data_preg2.tar > ../Result_taller_eval #decompress tar file and send to directory Result as file01.tar


#for (my $i=1;$i<=10;$i++;) #for i as the first tar file until the 10 th file find a pattern from tar files Data and send to Result directory fileresult.out

#do

#grep “agcct” Data/*.sh > Result/fileresult.out
#echo “\n”

#done

I try to run the first part but I get this error

tar: opción incorrecta -- «�»
Intente`tar --help' o `tar --usage' para mayor información.
I when to man tar but I do not what to do
when I run all the script give me an error
./scriptgrep.sh: 10: Syntax error: Bad for loop variable
How I make an good for loop variable

Thanks you for your help

shivaa 04-29-2013 10:39 PM

Fisrt thing is to add debug mode i.e. set -xv in your script and check that where scripts stuck.

Second, in order to check content of a tar file, use -t flag, as:
Code:

~$ tar -tvf foo.tar
Note that you cannot simply do cd on a tar file, because a .tar is not a directory, but a file.

However, you can grep a pattern simply, as:
Code:

~$ cd ../Data_taller_eval/
~$ grep <pattern> <(tar -tvf Data_preg2.tar)


amejiag 04-29-2013 11:45 PM

Thanks you for your answer I did as

#!/bin/sh

set -xv

cd ../Data_taller_eval/


grep "agcct" <(tar -tvf Data_preg2.tar) #list file with extention tar

#tar –xzvf Data_preg2.tar > ../Result_taller_eval #decompress tar file and send to directory Result as file01.tar


#for (my $i=1;$i<=10;$i++;) #for i as the first tar file until the 10 th file find a pattern from tar files Data and send to Result directory fileresult.out

#do

#grep “agcct” Data/*.sh > Result/fileresult.out
#echo “\n”

#done

and the answer is cd ../Data_taller_eval/
+ cd ../Data_taller_eval/


grep "agcct" <(tar -tvf Data_preg2.tar) #list file with extention tar
./scriptgrep.sh: 8: Syntax error: "(" unexpected
I do not recognized what is the syntax error and how I make this with 10 file the last part I used for (my $i=1;$i<=10;$i++;) but it also get error

Thanks you

shivaa 04-30-2013 12:03 AM

Use /bin/bash instead of /bin/sh.

You can also try like:
Code:

#!/bin/bash
while read -r file
do
grep 'agcct' $file
done < <(tar -tvf Data_preg2.tar | awk -F' ' '{print $6}')


amejiag 04-30-2013 12:27 AM

Thanks, I try to run this

#!/bin/bash


set -xv

cd ../Data_taller_eval/

while read -r file
do
grep 'agcct' $file
done < $(tar -tvf Data_preg2.tar)

and I get this

cd ../Data_taller_eval/
+ cd ../Data_taller_eval/

while read -r file
do
grep 'agcct' $file
done < $(tar -tvf Data_preg2.tar)
tar -tvf Data_preg2.tar)
tar -tvf Data_preg2.tar
++ tar -tvf Data_preg2.tar
tar: Data_preg2.tar: No se puede read: Es un directorio
tar: Al principio de la cinta, se terminará ahora
tar: Error is not recoverable: exiting now
tar -tvf Data_preg2.tar)
tar -tvf Data_preg2.tar
++ tar -tvf Data_preg2.tar
tar: Data_preg2.tar: No se puede read: Es un directorio
tar: Al principio de la cinta, se terminará ahora
tar: Error is not recoverable: exiting now
./scriptgrep1.sh: línea 11: $(tar -tvf Data_preg2.tar): redireccionamiento ambiguo

line 11 is ambiguos and how I can to make this to 10 file ?

shivaa 04-30-2013 07:52 AM

Just try it now:
Code:

#!/bin/bash
while read -r file
do
grep 'agcct' $file
done < <(tar -tvf Data_preg2.tar | awk -F' ' '{print $6}')



All times are GMT -5. The time now is 05:30 PM.