Question: Running a code in batch mode on a list of files
0
gravatar for A
6 months ago by
A3.9k
A3.9k wrote:

Hi

I have a script which modifies a .txt file

awk 'BEGIN{FS=OFS="\t"}{split($1,a,"_"); split(a[3],b,"/"); print a[1],a[2],b[1],b[2],$0}' file.txt | awk 'BEGIN{FS=OFS="\t"}{split($18,a,";"); split(a[2],b,"="); print b[2],$0}' > out_file.txt

I have a bunch of .txt files in a folder

I want to run this script on my files and write the output file by the name of the original file

I have tried this but nothing happening

> for  i in `find . -maxdepth 1 -type f -iname "*.txt" awk 'BEGIN{FS=OFS="\t"}{split($1,a,"_"); split(a[3],b,"/"); print a[1],a[2],b[1],b[2],$0}'| awk 'BEGIN{FS=OFS="\t"}{split($18,a,";"); split(a[2],b,"="); print b[2],$0}'print > out}' *.txt
-bash: syntax error near unexpected token `|'
(base) user112$

Any help to achieve this?

awk sed grep • 263 views
ADD COMMENTlink modified 6 months ago • written 6 months ago by A3.9k

Try giving awk the -F (input field separator argument) - tab or space or comma or whatever it is. Try that for one of the files.

Also your echo | awk thing can be achieved using s=$(basename $i .txt) if you just wish to prune the .txt.

ADD REPLYlink modified 6 months ago • written 6 months ago by Ram32k

you seem to be wrinting everything into out. im not good with bashs quoting rules, so to be safe:

for i in $(find ..); do awk 'cmd' $f > ${f}_out.txt ; done

This is a rather complicated statement and you should create a script from it that you can (re)use in a loop and reuse it. Saves time in the end.

ADD REPLYlink written 6 months ago by Ido Tamir5.1k
2
gravatar for A
6 months ago by
A3.9k
A3.9k wrote:
for i in *.txt; do

    awk 'BEGIN{FS=OFS="\t"}{split($1,a,"_"); split(a[3],b,"/"); print a[1],a[2],b[1],b[2],$0}' $i | awk 'BEGIN{FS=OFS="\t"}{split($18,a,";"); split(a[2],b,"="); print b[2],$0}' > $i'_1'.txt
done
ADD COMMENTlink written 6 months ago by A3.9k

You should provide feedback to people that invest time in your question or people will just start ignoring your posts.

ADD REPLYlink written 6 months ago by Ram32k

Sorry I did not provide feedback because I don't understand this things and I provided the code from a bioinformatician in our department for future of somebody like me was faced the same problem

ADD REPLYlink written 6 months ago by A3.9k

Do you realize that the code by itself holds no value, because your question is not clear and what the answer does is super confusing?

Ideally, if you add your own answer, explain what it does in detail. You're treating the site like it's a personal repository of your code snippets, which it is not.

ADD REPLYlink written 6 months ago by Ram32k
Please log in to add an answer.

Help
Access

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