Awk: Centering Text Using Printf


Recommended Posts

Hello everyone, I have an assignment which requires me to center text using Printf in Awk, I tried googling for a bit but none seem to do what I need, could someone please enlighten me? Thank you!

Link to comment
https://www.neowin.net/forum/topic/499928-awk-centering-text-using-printf/
Share on other sites

awk '{ printf("%*s\n", ('${COLUMNS}' + length($1))/2, $1); }'

Where $1 is, of course, the stuff you want to center. This should work in bash, maybe in zsh... don't know about other shells.

Brief overview:

printf with a * takes the width as a parameter before the string

${COLUMNS} is the number of columns in the terminal according to the shell -- you can see that it is not part of the actual awk expression, but rather evaluated at the shell level (this is the reason it might only work in bash and zsh)

length($1) is the size of the variable you want to center

You then divide the sum of these two parts by 2 to get the text off-center by +<N> (awk right-justifies text by default)

The second parameter to printf is the actual text you want to center

Hope this helped... ;)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • "Can" is fine. I want the ability for it to "not." Actually, I would prefer vice versa. But it has to be optional, otherwise companies wouldn't be too happy.
    • ChatGPT can now connect to Outlook, Teams, Gmail, Google Drive, and other services by Pradeep Viswanathan Apart from regular consumers, ChatGPT is also growing fast among business users. In fact, OpenAI now has 3 million paying (Enterprise, Team, and Edu) business users, up from 2 million in February. During a live stream today targeted toward business users, OpenAI announced that ChatGPT can now connect to more external services to pull in real-time context and provide more useful responses for users. The following are some of the external connectors available in ChatGPT Deep Research for Plus, Pro, Team, Enterprise, and Edu users: Microsoft Outlook Microsoft Teams Microsoft SharePoint Dropbox Box Google Drive Gmail Liner In addition to the above, IT admins in organizations can now build custom ChatGPT connectors using the popular Model Context Protocol (MCP). These custom connectors will allow organizations to make use of the data available inside their proprietary systems and other apps within ChatGPT, alongside pre-built connectors. Today, the ChatGPT team also announced a new feature called record mode for ChatGPT Team users on macOS. The record mode feature will allow users to capture meeting audio and get meeting transcriptions, meeting action items, summaries, etc. ChatGPT record mode will also be available to Plus, Pro, Enterprise, and Edu users in the future. Kevin Weil, Chief Product Officer at OpenAI, tweeted the following regarding today’s launch: While ChatGPT's new connectors and features mark significant progress, it faces a formidable challenge in the enterprise market against Microsoft 365 Copilot, which enjoys native integration within the Microsoft 365 ecosystem.
    • I decided to give this a try and, wow. I like it. One of the most customizable browsers I've ever used I think, and it even has the classic menu bar. That's one of the reasons I've stuck with Firefox for so long. Not sure if I'll make this my main browser yet but it's looking good so far.
    • No messaging app from a large commercial entity is really encrypted to a point they are actually unable to access your content. If you trust whatsapp and friends to do this either you are a fool.
    • From my point of view this seems like a good thing? Option to pay as you go in an otherwise Subscription app? Yes Please! Free Windsurf + BYOK = Now I have a reason to try Windsurf.
  • Recent Achievements

    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
    • Rookie
      Pat-Garrett went up a rank
      Rookie
    • Week One Done
      Outdoor Saunaio earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      331
    2. 2
      snowy owl
      165
    3. 3
      +FloatingFatMan
      158
    4. 4
      ATLien_0
      154
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!