Reggie Kapros Reggie Kapros - 1 year ago 73
Bash Question

How can I use the cut command in awk?

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.

Answer Source

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]++}'