Reputation: 101
I would like to use the ls -l file_path|cut -d ' ' -f 1 in awk script.I need the rights of a file and than use it as an index for an array.How can I do that? For example: if the output of the command is this : -rw-rw-r-- Than I would like to be able to do something like this: Array[-rw-rw-r--]++ I tried to do this:
awk '{ system("ls -l " FILENAME "|cut -d ' ' -f 1") }' `find $1 -type f`
to get the rights but it doesn't work.
Upvotes: 1
Views: 1045
Reputation: 203615
You would not write code like that as it's trying to use awk as a shell. It's like trying to dig a hole using a screwdriver while you're sitting in your backhoe. Instead you would write something like this with GNU tools:
find "$1" -type f -print0 | xargs -0 stat -c %A | awk '{arr[$0]++}'
or even:
find "." -type f -printf '%M\n' | awk '{arr[$0]++}'
Thanks to @CharlesDuffy for bug-fixes and inspiration in the comments.
Upvotes: 2
Reputation: 785196
Rather than using ls
and parsing it's output use stat
with getline
:
awk '{cmd="stat -c %A " FILENAME; cmd | getline perm; close(cmd); arr[perm]++}'
Upvotes: 1