Bash to rename portion of file using match to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to rename portion of file using match to another
# 1  
Old 10-14-2019
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 rename is not working. Thank you Smilie.

Code:
if ! [[ " ${id[*]} " == *\ $file\ * ]]
      then
          for id in $file ; do
           cd "$dir"
           rename s/"IonCode_[0-9][0-9][0-9][0-9]_"/""/g "$id"_test.bam      ## use perl rename to remove barcode and insert $id_test
           rename s/"IonCode_[0-9][0-9][0-9][0-9]_"/""/g "$id"_test.bam.bai  ## use perl rename to remove barcode and insert $id_test
           done
             break
          end
      fi

$id

Code:
IonCode_0404 00-0000-xxx-xxx-xxx IonCode_0402 11-1111-yy-yy-yyy

$file
Code:
IonCode_0402_xxx.xxx_xxx.bam
IonCode_0402_xxx.xxx_xxx.bam.bai
IonCode_0404_xxx.xxx_xxx.bam
IonCode_0404_xxx.xxx_xxx.bam.bai

desired in $dir
Code:
11-1111-yy-yy-yyy_test.bam
11-1111-yy-yy-yyy_test.bam.bai
00-0000-xxx-xxx-xxx_test.bam
00-0000-xxx-xxx-xxx_test.bam.bai


Last edited by cmccabe; 10-15-2019 at 07:28 AM..
# 2  
Old 10-15-2019
May I ask why you are doing in this in bash with a PERL rename?

Why not use a normal REGEX in PERL, python or PHP?

You are using bash with rename because ... ?
This User Gave Thanks to Neo For This Post:
# 3  
Old 10-15-2019
I do not know python well enough to write a script. php I have not used but have used bash with rename before. There probably is a better way but this was my attempt and I wanted to make a resonable attempt with what I have learned. Thank you Smilie.

Last edited by cmccabe; 10-15-2019 at 07:34 AM..
# 4  
Old 10-18-2019
I am going to clean this up and reformat and will repost. Thank you Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename file using partial match to another

In the below I am trying to rename the contents within each data subfolder in a specific run, based on a partial match of the IonCode_0000_ in each file in the data subdirectory to $1 in f1. There will be multiple runs in f1 but each run in $uniq is unique and will be found in f1 and the rename... (27 Replies)
Discussion started by: cmccabe
27 Replies

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

3. Shell Programming and Scripting

Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you :). ... (12 Replies)
Discussion started by: cmccabe
12 Replies

4. Shell Programming and Scripting

Bash to rename file after second occurence of underscore

I am trying to use bash to remove the text in all filenames after the second _ in specific files that end in .bam or .vcf. However the bash errors looking for the files in the directory. Thank you :). files in directory ... (4 Replies)
Discussion started by: cmccabe
4 Replies

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

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

7. Shell Programming and Scripting

BASH function to rename file to last mondays date

I'm trying to create a bash function that will allow me to rename a file name emails.txt to last monday's date renem () { d=`date -d last-monday` mv ~/emails.txt ~/myemails/$d } but i en up wit this error any suggestion mv: target `2014' is not a directory (3 Replies)
Discussion started by: robshal
3 Replies

8. Shell Programming and Scripting

Rename portion of file based on another file

Hello, I've been searching and reading, but I can't figure out how to solve this problem with my newbie skills. In my directory, I have a list of files (see dirlist.txt attachment) that I need to merge and rename. I have part of the code of the code figured out (see below). However, I... (3 Replies)
Discussion started by: anjulka
3 Replies

9. Shell Programming and Scripting

mv command to rename multiple files that retain some portion of the original file nam

Well the title is not too good, so I will explain. I need to move (rename) files using a simple AIX script. ???file1.txt ???file2.txt ???file1a.txt ???file2a.txt to be: ???renamedfile1'date'.txt ???renamedfile2'date'.txt ???renamedfile1a'date'.txt ???renamedfile2a'date'.txt ... (4 Replies)
Discussion started by: grimace15
4 Replies

10. Shell Programming and Scripting

Match portion of the filename

hi, I have a script which accept filename and process them, however, one of the file needs 'special' handling so I need to identify it, say the filename contains the word "STOCK" (i.e. NEWYORKSTOCKLIST20060627.txt), I want to check if the filename contains the word "STOCK", how can I do that?... (1 Reply)
Discussion started by: mpang_
1 Replies
Login or Register to Ask a Question