Bash scripting FS use

Viewed 1320

I have a command which finds and prints the filenames, but I am having a problem understanding the part "FS=/ <(find)" . The full command is:

awk 'NR==FNR{a[$2];next}$NF in a' FS=, data.txt FS=/ <(find)

And the output is:

  user>./test.sh data.txt
./test1/file1 ./test2/file2 ./test3/file3
./test1/file1 ./test2/file2 ./test3/file3
./test1/file1 ./test2/file2 ./test3/file3

Why does it output every file 3 times? I want only one time, because there is only file1 file2 file3. And the content of data.txt is:

user>cat data.txt
dir1,file2,2018/07/04,2009/10/12 10:45,john
dir3,file1,2018/07/04,2005/08/07 13:57,john
dir3,file3,2018/07/04,2006/10/14 09:56,john
1 Answers

I could not reproduce the issue.

I created a directory which contains 3 subdirectories (test1, test2, test3) and 3 files (./test1/file1 ./test2/file2 ./test3/file3) + the data.txt file

find returns :

user> find
.
./test2
./test2/file2
./test1
./test1/file1
./test3
./test3/file3
./data.txt

I'll try to explain the awk command :

awk 'NR==FNR{a[$2];next}$NF in a' FS=, data.txt FS=/ <(find)

awk is reading from file data.txt and find output, using , as separator for data.txt and / separator for find output.

awk command is equivalent to :

awk 'NR==FNR{a[$2] = 1}NR!=FNR{if($NF in a){print $0}}' FS=, data.txt FS=/ <(find)

which means :

for rows in data.txt:
    store second column in array
for file (or dir) in current_directory:
    if name of file in array:
        print path of file

On my system, this command returns what I expected :

./test2/file2
./test1/file1
./test3/file3

May you add to your question the exact output of the find command ? I assume that the command does not return the files in the proper format on your system (1 line per file or directory)

Related