Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

running the system command ls-l in an awk script

Status
Not open for further replies.

Musquo

Technical User
Oct 29, 2002
2
US
What I am trying to do is get a number of directory files in my current directory.

Here is a copy of my script and output

BEGIN {
print "The following are directory files"
}
{
system( "ls-l " )
}
END {
print "There were " NR " directories in this list."
}
What hapens is the BEGIN prints out the sstatement. Then the script is waiting for some input and I have to doa CTR-D to stop the script from running

Thanks for your help
 
Do you need to use awk? A shell command to do the same job would be something like

echo "There were`ls -l|wc -l|tr -s \" \"` directories in this list"

Bear in mind ls -l|wc -l will always report 1 more file than there actually is because of the header line.

Greg.
 
Yes I do need to use AWK because I am studying that in my UNIX I class.

Thanks
 

awk '
function dirlist() {
while (("ls -l" | getline array[a++]) > 0) {
}
for (x=1 ; x <= a ; x++) {
print array[x]
}
}

BEGIN {
dirlist()
}'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top