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

  • SEARCH
  • Home
  • 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 290337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:59:18+00:00 2026-05-12T05:59:18+00:00

I know this is silly but I can’t overcome my curiosity. Is it possible

  • 0

I know this is silly but I can’t overcome my curiosity. Is it possible to write a shell script to format a piece of java code?

For example, if a user writes in a code:

    public class Super{
    public static void main(String[] args){
    System.out.println("Hello world");
    int a=0;
    if(a==100)
    {
    System.out.println("Hello world");
    }
    else
    {
    System.out.println("Hello world with else");
    }
    }
}

I would like to write a shell script which would make the code like this.

 public class Super
 {
  public static void main(String[] args)
  {
   System.out.println("Hello world");
   int a=0;
   if(a==100){
    System.out.println("Hello world");
   }
   else{
    System.out.println("Hello world with else");
   }
}

To be precise, we should change the formatting of flower brackets. If it is try/catch or control structures we should change it to same line and if it is function/method/class it should come in next line.I have little knowledge about sed and awk which can do this task so easily. Also I know this can be done using eclipse.

  • 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-12T05:59:19+00:00Added an answer on May 12, 2026 at 5:59 am

    Well, I’ve had some free time on my hands, so I decided to relive my good old linux days :]

    After reading a bit about awk and sed, I’ve decided that it might be better to use both, as it is easier to add indentation in awk and parse strings in sed.

    Here is the ~/sed_script that formats the source file:

        # delete indentation
        s/^ \+//g
    
        # format lines with class
        s/^\(.\+class.\+\) *\({.*\)$/\1\n\2/g
    
        # format lines with methods
        s/^\(public\|private\)\( \+static\)\?\( \+void\)\? \+\(.\+(.*)\) *\({.*\)$/\1\2\3 \4\n\5/g
    
        # format lines with other structures
        /^\(if\|else\|for\|while\|case\|do\|try\)\([^{]*\)$/,+1 {   # get lines not containing '{'
                                                                # along with the next line
          /.*{.*/ d             # delete the next line with '{'
          s/\([^{]*\)/\1 {/g    # and add '{' to the first line
        }

    And here is the ~/awk_script that adds indentation:

        BEGIN { depth = 0 }
        /}/   { depth = depth - 1 }
              {
                  getPrefix(depth)
                  print prefix $0
              }
        /{/   { depth = depth + 1 }
    
              function getPrefix(depth) {
                  prefix = ""
                  for (i = 0; i < depth; i++) { prefix = prefix "    "}
                  return prefix
              }

    And you use them like that:

        > sed -f ~/sed_script ~/file_to_format > ~/.tmp_sed
        > awk -f ~/awk_script ~/.tmp_sed

    It is far from proper formatting tool, but I hope it will do OK as a sample script for reference :] Good luck with your learning.

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

Sidebar

Related Questions

I know this question might be little silly but I can't seem to find
I know this very silly, but can anybody help me in understanding what does
i know this sounds silly, but can someone please post the arrays described by
This seems really silly but; How can I know which version of ASP.net MVC
I know this may be really silly - but how can I disable the
I know this is a silly question but i can't find out how to
I know this may seem like a silly and tiny issue, but I can't
I know this must be a really silly question, but I can't figure out
I know this is going to sound silly but I can't find what's wrong.
I know this is silly question but as a beginner at xcode, and i

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.