Sponsored Content
Top Forums Shell Programming and Scripting Match and store numerical prefix to update files Post 302990944 by cmccabe on Friday 3rd of February 2017 08:17:33 AM
Old 02-03-2017
Some of the file names and locations were different because I was at the office then at home and that was a problem leading to typos. I apologize for that and am back in the office. The details below are the exact files in the directory.

There are always 3 vcf.gz files in the following format below in each directory:

Files in /home/cmccabe/Desktop/NGS/test
Code:
16-0000_File-A_variant_strandbias_readcount.vcf.gz
16-0002_File-A_variant_strandbias_readcount.vcf.gz
16-0005_File-A_variant_strandbias_readcount.vcf.gz

The numeric prefix of each is always 7 characters xx-xxxx and unique. The portion of code in the previous post uses each vcf.gz file to create a corresponding .txt file with only the unique numeric prefix followed by _header.

Files in /home/cmccabe/Desktop/NGS/test (if code works as expected)
Code:
16-0000_File-A_variant_strandbias_readcount.vcf.gz
16-0002_File-B_variant_strandbias_readcount.vcf.gz
16-0005_File-A_variant_strandbias_readcount.vcf.gz
16-0000_header.txt
16-0002_header.txt
16-0005_header.txt

I am trying to match the 16-0000.vcf.gz with the 16-0000.txt and read 16-0000.vcf.gz into $file1 and 16-0000.txt into $file2.

These two variables $file1 and $file2 would be passed to the reheader command, in bold,below which is part of a loop.

After the $file1 and $file2 are processed for 16-0000, the process is repeated for the remaining two files, 16-0002 and 16-0005.

reheader command
Code:
logfile=/home/cmccabe/Desktop/NGS/test/process.log
for f in /home/cmccabe/Desktop/NGS/test/*.vcf.gz ; do
     echo "Start vcf edit header creation: $(date) - file: $f"
     bname=`basename $f`
     pref=${bname%%.vcf.gz}
     bcftools reheader -h $file1 $file2 > ${pref}_fixed.vcf.gz
     echo "End edit header creation: $(date) - file: $f"
done >> "$logfile"

There is only one directory that contains the files and that is /home/cmccabe/Desktop/NGS/test. The numerical prefixes are not being stripped off and used to perform the match between files nor are the variables being passed to reheader. Thank you Smilie.

Code:
ls -l /home/cmccabe/Desktop/NGS/test
total 6116
-rw-rw-r-- 1 cmccabe cmccabe    9350 Feb  3 07:08 16-0000_File-A_variant_strandbias_readcount_header.txt
-rw-rw-r-- 1 cmccabe cmccabe 2031861 Jan 31 11:07 16-0000_File-A_variant_strandbias_readcount.vcf.gz
-rw-rw-r-- 1 cmccabe cmccabe    9353 Feb  3 07:08 16-0002_File-B_variant_strandbias_readcount_header.txt
-rw-rw-r-- 1 cmccabe cmccabe 2361911 Jan 31 11:07 16-0002_File-B_variant_strandbias_readcount.vcf.gz
-rw-rw-r-- 1 cmccabe cmccabe    9351 Feb  3 07:08 16-0005_File-C_variant_strandbias_readcount_header.txt
-rw-rw-r-- 1 cmccabe cmccabe 1816700 Jan 31 11:07 16-0005_File-C_variant_strandbias_readcount.vcf.gz
-rw-rw-r-- 1 cmccabe cmccabe    2622 Feb  3 07:08 process.log
-rw-rw-r-- 1 cmccabe cmccabe    1278 Feb  2 13:17 process.log~

Does this help? Thank you very much Smilie.

Last edited by cmccabe; 02-07-2017 at 05:19 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

Fuzzy Match Logic for Numerical Values

I have searched the internet (including these forums) and perhaps I'm not using the right wording. What I'm looking for is a function (preferably C) that analyzes the similitude of two numerical or near-numerical values, and returns either a true/false (match/nomatch) or a return code that... (4 Replies)
Discussion started by: marcus121
4 Replies

2. Shell Programming and Scripting

Match Pattern and store next value into array

Hi, I am trying to write a script which parses a log file and will eventually put the values in an array so that I can perform some math on it. In this file I am only interested in the last 200 lines so here is the command I use to display the contents in a manageable manner. tail -200... (3 Replies)
Discussion started by: Filter500
3 Replies

3. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

4. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

5. Shell Programming and Scripting

Bash to match and store line as variable

The bash below loops through a specific directory dir and finds and writes the oldest folder to a variable called $filename. #!/bin/bash # oldest folder stored as variable for analysis, version log created, and quality indicators matched to run dir=/home/cmccabe/Desktop/NGS/test find... (2 Replies)
Discussion started by: cmccabe
2 Replies

6. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

7. Shell Programming and Scripting

Bash to add portion of text to files in directory using numerical match

In the below bash I am trying to rename eachof the 3 text files in /home/cmccabe/Desktop/percent by matching the numerical portion of each file to lines 3,4, or 5 in /home/cmccabe/Desktop/analysis.txt. There will always be a match between the files. When a match is found each text file in... (2 Replies)
Discussion started by: cmccabe
2 Replies

8. Shell Programming and Scripting

awk to update file with numerical difference if condition is met

In the file1 below if $9 and $12 are . (dot) then the value in $8 of file1 is used as a key (exact match) to lookup in each $2 of file2, when a match is found then the value of $4 in file1 is used to look for a range match within +/- 50 using the values in $4 and after in file2. The number of... (9 Replies)
Discussion started by: cmccabe
9 Replies

9. Shell Programming and Scripting

awk move select fields to match file prefix in two directories

In the awk below I am trying to use the file1 as a match to file2. In file2 the contents of $5,&6,and $7 (always tab-delimited) and are copied to the output under the header Quality metrics. The below executes but the output is empty. I have added comments to help and show my thinking. Thank you... (0 Replies)
Discussion started by: cmccabe
0 Replies

10. Shell Programming and Scripting

Bash to update file on prefix match in two directories

I am trying to use bash to loop through a directory /path/to/data using a prefix match from /path/to/file. That match is obtained and works using the code below (in green)... what I can not seem to do is populate or update the corresponding prefix_file.txt in /path/to/data with the values in each... (3 Replies)
Discussion started by: cmccabe
3 Replies
All times are GMT -4. The time now is 06:21 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy