Rename file using partial match to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename file using partial match to another
# 15  
Old 10-26-2019
Are you using bash shell? Can you post output with this additional debugging:

Code:
#!/bin/bash
dir=/path/to/run/
for run in "$dir"/R_2019* ; do  ## # matching "R_2019*" to operate on desired directory and expand
  uniq=${run##*/}  ## store run with no path as s5
  while read from to
  do
     (
       cd "$dir"/"$uniq"/data
       echo "Rename from:$from to:$to"
       for file in *.bam*
       do
          newname=${file/$from*.bam/${to}_test.bam}
          [ -f "$file" ] && [ "$newname" != "$file" ] && echo mv "$file" "$newname"
       done
     )
  done <<<$(
     awk -F '\n' -v RS="" -v ref="$uniq" '
         $0 ~ ref {
             d=split($0, val);
             for(i=1;i<d;i++) print val[i];
          }' "$dir"/f1
  )  ## loop through f1 for unique run and populate from and to
done

Here is how this looks with my setup:

Code:
$ ./cmccabe_rename 
Rename from:IonCode_0404 to:10-0000-aa-aa-aa
Rename from:IonCode_0412 to:55-1111-bb-bbb-bbb
Rename from:IonCode_0402 to:22-2222-zz-zzzz-zzz
mv IonCode_0402_xxx.xxx_xxx.bam 22-2222-zz-zzzz-zzz_test.bam
mv IonCode_0402_xxx.xxx_xxx.bam.bai 22-2222-zz-zzzz-zzz_test.bam.bai
Rename from:IonCode_0404 to:00-0000-xxx-xxx-xxx
mv IonCode_0404_xxx.xxx_xxx.bam 00-0000-xxx-xxx-xxx_test.bam
mv IonCode_0404_xxx.xxx_xxx.bam.bai 00-0000-xxx-xxx-xxx_test.bam.bai
Rename from:IonCode_0402 to:11-1111-yy-yy-yyy

This User Gave Thanks to Chubler_XL For This Post:
# 16  
Old 10-28-2019
Here is the output, yes I am using bash shell. Thank you Smilie.


Code:
 with echo
Rename from: to:
mv IonCode_0402_xxx.xxx_xxx.bam _test.bam
mv IonCode_0402_xxx.xxx_xxx.bam.bai _test.bam.bai
mv IonCode_0404_xxx.xxx_xxx.bam _test.bam
mv IonCode_0404_xxx.xxx_xxx.bam.bai _test.bam.bai

Files in directory to rename
Code:
ll
total 8
drwxr--r-- 2 cmccabe cmccabe 4096 Oct 28 09:59 ./
drwxr--r-- 3 cmccabe cmccabe 4096 Oct 28 08:12 ../
-rw-rw-r-- 1 cmccabe cmccabe    0 Oct 28 09:56 IonCode_0404_xxx_xxx_xxx.bam
-rw-rw-r-- 1 cmccabe cmccabe    0 Oct 28 09:58 IonCode_0404_xxx_xxx_xxx.bam.bai
-rw-rw-r-- 1 cmccabe cmccabe    0 Oct 28 09:50 IonCode_0402_xxx_xxx_xxx.bam
-rw-rw-r-- 1 cmccabe cmccabe    0 Oct 28 09:54 IonCode_0402_xxx_xxx_xxx.bam.bai

without echo in the code only one pair of files gets renamed with both values
Code:
ll
total 8
drwxr--r-- 2 cmccabe cmccabe 4096 Oct 28 09:59 ./
drwxr--r-- 3 cmccabe cmccabe 4096 Oct 28 08:12 ../
-rw-rw-r-- 1 cmccabe cmccabe    0 Oct 28 09:56 00-0000-xxx-xxx-xxx IonCode_0402 11-1111-yy-yy-yyy_test.bam
-rw-rw-r-- 1 cmccabe cmccabe    0 Oct 28 09:58 00-0000-xxx-xxx-xxx IonCode_0402 11-1111-yy-yy-yyy_test.bam.bai
-rw-rw-r-- 1 cmccabe cmccabe    0 Oct 28 09:50 IonCode_0402_xxx_xxx_xxx.bam
-rw-rw-r-- 1 cmccabe cmccabe    0 Oct 28 09:54 IonCode_0402_xxx_xxx_xxx.bam.bai


Last edited by cmccabe; 10-28-2019 at 12:18 PM..
# 17  
Old 10-28-2019
I suspect you have some corruption in the format of the f1 file. Perhaps it has been edited with a DOS editor or something like that.

Can you post here the output of od -c /path/to/run/f1
This User Gave Thanks to Chubler_XL For This Post:
# 18  
Old 10-28-2019
Here is the output ofod -c /path/to/f1. Thank you Smilie.

Code:
0000000   I   o   n   C   o   d   e   _   0   4   0   4       0   0   -
0000020   0   0   0   0   -   x   x   x   -   x   x   x   -   x   x   x
0000040  \n   I   o   n   C   o   d   e   _   0   4   0   2       1   1
0000060   -   1   1   1   1   -   y   y   -   y   y   -   y   y   y  \n
0000100   R   _   2   0   1   9   _   0   0   _   0   0   _   0   0   _
0000120   0   0   _   0   0   _   x   x   x   x   _   x   x   1   -   1
0000140   2   7   -   x   x   x   _   x   x   x   _   x   x   x   _   x
0000160   x   x   _   x   x   _   x   x   _   x   x  \n  \n   I   o   n
0000200   C   o   d   e   _   0   4   0   2       2   2   -   2   2   2
0000220   2   -   z   z   -   z   z   z   z   -   z   z   z  \n   R   _
0000240   2   0   1   9   _   0   0   _   0   0   _   0   0   _   0   0
0000260   _   0   0   _   x   x   x   x   _   x   x   1   -   1   2   6
0000300   -   x   x   x   _   x   x   x   _   x   x   x   _   x   x   x
0000320   _   x   x   _   x   x   _   x   x  \n  \n   I   o   n   C   o
0000340   d   e   _   0   4   0   4       1   0   -   0   0   0   0   -
0000360   a   a   -   a   a   -   a   a  \n   I   o   n   C   o   d   e
0000400   _   0   4   1   2       5   5   -   1   1   1   1   -   b   b
0000420   -   b   b   b   -   b   b   b  \n   R   _   2   0   1   9   _
0000440   0   0   _   0   0   _   0   0   _   0   0   _   0   0   _   x
0000460   x   x   x   _   x   x   1   -   1   2   0   -   x   x   x   _
0000500   x   x   x   _   x   x   x   _   x   x   x   _   x   x   _   x
0000520   x   _   x   x  \n

# 19  
Old 10-28-2019
Nothing wrong with the data file, it matches what I'm using here byte-for-byte. Can you post the script you are using?
This User Gave Thanks to Chubler_XL For This Post:
# 20  
Old 10-28-2019
I pass run_dir as an argument instead of hardcoding dir. I think that is the only difference. I made that change to make it easier for others. Thank you Smilie

Code:
run_dir=$1
for run in "$run_dir" ; do  ## # grab run to operate on desired directory
   uniq=${run_dir##*/}  ## store run with no path as uniq
while read from to
  do
     (
       cd "$run_dir"/bam
       echo "Rename from:$from to:$to"
       for file in *.bam*
       do
          newname=${file/$from*.bam/${to}_RNA.bam}
          [ -f "$file" ] && [ "$newname" != "$file" ] && mv "$file" "$newname"
       done
     )
  done <<<$(
     awk -F '\n' -v RS="" -v ref="$uniq" '
         $0 ~ ref {
             d=split($0, val);
             for(i=1;i<d;i++) print val[i];
          }' "$run_dir"/f1
  )  ## loop through f1 for unique run and populate from and to
done

# 21  
Old 10-29-2019
Running out of ideas on why this isn't working for you - Is they running on Solaris OS? Perhaps you need to user nawk instead of awk
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to rename portion of file using match to another

In the portion of bash below I am using rename to match the $id variable to $file and when a match (there will alwsys be one) is found then the $id is removed from each bam and bam.bai in $file and _test is added to thee file name before the extension. Each of the variables is set correctly but... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. 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

3. Shell Programming and Scripting

Partial Match and Replace

Hi, I have a tab delimited text file like this one. I need to do a partial match of a particular cell and then replace matches with an empty cell. So here is a sample: Smith FordMustang ChevroletCamaro Miller FordFiesta Jones KiaSorrento Davis ChevroletCamaro Johnson ToyotaHighlander I... (4 Replies)
Discussion started by: mikey11415
4 Replies

4. Shell Programming and Scripting

Rename files to match file list pattern

Hi All, I have 100 folders with the first delimiter has a unique name i.e (123_hello and 575_hello) and each folder have atlist 1000 plus files with naming convention i.e (575_hello_1.iso ... 575_hello_1000.iso). 575_hello/575_hello_1.iso 575_hello/575_hello_2.iso 575_hello/575_hello_3.iso... (8 Replies)
Discussion started by: lxdorney
8 Replies

5. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

6. Shell Programming and Scripting

Match partial text

I posted the incorrect files yesterday and apologize. I also modified the awk script but with no luck. There are two text files in the zip (name.txt and output.txt). I am trying to match $2 in name.txt with $1 in output.txt and if they match then $1 of name.txt is copied to $7 of output.txt. ... (7 Replies)
Discussion started by: cmccabe
7 Replies

7. UNIX for Dummies Questions & Answers

How to substitute for the partial match?

Hi I have a question and hope I can get answer here. Thank you in advance. I have two files: file1: aa X bb Y cc Z file2: cc A bb B dd C aa D bb E If the 1st column match in both file1 and file2, the 2nd column in file2 will be replaced by the 2nd column in file1. If there is no... (2 Replies)
Discussion started by: yuejian
2 Replies

8. UNIX for Dummies Questions & Answers

Partial match in two files then substitute

Hi, I was trying to figure this out but failed so I hope someone here can help me, thank you in advance. I have two files. file1: aa M bb N cc O dd P ee Q file2: aa A_87_P254063 cc A_87_P016532 bb A_87_P104793 dd A_87_P055331 ee A_87_P059706 aa A_87_P071636 ee A_87_P028302... (2 Replies)
Discussion started by: yuejian
2 Replies

9. Shell Programming and Scripting

Using grep returns partial matches, I need to get an exact match or nothing

I’m trying to modify someone perl script to fix a bug. The piece of code checks that the zone name you want to add is unique. However, when the code runs, it finds a partial match using grep, and decides it already exists, so the “create” command exits. $cstatus = `${ZADM} list -vic | grep... (3 Replies)
Discussion started by: TKD
3 Replies

10. Shell Programming and Scripting

awk partial match and filter records

Hi, I am having file which contains around 15 columns, i need to fetch column 3,12,14 based on the condition that column 3 starts with 40464 this is the sample data how to achieve that (3 Replies)
Discussion started by: aemunathan
3 Replies
Login or Register to Ask a Question