Rename file using partial match to another


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename file using partial match to another
# 22  
Hmm, i am using ubuntu and centos. Thank you Smilie.
# 23  
OK I've found the issue - you have a folder that wasn't in the f1 file and so from and to were not populated this should correct it:

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
     [ -z "$to" ] ||
     (
       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

This User Gave Thanks to Chubler_XL For This Post:
# 24  
Here is the output, is the mv without the echo still using the missing file? Thank you very much Smilie.

Code:
echo mv

Code:
Rename from:IonCode_0404 to:00-0000-xxx-xxx-xxx IonCode_0402 11-1111-yy-yy-yyy
mv IonCode_0404_xxx.xxx_xxx.bam 00-0000-xxx-xxx-xxx IonCode_0402 11-1111-yy-yy-yyy_RNA.bam
mv IonCode_0404_xxx.xxx_xxx.bam.bai 00-0000-xxx-xxx-xxx IonCode_0402 11-1111-yy-yy-yyy_RNA.bam.bai


mv
Code:
00-0000-xxx-xxx-xxx IonCode_0402 11-1111-yy-yy-yyy_RNA.bam.bai
00-0000-xxx-xxx-xxx IonCode_0402 11-1111-yy-yy-yyy_RNA.bam
IonCode_0402_xxx.xxx_xxx.bam
IonCode_0402_xxx.xxx_xxx.bam.bai

# 25  
OK we are getting there I think.

What do you get if you run this script:

Code:
dir=/path/to/run/
awk -F '\n' -v RS="" '
{
    d=split($0, val);
    for(i=1;i<d;i++) print val[i];
}' "$dir"/f1

Here I'm getting:
Code:
IonCode_0404 00-0000-xxx-xxx-xxx
IonCode_0402 11-1111-yy-yy-yyy
IonCode_0402 22-2222-zz-zzzz-zzz
IonCode_0404 10-0000-aa-aa-aa
IonCode_0412 55-1111-bb-bbb-bbb

Any difference if you try:
Code:
dir=/path/to/run/
awk -F $'\n' -v RS="" '
{
    d=split($0, val);
    for(i=1;i<d;i++) print val[i];
}' "$dir"/f1

or
Code:
dir=/path/to/run/
awk -F '\n' -v RS="" '
{
    d=split($0, val, "\n");
    for(i=1;i<d;i++) print val[i];
}' "$dir"/f1

This User Gave Thanks to Chubler_XL For This Post:
# 26  
Here is the output as well a fourth and a commented code that I hope is close to correct and makes sense. Thank you Smilie.

Is each line in uniq being stored in the split d=split($0, val, " ");

Code:
run_dir=$1  # define run
awk -F '\n' -v RS="" '
{
    d=split($0, val);
    for(i=1;i<d;i++) print val[i];
}' "$run_dir"/f1

output:
Code:
IonCode_0404 00-0000-xxx-xxx-xxx
IonCode_0402 11-1111-yy-yy-yyy
IonCode_0402 22-2222-zz-zzzz-zzz
IonCode_0404 10-0000-aa-aa-aa
IonCode_0412 55-1111-bb-bbb-bbb

Code:
run_dir=$1  # define run
awk -F $'\n' -v RS="" '
{
    d=split($0, val);
    for(i=1;i<d;i++) print val[i];
}' "$run_dir"/f1

output:
Code:
IonCode_0404 00-0000-xxx-xxx-xxx
IonCode_0402 11-1111-yy-yy-yyy
IonCode_0402 22-2222-zz-zzzz-zzz
IonCode_0404 10-0000-aa-aa-aa
IonCode_0412 55-1111-bb-bbb-bbb

Code:
run_dir=$1  # define run
awk -F '\n' -v RS="" '
{
    d=split($0, val, "\n");
    for(i=1;i<d;i++) print val[i];
}' "$run_dir"/f1

output:
Code:
IonCode_0404 00-0000-xxx-xxx-xxx
IonCode_0402 11-1111-yy-yy-yyy
IonCode_0402 22-2222-zz-zzzz-zzz
IonCode_0404 10-0000-aa-aa-aa
IonCode_0412 55-1111-bb-bbb-bbb

Code:
run_dir=$1  # define run
awk -F $'\n' -v RS="" '
{
    d=split($0, val, " ");
    for(i=1;i<d;i++) print val[i];
}' "$run_dir"/f1

output:
Code:
IonCode_0404
00-0000-xxx-xxx-xxx
IonCode_0402
11-1111-yy-yy-yyy
IonCode_0402
22-2222-zz-zzzz-zzz
IonCode_0404
10-0000-aa-aa-aa
IonCode_0412
55-1111-bb-bbb-bbb

commented code
Code:
#!/bin/bash

run_dir=$1  # define run
for run in "$run_dir" ; do  ## operate on desired directory
   uniq=${run_dir##*/}  ## store run with no path as uniq
while read from to   ## start from to loop
  do
     [ -z "$to" ] ||  ## if not in $to do nothing
     (
       cd "$run_dir"/RNA_BAM  ## define from
       echo "Rename from:$from to:$to"  ## for testing
       for file in *.bam*  ## start loop
       do
          newname=${file/$from*.bam/${to}_RNA.bam}  ## rename from:to adding text
          [ -f "$file" ] && [ "$newname" != "$file" ] && echo mv "$file" "$newname"  ## if bam matches to and is not renamed then do so, add echo before mv to test
       done  ## close loop
     )
  done <<<$(  ## start here loop and close loop
     awk -F $'\n' -v RS="" -v ref="$uniq" ' ## use newline, space, and barcode to get unique run
         $0 ~ ref {  ## search all lines
             d=split($0, val, " "); ## split value in unique run using space and store in val
             for(i=1;i<d;i++) print val[i];  ## loop through values and print
          }' "$run_dir"/f1  ## define $to
  )  ## loop through f1 for unique run and populate from and to
done  ## end processing


Last edited by cmccabe; 10-30-2019 at 10:42 AM.. Reason: added details
# 27  
I'm really confused why this is not working for you. Let's get rid of the here-string and pipe awk into the read:

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

  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 |
  while read from to
  do
     [ -z "$to" ] ||
     (
       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
done

This User Gave Thanks to Chubler_XL For This Post:
# 28  
Thank you for all your help, works perfect Smilie.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #423
Difficulty: Medium
C# is a general-purpose, multi-paradigm programming language encompassing strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.
True or False?

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