Question: How to copy one column from many files to a new file?
gravatar for Seq225
27 days ago by
Seq22590 wrote:


I have 200 files. Each has 2 columns. I want to copy the first column from each of the 200 files into a new file. The new file will have 200 columns. How to do this (using awk/cut/paste etc). I have tried awk, but putting it in a for loop is difficult and I have failed to do so.

Thank you for your help!!

sequence assembly genome • 153 views
ADD COMMENTlink modified 27 days ago by bioinformatics2020340 • written 27 days ago by Seq22590

Take inspiration from here:

ADD REPLYlink written 27 days ago by genomax87k

It would help if you could provide a sample. Is it a plain text file, does it contain non-ascii characters? Are there headers that you want to preserve? What is the separator between columns? Are there spaces in the entries?

ADD REPLYlink written 26 days ago by user_without_id140
gravatar for bioinformatics2020
27 days ago by
bioinformatics2020340 wrote:

I know you wanted an awk/cut solution, but this Rscript does the trick. Save it to a folder as script.R that has the 200 txt files and it'll automatically merge them and save it as "merged.txt"

Rscript script.R

if(!require("tidyverse")) install.packages("tidyverse")
files <- list.files(pattern = ".txt")
files <- purrr::map(files, ~ readr::read_tsv(.x, col_names = FALSE)) 
files <- purrr::map(files, ~ .x[1])
files <- dplyr::bind_cols(files)
readr::write_csv(files, path = "merged.txt", col_names = FALSE)
ADD COMMENTlink modified 12 days ago • written 27 days ago by bioinformatics2020340

Thanks a ton! It works!!

ADD REPLYlink written 13 days ago by Seq22590

Seq225 : Besides thanking posters, please accept answers (green check mark) to validate them and provide closure to the thread. Do this for your past questions as well. You can accept more than one answer if they work.

ADD REPLYlink written 13 days ago by genomax87k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1358 users visited in the last hour