1

I've tried invoking the script below with the line awk 'filecount'. It should print out the file information and a line number, followed by the total number of files in the directory, but nothing happens.

What's wrong with this script?

#!/usr/bin/awk

BEGIN { 
    files=0;
}
{
    files++;
    print files, "\t", $9, "\n", $3, "\n\n";
}
END {
    print "Total Files:  ", files, "\n\n"
}
  • What information about the files do you want to print? I don't know awk, but probably there will be an alternative solution. – Byte Commander Jul 14 '16 at 16:37
  • 1
    Where is it supposed to get this file information from? My guess is you want it to parse the output of an ls -l command. Also note that the shebang for an awk script needs to be followed by -f in order for it to read commands from the remainder of the file #!/usr/bin/awk -f – steeldriver Jul 14 '16 at 16:40
  • Thanks. Yes, I want to pipe output into it with ls -l. And that shebang might help a lot. I'll certainly try it. – ThomasLMahoney Jul 16 '16 at 14:20
0

Issues with original script

There's a few problems with your script. First of all, the first line should be #!/usr/bin/awk -f. Second you said,

It should print out the file information and a line number

but in your script this part counts line number instead of file count, which is also redundant , because AWK has NR and FNR variables that already do that:

{
    files++;
    print files, "\t", $9, "\n", $3, "\n\n";
}

Proper working approach

As far as I understand, your main requirement is to print line count in each file and print total processed files in current directory. This can be easily done with bash on command line:

$ counter=0; for file in * ; do [ -f "$file" ] && [ -r "$file"  ] &&  { wc -l "$file"; counter=$(($counter+1)); }; done ; echo "Total files:" "$counter"
45 center_active_window.py
41 get_terminal.py
91 typescript
1 xprop.txt
17 xrandr_script.py
Total files: 5

If you insist on doing this with awk , it's possible as well , but pay attention - all files in the directory should be regular text files and readable.

Below is the script and its sample run:

$ cat ~/filecount.awk                                                                                                    
#!/usr/bin/awk -f

FNR==1{

    if (FNR!=NR){
        print counter,last_file;
    }
    filecounter++;
    last_file=ARGV[ARGIND];
    counter=0
};

{counter++};

END{
    print last_file,counter;    
    print "Files processed: ", filecounter;
}

$ ~/filecount.awk *  2> /dev/null                                                                                        
45 center_active_window.py
41 get_terminal.py
91 typescript
1 xprop.txt
xrandr_script.py 17
Files processed:  5

The idea here is simple:

  • every time line number in file ( the FNR variable ) is 1 , we record filename using ARGV array and ARGIND variable. We increment file counter, and set line number to 0;

  • if the record number isn't the same as record number in current file, that means we've already processed at least one file, so we can print out the information about it. That's what the if (FNR!=NR){ . . . part does

  • Essentially we're processing file, and then looking backwards at its info.

  • Once we're through all files, we need to print information about last one (because remember, the script works by looking backwards at all items) , and then print the total file counter.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.