Parse files in directory and compare with another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse files in directory and compare with another file
# 1  
Old 09-07-2012
Parse files in directory and compare with another file

I have two files

File 1 in reading directory is of following format
 
Read 1 A T
Read 3 T C
Read 5 G T
Read 7 A G
Read 10 A G
Read 12 C G

File 2 in directory contains
 
Read 5 A G
Read 6 T C
Read 7 G A
Read 8 G A
Read 20 A T
File2 contains

1
2
3
4
5
6
7
I need to read file 2 first for positons and print out the corresponding value in fourth column from opened file, in directory, in horizontal manner. If no match for that postion, it is printed as "-". The output for above should be

Code:
     1 2 3 4 5 6 7
Read T - C - T - G
Read - - - - G C A

I need to do that for all the files and print in above format in another row. so output will be only one file with number of rows equals no of files. Can i do this in perl easily?
# 2  
Old 09-07-2012
file is the index (file2 in your example with "1 2 3 4 5 6 7").

Code:
awk '
function print_tab()
{
  for (k=1;k<=j;k++) {
    i=ind[k]
    printf "%-4s",tab[i]
    tab[i]="-"
  }
  printf "\n"
}

FNR == NR {j++ ; ind[j]=$1 ; tab[$1]=$1 ; next}
FNR == 1 && NR !=1 {print_tab()}
$2 in tab {tab[$2]=$4}
END {print_tab()}
' file file1 file2

So there are two arrays:
- ind : is a buffer of the index file. ind[i] is the content of the i line. (in your exemple ind[1]=1…)
- tab : is the letter of the number of the second field. first line : tab[1]=1, second line tab[1]=T, third line tab[1]=-


Code:
FNR == NR {j++ ; ind[j]=$1 ; tab[$1]=$1 ; next}

when awk read the first file (so the index file with 1 2 3 4 5 6 7) we fill in arrays. Please note that "j" will be the number of line of the first file after the reading (7 in the example).
Code:
FNR == 1 && NR !=1 {print_tab()}

when awk read the first line of others file print the line
Code:
$2 in tab {tab[$2]=$4}

when the second file is in the index of tab we fill in with the value



PRINT TAB :
Code:
for (k=1;k<=j;k++) {

k between 1 and j (number of line in the index file).
Code:
i=ind[k]

so i is now the "k" line of index file.
Code:
printf "%-4s",tab[i]

print the corresponding letter
Code:
tab[i]="-"

refresh array with "-"[/code]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare directories and copy differences (files) in a another directory

Hey im working on script that can compare 2 directory and check difference, then copy difference files in third diretory. here is the story: in folder one we have 12 subfolder and in each of them near 500 images hosted. 01 02 03 04 05 06 07 08 09 10 11 12 in folder 2 we have same subfolder... (2 Replies)
Discussion started by: nimafire
2 Replies

2. Shell Programming and Scripting

Compare Only "File Names" in 2 Files with file lists having different directory structure

I have a tar arcive arch_all.tar.gz and 4 batched tar archive . These batches are supposed to have all the files form arch1.all.tar.gz arch1_batch1.tar.gz arch1_batch2.tar.gz arch1_batch3.tar.gz arch1_batch4.tar.gz my issue is that the directory structure in "arch_all.tar.gz" is... (6 Replies)
Discussion started by: sumang24
6 Replies

3. UNIX for Advanced & Expert Users

Script to parse and compare information in two fields of file

Hello, I am working parsing a large input file1(field CFA) I have to compare the the file1 field(CFA byte 88-96) with the content of the file2(It contains only one field) and and insert rows equal in another file. Here is my code and sample input file: ... (7 Replies)
Discussion started by: GERMANOS
7 Replies

4. Shell Programming and Scripting

Parse multiple html files in directory

I have downloaded source code for 97 files using: wget -x -i link.txt then run a rename loop: for file in * do mv $file $file.txt done to keep the html tags but make the file a text that can be parsed. In each of the 97 txt files the gene # is variable, but the gene is associated... (15 Replies)
Discussion started by: cmccabe
15 Replies

5. Shell Programming and Scripting

perl Compare zone files in directory with what is listed in named.conf

I would really appreciate any assistance that I can get here. I am fairly new to perl. I am trying to rewrite my shell scripts to perl. Currently I have a shell script (using sed, awk, grep, etc) that gets a list of all of the zone files in a directory and then looks in named.conf for what... (0 Replies)
Discussion started by: brianjb
0 Replies

6. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

7. Shell Programming and Scripting

Compare files in a directory

Hi friends, can anyone help me comparing files in a directory. I have files f1, f2, f3, f4, f5, f6, f7, f8 in a directory each created on a daily basis as f1 on day1, f2 on day2, f3 on day3, f4 on day4, f5 on day5, f6 on day6, f7 on day7, f8 on day8. I need ignore the latest two files (here f7 and... (5 Replies)
Discussion started by: nmattam
5 Replies

8. Shell Programming and Scripting

compare files in two directories and output changed files to third directory

I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together. I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Discussion started by: bkeep
4 Replies

9. Shell Programming and Scripting

to parse a directory and its subdirectories and find owner name of files

hi all, i need to capture all the files in a directory and its subdirectories that have owner name different than the root owner. for one file it is " stat -c %U filename " but i need to search for each and every file and record it. thanks in advance (14 Replies)
Discussion started by: vyasa
14 Replies

10. Shell Programming and Scripting

Compare all files in a directory to a threshold value

Hi guys, I have the following, and would like to enhance it be be able to run it in the hard coded directory and compare each file in the directory with the expectedSizeHow would I go about doing this? Thanks, Bloke #!/bin/sh ] || { echo "Usage: watchSizes 400"; exit 0 ; } #Hammer: How... (1 Reply)
Discussion started by: Bloke
1 Replies
Login or Register to Ask a Question