text: user3766: I have a matrix file (expression.txt) in which the first column is a gene_id and from the second column the sample id's start. This matrix has 20,000 columns with sample id's corresponding to different tissue. I want to extract the samples of one tissue. Example:  SM-5GZZ7 SM-5GIEN SM-5EGGH are samples from brain tissue. So I want to separate the list of samples [(file=brain.txt;n=5000] corresponding to brain tissue from this matrix (with bash or R)
  Name    SM-5GZZ7    SM-5EGHI    SM-5EGHJ    SM-5N9CS    SM-5GIEN    SM-5EGHH    SM-5EGGH
  ENSG00000223972.5   0   0   0   0   0   0   0
  ENSG00000227232.5   187 109 143 251 113 139 199
  ENSG00000278267.1   0   0   1   0   0   0   0
  ENSG00000243485.5   1   0   0   1   0   0   0
  ENSG00000237613.2   0   0   0   0   0   0   0
  ENSG00000268020.3   0   1   0   0   0   1   1
  
  The file brain.txt looks like (Number of lines in this files = 5000)
  SM-5GZZ7
  SM-5GIEN 
  SM-5EGGH
  SM-5GZXC
  SM-5GZXO
  SM-5GZY1
  SM-5EGHK
  SM-5EGHL
  
  
  user7153: There's a million different ways to do simple taks like this. Pick one and learn it:) Python is my go to as it can do pretty much everything you'd ever want. I saved your example as csv with no spaces called expression.txt.
  import pandas as pd
  df = pd.read_csv('expression.txt')
  df[['SM-5GZZ7','SM-5GIEN','SM-5EGGH']]
  
  SM-5GZZ7    SM-5GIEN    SM-5EGGH
  0   0   0.0 0.0
  1   187 109 143 251 113 139 199 NaN NaN
  2   0   0.0 0.0
  3   1   0.0 0.0
  4   0   0.0 0.0
  5   0   0.0 1.0
  
  
  user3694: Example in R. If you have a file of column names, names.txt, such as:
  mpg 
  cyl  
  disp
  
  and a data.frame like mtcars
  > mtcars
                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
  Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
  Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
  Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
  Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
  Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
  Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
  Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
  Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
  Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
   [ reached 'max' / getOption("max.print") -- omitted 23 rows ]
  
  Then you can select the columns by:
  library(dplyr)
  list_names = read.table("names.txt", header=F) %>% dplyr::select(V1)
  > mtcars %>% dplyr::select(list_names$V1)
                       mpg cyl  disp
  Mazda RX4           21.0   6 160.0
  Mazda RX4 Wag       21.0   6 160.0
  Datsun 710          22.8   4 108.0
  Hornet 4 Drive      21.4   6 258.0
  Hornet Sportabout   18.7   8 360.0
  Valiant             18.1   6 225.0
  Duster 360          14.3   8 360.0
  Merc 240D           24.4   4 146.7
  
  
original_text: 3766: <p>I have a matrix file (expression.txt) in which the first column is a gene_id and from the second column the sample id's start. This matrix has 20,000 columns with sample id's corresponding to different tissue. I want to extract the samples of one tissue. Example:  SM-5GZZ7 SM-5GIEN SM-5EGGH are samples from brain tissue. So I want to separate the list of samples [(file=brain.txt;n=5000] corresponding to brain tissue from this matrix (with bash or R)</p>
  <pre><code>Name    SM-5GZZ7    SM-5EGHI    SM-5EGHJ    SM-5N9CS    SM-5GIEN    SM-5EGHH    SM-5EGGH
  ENSG00000223972.5   0   0   0   0   0   0   0
  ENSG00000227232.5   187 109 143 251 113 139 199
  ENSG00000278267.1   0   0   1   0   0   0   0
  ENSG00000243485.5   1   0   0   1   0   0   0
  ENSG00000237613.2   0   0   0   0   0   0   0
  ENSG00000268020.3   0   1   0   0   0   1   1
  </code></pre>
  <p>The file brain.txt looks like (Number of lines in this files = 5000)</p>
  <pre><code>SM-5GZZ7
  SM-5GIEN 
  SM-5EGGH
  SM-5GZXC
  SM-5GZXO
  SM-5GZY1
  SM-5EGHK
  SM-5EGHL
  </code></pre>
   7153: <p>There's a million different ways to do simple taks like this. Pick one and learn it:) Python is my go to as it can do pretty much everything you'd ever want. I saved your example as csv with no spaces called expression.txt.</p>
  <pre><code>import pandas as pd
  df = pd.read_csv('expression.txt')
  df[['SM-5GZZ7','SM-5GIEN','SM-5EGGH']]
  
  SM-5GZZ7    SM-5GIEN    SM-5EGGH
  0   0   0.0 0.0
  1   187 109 143 251 113 139 199 NaN NaN
  2   0   0.0 0.0
  3   1   0.0 0.0
  4   0   0.0 0.0
  5   0   0.0 1.0
  </code></pre>
   3694: <p>Example in R. If you have a file of column names, <code>names.txt</code>, such as:</p>
  <pre><code>mpg 
  cyl  
  disp
  </code></pre>
  <p>and a data.frame like <code>mtcars</code></p>
  <pre><code>&gt; mtcars
                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
  Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
  Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
  Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
  Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
  Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
  Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
  Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
  Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
  Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
   [ reached 'max' / getOption(&quot;max.print&quot;) -- omitted 23 rows ]
  </code></pre>
  <p>Then you can select the columns by:</p>
  <pre><code>library(dplyr)
  list_names = read.table(&quot;names.txt&quot;, header=F) %&gt;% dplyr::select(V1)
  &gt; mtcars %&gt;% dplyr::select(list_names$V1)
                       mpg cyl  disp
  Mazda RX4           21.0   6 160.0
  Mazda RX4 Wag       21.0   6 160.0
  Datsun 710          22.8   4 108.0
  Hornet 4 Drive      21.4   6 258.0
  Hornet Sportabout   18.7   8 360.0
  Valiant             18.1   6 225.0
  Duster 360          14.3   8 360.0
  Merc 240D           24.4   4 146.7
  </code></pre>
  