Awk to ls -l

Hi,
I want to send the result of awk to ls -l commande then I had an error:

df -g | grep sgbd | awk '{print $7}' -exec ls -l {} \; 
awk: 0602-533 Cannot find or open file -exec.

Thanks for help

This is find syntax.
Do you want to run ls -l on each of what is returned by
df -g | grep sgbd | awk '{print $7}'
?
Then consider xargs

df -g | awk '/sgbd/ {print $7}' | xargs ls -l

The xargs converts the stdin to arguments for ls -l
However this won't work with embedded spaces, and gives an error if nothing is in the stdin.
Very correct is a loop.

df -g | awk '/sgbd/ {print $7}' | while IFS= read -r line; do ls -l "$line"; done

ksh93 and bash and zsh also take a "process substitution":

while IFS= read -r line; do ls -l "$line"; done < <( df -g | awk '/sgbd/ {print $7}' )
1 Like

in which case xargs would have to read spaces if awk is printing one field while the field separator is space? And to protect xargs against a blank input you can use -r.

1 Like

Hi,
Thak you. The following worked nice:

df -g | awk '/sgbd/ {print $7}' | xargs ls -l

Regards.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.