Question: (Closed) Create multiple files using awk
gravatar for Abdel
5 days ago by
Abdel20 wrote:

Hi, all

I use awk to extract rows from a text file:

awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file1.txt > new_file1.txt

But how can I use the same code for multiple files ?

I can use :

awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file1.txt > new_file1.txt
awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file2.txt > new_file2.txt
awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file3.txt > new_file3.txt
awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file4.txt > new_file4.txt
awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file5.txt > new_file5.txt
. . . .        .    .     .     .    .     .      .     .    .    .    .    .  
awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file29.txt > new_file29.txt

But there is some ways to do it automaticaly ? for example a loop or file.txt > new_file.txt.



bash sed awk grep • 84 views
ADD COMMENTlink modified 5 days ago by finswimmer8.8k • written 5 days ago by Abdel20

You already know how to do this since you've identified its a problem for a loop (or better yet a parallel process).

Go and look up how to construct loops in bash (hint: for i in {1..10}...) and then come back and show us what you've attempted/where you get stuck.

Be aware that this isn't really a bioinformatics problem and this thread might be closed.

ADD REPLYlink modified 5 days ago • written 5 days ago by jrj.healey9.7k

Thanks for reply. I am a biologist and beginer in unix and data analysis :). I asked the question to reduce the time of searching in pdf courses.


ADD REPLYlink written 5 days ago by Abdel20

Especially as a beginner you should invest quality time in solving and learning these tasks. Those are basics that you'll need everyday in bioinformatics. Start with simple examples and then get deeper into optimizing your code, making it run cleaner and faster to be able to process large datasets.

ADD REPLYlink written 5 days ago by ATpoint12k

You don’t need PDF courses, you just need to google your problem.

“How to write a bash loop” is all you need. Any of the answers in google will guide you. If you are still stuck, we will help you troubleshoot, but you should attempt the problem yourself first.

ADD REPLYlink written 5 days ago by jrj.healey9.7k

Hello Abdel!

We believe that this post does not fit the main topic of this site.

Not really bioinformatics

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.


ADD REPLYlink written 5 days ago by RamRS20k
gravatar for finswimmer
5 days ago by
finswimmer8.8k wrote:

There is a variable called FILENAME which provide the current filename awk reads in. You can take this to create a filename awk should print its output.

$ awk 'NR==FNR{vals[$1];next} ($1) in vals { print $0 >> "new_"FILENAME}' indiv.txt file1.txt file2.txt file3.txt

fin swimmer

ADD COMMENTlink written 5 days ago by finswimmer8.8k

Thank all for helping, and for advices.

ADD REPLYlink written 2 days ago by Abdel20
Please log in to add an answer.
The thread is closed. No new answers may be added.


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