JM88
JM88

Reputation: 477

Executing script on files and saving modifications with the same file names

I have this script in bash that want to execute it on different text files and then save this modifications on the files with the same name.

Here is the script:

#!/bin/bash

awk '{if (d) d=d RS $0; else d=$0}
/>/{s++;next}
s==1 && /[ACGT]/{gsub(/[^ACGT]+/, ""); n+=length($0)} 
END{print s, n, "1" RS d}' < mytextfiles_*.txt > mytexfiles_withmodifications_*.txt

Thanks in advance.

Upvotes: 0

Views: 63

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207758

Something like this, but backup first!

#!/bin/bash
for i in *.txt
do
    echo Processing ${i}
    awk .... "${i}" > $$.tmp && mv $$.tmp "${i}"
done

You may want to select other than "*.txt". Put your own awk stuff in where I have "..."

Explanation: It loops through all the "txt" files in your current directory. It runs your awk command on each one and saves the output in a temporary file (name formed using ProcessId.tmp). If the awk is successful, it renames the temporary file the same as the original - thereby overwriting it. Note that permissions will not be preserved.

Upvotes: 1

Related Questions