Answers.Body: <p>Assuming the names of all the files from one group are in file named list.  </p>
  
  <pre><code>cat pre_post.list | awk '{print <span class="math-container">$2}' | sed 's@.*/@@'  &gt; post_list.txt
  cat pre_post.list | awk '{print $</span>1}' | sed 's@.*/@@'  &gt; pre_list.txt
  </code></pre>
  
  <p>Now you have two number list</p>
  
  <pre><code>    for item in  <span class="math-container">$(cat  pre_list.txt | grep -Eo '[0-9]{1,4}');
      do if grep -q $</span>item clin_name.txt ;
      then out=<span class="math-container">$(grep $</span>item clin_name.txt | awk  '{print <span class="math-container">$1}');
           cp *$</span>out* /dir/for_pre_files/
      else echo post_list $item;  fi;  done
  </code></pre>
  
  <p>for post</p>
  
  <pre><code>    for item in  <span class="math-container">$(cat  post_list.txt | grep -Eo '[0-9]{1,4}');
      do if grep -q $</span>item clin_name.txt ;
      then out=<span class="math-container">$(grep $</span>item clin_name.txt | awk  '{print <span class="math-container">$1}');
           cp *$</span>out* /dir/for_post_file/
      else echo Prelist $item;  fi;  done
  </code></pre>
  
  <p>or you could do this if one that isn't in post belongs in pre</p>
  
  <pre><code>    for item in  <span class="math-container">$(cat  post_list.txt | grep -Eo '[0-9]{1,4}');
      do if grep -q $</span>item clin_name.txt ;
      then out=<span class="math-container">$(grep $</span>item clin_name.txt | awk  '{print <span class="math-container">$1}');
           cp *$</span>out* /dir/for_post_file/
      else cp *$out* /dir/for_pre_file/;  fi;  done
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-18T11:11:33.740Z
Answers.Text: Sorry I tried your kindly code but did not work and I got this error in very  early stage  [fi1d18@cyan01 ~]$ cat pre_post.list | awk '{print $2}' | sed 's@.*/@@'  > post_list.txt
  cat: pre_post.list: No such file or directory
Answers.UserDisplayName: Zizogolu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-18T14:23:11.043Z
Answers.Text: You have to change the names in my bash code to reflect the names of the actual files on my computer
Answers.UserDisplayName: Michael Hearn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-22T17:01:33.947Z
Answers.Text: Thank you I am getting this the for loop  grep: clin_name.txt: No such file or directory
  post_list 069 this being repeated for all of samples
Answers.UserDisplayName: Zizogolu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-22T17:04:21.757Z
Answers.Text: You need to replace all the file names in the script with your file names.
Answers.UserDisplayName: Michael Hearn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-04T16:18:06.007Z
Answers.Text: Sorry what is "clin_name.txt" here? I mean where clin_name.txt comes from?  If I know what clin_name.txt is, I might be able to adopt that to my case Thank you
Answers.UserDisplayName: Zizogolu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-04T16:51:08.937Z
Answers.Text: file that contains all OC/AH/120/E/T3  LP6005409-DNA_F01
  OC/AH/126/E/T1  LP2000325-DNA_A01
  OC/AH/136/E/T3  LP6005409-DNA_E02
Answers.UserDisplayName: Michael Hearn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-05T16:36:23.060Z
Answers.Text: Hello @Michael Hearn I tried what you kindly wrote for me edited in my main post but I am getting errors like cp: missing destination file operand after `/temp/hgig/fi1d18/TRG45/snp/pre/'
Answers.UserDisplayName: Zizogolu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-13T06:32:11.597Z
Answers.LastActivityDate: 2019-11-13T06:32:11.597Z
Answers.UserDisplayName: Michael Hearn
Body: <blockquote>
    <p>EDITED</p>
  </blockquote>
  
  <p>I have list of my samples in first column and corresponding file name for my .vcf files in second column of a file name <code>clin_name.txt</code> like below. For example name of one vcf is <code>LP6008031-DNA_A04_vs_LP6008032-DNA_D03.snp.pass.vcf</code></p>
  
  <pre><code>OC/AH/120/E/T3  LP6005409-DNA_F01
  OC/AH/126/E/T1  LP2000325-DNA_A01
  OC/AH/136/E/T3  LP6005409-DNA_E02
  </code></pre>
  
  <p>This is the link of my file <a href="https://www.dropbox.com/s/mvzf47feiqe7x69/clin_name.txt.txt?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/mvzf47feiqe7x69/clin_name.txt.txt?dl=0</a></p>
  
  <p>In the second file name <code>pre_post.list</code> I have information of pre or post treatment for each sample like this</p>
  
  <pre><code>pre-treated post-treated
  OC/AH/120   OC/AH/065
  OC/AH/126   OC/AH/069
  OC/AH/136   OC/AH/074
  OC/AH/160   OC/AH/084
  </code></pre>
  
  <p>This is link of this <a href="https://www.dropbox.com/s/ikrl1h8yoc0sngx/pre_post.list.xlsx?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/ikrl1h8yoc0sngx/pre_post.list.xlsx?dl=0</a></p>
  
  <p>As you suggested I typed </p>
  
  <pre><code>cat pre_post.list | awk '{print <span class="math-container">$2}' | sed 's@.*/@@'  &gt; post_list.txt
  cat pre_post.list | awk '{print $</span>1}' | sed 's@.*/@@'  &gt; pre_list.txt
  </code></pre>
  
  <p>Then I have </p>
  
  <pre><code>pre-treated
  120
  126
  136
  160
  182
  
  post-treated
  065
  069
  074
  084
  </code></pre>
  
  <p>in I typed <code>./test.sh</code> in which I wrote</p>
  
  <pre><code>for item in  <span class="math-container">$(cat  pre_list.txt | grep -Eo '[0-9]{1,4}');
      do if grep -q $</span>item clin_name.txt ;
      then out=<span class="math-container">$(grep $</span>item clin_name.txt | awk  '{print <span class="math-container">$1}');
           cp *$</span>out* /temp/hgig/fi1d18/TRG45/snp/
      else echo post_list $item;  fi;  done;
  </code></pre>
  
  <p>That returns</p>
  
  <pre><code>cp: cannot stat `*OC/AH/069/S/T3*': No such file or directory
  [fi1d18@cyan01 snp]$
  </code></pre>
  
  <p>Or I run </p>
  
  <pre><code>for item in  <span class="math-container">$(cat  post_list.txt | grep -Eo '[0-9]{1,4}');
      do if grep -q $</span>item clin_name.txt ;
      then out=<span class="math-container">$(grep $</span>item clin_name.txt | awk  '{print <span class="math-container">$1}');
           cp *$</span>out* /temp/hgig/fi1d18/TRG45/snp/pre/
      else cp *$out* /temp/hgig/fi1d18/TRG45/snp/post/;  fi;  done;
  </code></pre>
  
  <p>That returns</p>
  
  <pre><code>cp: cannot stat `OC/ST/026/S/T1*': No such file or directory
  [fi1d18@cyan01 snp]$
  </code></pre>
  
  <p>I really don't know how to solve this :(</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-12T12:30:16.620Z
Comments.Text: This is more of a job for snakemake or similar. You'd end up following the same logic in R if you really prefer that. Show us what you've tried so far, since there's no reason someone should write a custom program for you.
Comments.UserDisplayName: Devon Ryan
Tags: <r><bash><looper>
Title: How I move these files based on their names in R or terminal
ThreadText: # How I move these files based on their names in R or terminal
  
  (Asked by: username_0 on 2019-11-12)
  
  > EDITED
  
  I have list of my samples in first column and corresponding file name for my .vcf files in second column of a file name `clin_name.txt` like below. For example name of one vcf is `LP6008031-DNA_A04_vs_LP6008032-DNA_D03.snp.pass.vcf`
  
  ```
  OC/AH/120/E/T3  LP6005409-DNA_F01
  OC/AH/126/E/T1  LP2000325-DNA_A01
  OC/AH/136/E/T3  LP6005409-DNA_E02
  
  ```
  
  This is the link of my file <https://www.dropbox.com/s/mvzf47feiqe7x69/clin_name.txt.txt?dl=0>
  
  In the second file name `pre_post.list` I have information of pre or post treatment for each sample like this
  
  ```
  pre-treated post-treated
  OC/AH/120   OC/AH/065
  OC/AH/126   OC/AH/069
  OC/AH/136   OC/AH/074
  OC/AH/160   OC/AH/084
  
  ```
  
  This is link of this <https://www.dropbox.com/s/ikrl1h8yoc0sngx/pre_post.list.xlsx?dl=0>
  
  As you suggested I typed
  
  ```
  cat pre_post.list | awk '{print $2}' | sed 's@.*/@@'  > post_list.txt
  cat pre_post.list | awk '{print $1}' | sed 's@.*/@@'  > pre_list.txt
  
  ```
  
  Then I have
  
  ```
  pre-treated
  120
  126
  136
  160
  182
  
  post-treated
  065
  069
  074
  084
  
  ```
  
  in I typed `./test.sh` in which I wrote
  
  ```
  for item in  $(cat  pre_list.txt | grep -Eo '[0-9]{1,4}');
      do if grep -q $item clin_name.txt ;
      then out=$(grep $item clin_name.txt | awk  '{print $1}');
           cp *$out* /temp/hgig/fi1d18/TRG45/snp/
      else echo post_list $item;  fi;  done;
  
  ```
  
  That returns
  
  ```
  cp: cannot stat `*OC/AH/069/S/T3*': No such file or directory
  [fi1d18@cyan01 snp]$
  
  ```
  
  Or I run
  
  ```
  for item in  $(cat  post_list.txt | grep -Eo '[0-9]{1,4}');
      do if grep -q $item clin_name.txt ;
      then out=$(grep $item clin_name.txt | awk  '{print $1}');
           cp *$out* /temp/hgig/fi1d18/TRG45/snp/pre/
      else cp *$out* /temp/hgig/fi1d18/TRG45/snp/post/;  fi;  done;
  
  ```
  
  That returns
  
  ```
  cp: cannot stat `OC/ST/026/S/T1*': No such file or directory
  [fi1d18@cyan01 snp]$
  
  ```
  
  I really don't know how to solve this :(
  
  **Tags:** r, bash, looper
  
  > **Comments:**
  > **username_1**: This is more of a job for snakemake or similar. You'd end up following the same logic in R if you really prefer that. Show us what you've tried so far, since there's no reason someone should write a custom program for you.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-11-13. Score: 1
  
  Assuming the names of all the files from one group are in file named list.
  
  ```
  cat pre_post.list | awk '{print $2}' | sed 's@.*/@@'  > post_list.txt
  cat pre_post.list | awk '{print $1}' | sed 's@.*/@@'  > pre_list.txt
  
  ```
  
  Now you have two number list
  
  ```
      for item in  $(cat  pre_list.txt | grep -Eo '[0-9]{1,4}');
      do if grep -q $item clin_name.txt ;
      then out=$(grep $item clin_name.txt | awk  '{print $1}');
           cp *$out* /dir/for_pre_files/
      else echo post_list $item;  fi;  done
  
  ```
  
  for post
  
  ```
      for item in  $(cat  post_list.txt | grep -Eo '[0-9]{1,4}');
      do if grep -q $item clin_name.txt ;
      then out=$(grep $item clin_name.txt | awk  '{print $1}');
           cp *$out* /dir/for_post_file/
      else echo Prelist $item;  fi;  done
  
  ```
  
  or you could do this if one that isn't in post belongs in pre
  
  ```
      for item in  $(cat  post_list.txt | grep -Eo '[0-9]{1,4}');
      do if grep -q $item clin_name.txt ;
      then out=$(grep $item clin_name.txt | awk  '{print $1}');
           cp *$out* /dir/for_post_file/
      else cp *$out* /dir/for_pre_file/;  fi;  done
  
  ```
  
  > **Comments:**
  > **username_0**: Sorry I tried your kindly code but did not work and I got this error in very  early stage  [fi1d18@cyan01 ~]$ cat pre_post.list | awk '{print $2}' | sed 's@.*/@@'  > post_list.txt cat: pre_post.list: No such file or directory  
  > **username_2**: You have to change the names in my bash code to reflect the names of the actual files on my computer  
  > **username_0**: Thank you I am getting this the for loop  grep: clin_name.txt: No such file or directory post_list 069 this being repeated for all of samples  
  > **username_2**: You need to replace all the file names in the script with your file names.  
  > **username_0**: Sorry what is "clin_name.txt" here? I mean where clin_name.txt comes from?  If I know what clin_name.txt is, I might be able to adopt that to my case Thank you  
  > **username_2**: file that contains all OC/AH/120/E/T3  LP6005409-DNA_F01 OC/AH/126/E/T1  LP2000325-DNA_A01 OC/AH/136/E/T3  LP6005409-DNA_E02  
  > **username_0**: Hello @username_2 I tried what you kindly wrote for me edited in my main post but I am getting errors like cp: missing destination file operand after `/temp/hgig/fi1d18/TRG45/snp/pre/'  
  
  ---
  
  