Search code examples
for-loopawkfindsubdirectory

How do I find files to edit and export to the same directory as a different file using Awk and Find in a for loop


A stupid question to ask however I don't seem to be able to find the answer through stackoverflow or other googled results. Either this question is too specific or too dumb. I think it's the later one...

I am using awk to filter files that are under different subdirectory and I want to output the results under the same subdirectory where the file is located.

I was playing with the find command and was able to awk the files in all the subdirectory that matches the pattern.

I find files ended with .tsv and then awk the 8th column (values that under -1) and then I output the file use the base of the file name and add .txt

for file in $(find $DIR2 -type f -name '*.tsv'); do
awk -F$'\t' '$8 < -1' $(find $DIR2 -type f -name '*.tsv') > $DIR2/$(basename ${file%.*}).txt; done

However, I am kind of stuck, not knowing how to output the files under the original subdirectory.

If I use $DIR2/ it only export under this specific directory and without the $DIR2/, it gets exported to the current working directory.


Solution

  • Try this (untested):

    find "$DIR2" -type f -name '*.tsv' -exec \
    awk -F'\t' 'FNR==1{close(out); out=FILENAME; sub(/[^.]+$/,"txt",out)} $8 < -1{print > out}' {} +
    

    If your find doesn't support + then replace it with \;.