I'll join the other advice that you shouldn't parse the output of ls, so this is a bad example. But as a more general matter, I would include the awk script directly in the shell script by passing it as an argument to awk.
#!/bin/bash
ls -lhF "$@" | awk '
( NF >= 9 ) {
print "|-- [" $5 "] " $9
}'
Note that if the awk script must include the ' (single quote) character, you need to quote it: use '\'' (close single quote, literal single quote, open single quote).
To avoid having to quote, you can use a here document instead. But it's awkward because you can't use standard input both to feed input to awk and to feed the script. You need to use an additional file descriptor (see When would you use an additional file descriptor? http://unix.stackexchange.com/questions/13724/file-descriptors-shell-scripting).
#!/bin/bash
ls -lhF "$@" | awk -f /dev/fd/3 3<<'EOF'
( NF >= 9 ) {
print "|-- [" $5 "] " $9
}
EOF
Inside awk, you can read input from another command using the getline function and the pipe construct. It's not the way awk is primarily designed to be used, but it can be made to work. You need to quote the file name arguments for the underlying shell, which is highly error-prone. And since the text to be processed doesn't come from the expected sources (standard input or the files named on the command line), you end up with all the code in the BEGIN block.
#!/usr/bin/awk -f
BEGIN {
command = "ls -lhF"
for (i = 1; i <= ARGC; i++) {
arg = ARGV[i];
gsub("'", "'\\''", arg);
command = command " '" arg "'";
}
ARGC = 0; for (i in ARGV) delete ARGV[i];
while ((command | getline) > 0) {
if (NF >= 9) { print "|-- [" $5 "] " $9 }
}
}
In short, use a shell for what it's good at (such as piping commands together), and awk for what it's good at (such as text processing).
lsoutput for your own good. That is almost never the right approach for most tasks. I would suggest telling us instead what you hope to accomplish, so others may suggest better approaches. – jw013 Jul 16 '12 at 1:31lsin for loops etc. I don't see why I shouldn't useawkto parse it. AFAIKlsgives a clean output, with fields separated by whitespaces. And I think thatlesswas created to parse exactly that kind of data. – westeros91 Jul 16 '12 at 1:36lsdoesn't display filenames reliably when strange characters are present. There's also possible inconsistencies with the time format. There is no reliable way to parselsoutput - it doesn't matter what you try to use. Awk does not have special magic that allows it to parsels. Finally,lessis just a pager - it doesn't parse anything. – jw013 Jul 16 '12 at 1:47lswrapper. I have no intention of working with files based on the output ofls. I merely want to show the output in a different form. I still don't see how special characters would cause a problem to that. And I don't know why I mentionedlessin my earlier comment (and I can't edit it now :P). I meantawkalright. github.com/trapd00r/ls-- parseslsuses Perl. – westeros91 Jul 16 '12 at 1:56tree. Most distributions have a package for it. – jw013 Jul 16 '12 at 12:48