Shell scripting : Help Me for this sequence

I have two input files (given below) and to compare each line of the File1 with each line of File2 starts with '>sample1'. If a match occurs and that matched line in the File2 contains another line or sequence of lines starting with "Chr" they have to be displayed in output file with that sample. If a match occurs and the matched line in File2 does not contain a 'Chr' line(s) it has be omitted or not taken into consideration for output file. For easy understanding, I marked the matched lines in file1 and file2 with blue color which are taken into consideration for final output. I maked the matched lines in file1 with file2 (which doesnt contain 'Chr' lines) in red color which are not taken into account for output. The final output to be selected also given below. PLS. KINDLY HELP ME FOR THE SHELL SCRIPTING OF THIS. [/COLOR

File1:
[COLOR="red"]>sample1:1:1:1057:7503#0 0 0
>sample1:1:1:1057:12664#0 0 0
>sample1:1:1058:8130#0 5 830
>sample1:1:1:1059:6357#0 0 0
>sample1:1:1:1059:10418#0 0 0
>sample1:1:1:1059:12084#0 1 1
>sample1:1:1:1060:11510#0 0 0
>sample1:1:1:1060:5177#0 0 0
>sample1:1:1:1061:8105#0 0 0
>sample1:1:1:1063:6105#0 0 0
>sample1:1:1:1064:11266#0 0 0
>sample1:1:1:1066:5654#0 0 0
>sample1:1:1:1067:10266#0 0 0
>sample1:1:1:1068:2100#0 0 0
>sample1:1:1:1069:3450#0 0 0
>sample1:1:1:1070:7530#0 0 0
>sample1:1:1:1071:8627#0 0 0
>sample1:1:1:1071:8552#0 0 0
>sample1:1:1:1072:7060#0 0 0
>sample1:1:1:1073:7329#0 0 0
>sample1:1:1:1073:20394#0 0 0
>sample1:1:1:1074:7081#0 0 0
>sample1:1:1:1076:1654#0 0 0
>sample1:1:1:1077:15575#0 0 0
>sample1:1:1:1077:15683#0 0 0

File2:
>sample1:1:1:1056:8164#0 1 1
Chr21 +25913822 2
>sample1:1:1:1057:7503#0 0 0
>sample1:1:1:1057:18666#0 1 1
Chr21 +25913822 2
>sample1:1:1:1057:1725#0 1 1
Chr21 +25913822 2
>sample1:1:1:1057:12664#0 0 0
>sample1:1:1:1057:18537#0 1 1
Chr21 +25913822 2
>sample1:1:1:1058:8130#0 5 830
Chr19 +52245923 1
Chr17 +69679873 1
Chr23 +52121254 1
Chr11 +100949523 1
Chr8 +28333267 1
>sample1:1:1:1058:19619#0 1 1
Chr21 +25913822 2
>sample1:1:1:1059:6357#0 0 0
>sample1:1:1:1059:10418#0 0 0
>sample1:1:1:1059:12084#0 1 1
Chr12 -19596251 2
>sample1:1:1:1060:13498#0 1 1

Output:
sample1:1:1:1058:8130#0 5 830
Chr19 +52245923 1
Chr17 +69679873 1
Chr23 +52121254 1
Chr11 +100949523 1
Chr8 +28333267 1
sample1:1:1:1059:12084#0 1 1
Chr12 -19596251

You posted the input data, but it would be kind to post us the expected result/output.

Regards

That's because your problem statement is quite vague. A "read" operation does not "have" a format, which is why "read" cannot "be" in any format.

Your input data in a file or from a pipe "has" or "is in" some particular format. So, I'll assume that this -

Chr18:4000-4010
Chr20:393939-400303
Chr30:38838-30020

is the format of your input data. Again, I'll assume that this data is in a file, as opposed to a pipe stream.

In the shell, you'd read a file like so -

$
$ # display the content of the file. My file is called "f32", yours may be different.
$ cat f32
Chr18:4000-4010
Chr20:393939-400303
Chr30:38838-30020
$
$ # read data from input file "f32"
$ while read LINE; do   echo "Oh my! I've now read this line => $LINE"; done < f32
Oh my! I've now read this line => Chr18:4000-4010
Oh my! I've now read this line => Chr20:393939-400303
Oh my! I've now read this line => Chr30:38838-30020
$
$

HTH,
tyler_durden

My input file (data1) is like this

Chr8:4000-4500
Chr10:4000-4600

I written a shell program like this.

while read LINE;
do   echo "$LINE";
samtools faidx Bos_taurusUMD3.fa "$line";
done < data1

It has to read a line like Chr8:4000-4500 from input file and that line has to be executed in a command like 'samtools faidx Bos_taurusUMD3.fa

Chr8:4000-4500'. Then I will get the sequence like this.
>Chr8:4000-4500
TAATTCGTTTTTCTTTTTTCCTCTCTGACTCATTTATTTGTACCATTCTATCTTCTAATT
CACTAATCTTATCTTCTGCCTCTGTTATTCTACTATTTGTCGCCTCCAGAGTGTTTTTGA
TCTCATTTATTGCATTATTCATTATATATTGACTCTTTTTTATGTCTTCTAGGTCCTTGT
TAAACCTTTCTTGCATCTTCTCAATCCTTGTCTCCAGGTTATTTATCTGTGATTCCATTT
TGATTTCAAGATTTTGGATCAATTTCACTATCATTATTCAGAATTCTTTATCAGGTAGAT
TCCTTATCTCTTCCTCTTTTGTTTTGTTTGGTGGGCATTTATCCTGTTCCTTTACCTGCT
GGGTATTCCTCTGTCTCTTCATCTTGTTTATATTGCTGAGTTTGGGGTGTCCTTTCTGTA
TTCTGGCAGTTTGTGGAGTTCTCTTTATTGTGGAGTTTCCTCGCTGTGTATGGGTTTGTA
CAGGTGGCTTGTCAAGGTTTC

but when I execute the shell it is displaying the output like this

 Chr8:4000-4500
>
Chr10:4000-4600
>

but not displaying the sequence.but I execute the command at $ prompt like
$samtools faidx Bos_taurusUMD3.fa Chr8:4000-4500
it is running and displaying the sequence.

How to execute it in my shell by reading each line from input line, execute it in command and display the sequences.
KINDLY HELP ME

try like this

samtools faidx Bos_taurusUMD3.fa "$line"

Thank u very much for your reply. I used as u specified "$line" in samtools command like samtools faidx Bos_taurusUMD3.fa "$line";

It is giving out like this. Sequence was not given. it displays it as > only.

Chr8:86884850-86884997
>
ChrX:96383583-96383703
>
Chr15:33347613-33347720
>
~

Kindly help me how to execute that to generate the sequence.

Little issue, but simple code.

awk 'BEGIN{RS=ORS=">";FS="\n"} NF==2' infile

sample1:1:1:1057:7503#0 0 0
>sample1:1:1:1057:12664#0 0 0
>sample1:1:1:1059:6357#0 0 0
>sample1:1:1:1059:10418#0 0 0
>sample1:1:1:1060:11510#0 0 0
>sample1:1:1:1060:5177#0 0 0
>sample1:1:1:1061:8105#0 0 0
>sample1:1:1:1063:6105#0 0 0
>sample1:1:1:1064:11266#0 0 0
>sample1:1:1:1066:5654#0 0 0
>sample1:1:1:1067:1055#0 1 1
>

Avoids missing ">" on 2nd line and extra ">" at end:

$ awk '{ if(NR>1&&$0!~"^miR"&&p!~"^miR")print p; p=$0}' infile

>sample1:1:1:1057:7503#0 0 0
>sample1:1:1:1057:12664#0 0 0
>sample1:1:1:1059:6357#0 0 0
>sample1:1:1:1059:10418#0 0 0
>sample1:1:1:1060:11510#0 0 0
>sample1:1:1:1060:5177#0 0 0
>sample1:1:1:1061:8105#0 0 0
>sample1:1:1:1063:6105#0 0 0
>sample1:1:1:1064:11266#0 0 0
>sample1:1:1:1066:5654#0 0 0

I'll opt for

awk 'BEGIN{FS=RS;RS=ORS=">"} !/miR/' file

I know I know .... this is hugly :smiley:

#!/usr/bin/ksh

cat file1 | tr -d '>' >file1.filtered
cat file2 | tr -d '>' >file2.filtered
comm -12 file1.filtered file2.filtered >common
>output
PRN=N
prevline="`head -1 file2.filtered`"

while read line
do
if ( echo "$line" | grep Chr >/dev/null 2>&1 )
then
        if ( grep "$prevline" common >/dev/null 2>&1 )
        then
                PRN=Y
                echo "$prevline"
                echo "$line"
        else
                ( echo "$prevline" | grep Chr >/dev/null 2>&1 ) && echo "$line"
        fi
else
PRN=N
fi
prevline="$line"
done <file2.filtered | tee -a output
rm file1.filtered file2.filtered common