Running a code in batch mode on a list of files
1
0
Entering edit mode
4.3 years ago
zizigolu ★ 4.3k

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 GREP SED • 1.1k views
ADD COMMENT
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
2
Entering edit mode
4.3 years ago
zizigolu ★ 4.3k
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 COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY

Login before adding your answer.

Traffic: 2144 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6