Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6946979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:38:05+00:00 2026-05-27T13:38:05+00:00

I’m working on a home work assignment. The question is: Write an awk script

  • 0

I’m working on a home work assignment. The question is:

Write an awk script to select all regular files (not directories or
links) in /etc ending with .conf, sort the result by size from
smallest to largest, count the number of files, and print out the
number of files followed by the filenames and sizes in two columns.
Include a header row for the filenames and sizes. Paste both your
script and its output in the answer area.

I’m really struggling trying to get this to work through using awk. Here’s what I came up with.

 ls -lrS /etc/*.conf |wc –l 

will return the number 33 which is the number of files .conf files in the directory.

 ls -lrS /etc/*.conf |awk '{print "File_Size"": " $5 "   ""File_Name and Size"": " $9}'

this will make 2 columns with the name and size of the .conf file in the directory.

It works, but I don’t think it is what he’s looking for. I’m having an AWKful time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:38:06+00:00Added an answer on May 27, 2026 at 1:38 pm

    Let’s see here…

    select all regular files (not directories or links)

    So far you haven’t addressed this, but if you are piping in the output of ls -l..., this is easy, select on

    /^-/
    

    because directories start with d, symbolic links with l and so on. Only plain old files start with -. Now

    print out the number of files followed

    Well, counting matches is easy enough…

    BEGIN{count=0}  # This is not *necessary*, but I tend to put it in for clarity
    /^-/ {count++;}
    

    To get the filename and size, look at the output of ls -l and count up columns

    BEGIN{count=0}
    /^-/ {
      count++;
      SIZE=$5;
      FNAME=$9;
    }
    

    The big difficulty here is that awk doesn’t provide much by way of sorting primitives, so that’s the hard part. That can be beaten if you want to be clever but it is not particularly efficient (see the awful thing I did in a [code-golf] solution). The easy (and unixy) thing to do would be to pipe part of the output to sort, so…we collect a line for each file into a big string

    BEGIN{count=0}
    /^-/ {
      count++
      SIZE=$5;
      FNAME=$9;
      OUTPUT=sprintf("%10d\t%s\n%s",SIZE,FNAME,OUTPUT);
    }
    END{
       printf("%d files\n",count);
       printf("  SIZE    \tFILENAME"); # No newline here because OUTPUT has it
       print OUTPUT|"sort -n --key=1";
    }
    

    Gives output like

    11 files
      SIZE          FILENAME
           673      makefile
          2192      houghdata.cc
          2749      houghdata.hh
          6236      testhough.cc
          8751      fasthough.hh
         11886      fasthough.cc
         19270      HoughData.png
         60036      houghdata.o
        104680      testhough
        150292      testhough.o
        168588      fasthough.o
    

    (BTW–There is a test subdirectory here, and you’ll note that it does not appear in the output.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.