Thank all for helping, and for advices.
• 0 views
•
link
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.
Thanks!
Abdel
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
Thank all for helping, and for advices.
Log in to answer this question.
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.
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.
Abdel
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.
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.
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.
Cheers!