Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I need to create a bash script that adds a row before every curly bracket for functions, classes (classes and methods) and try/catch blocks in PHP code. I think ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! laresistance's Avatar
    Join Date
    Oct 2012
    Posts
    1

    Question Bash script which adds a line before every curly brackets


    Hello,
    I need to create a bash script that adds a row before every curly bracket for functions, classes (classes and methods) and try/catch blocks in PHP code.

    I think using the program in conjunction with sed
    E.G. : find . -type f \( -name "*.php \) -exec sed .......

    For example:

    PHP Code:
    <?php
    function my_function() {

    }
    Becomes:

    PHP Code:
    <?php
    function my_function() 
    {

    }
    Can you help me so that I can achieve this, please?

    Thank you!

  2. #2
    Just Joined!
    Join Date
    May 2012
    Posts
    43
    The sed part is easy.
    Code:
    sed -e "s|{|\n{|g"
    Not sure about find.

  3. #3
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    adding to what Cancerous has said:

    Code:
    find . -type f -name "*.php" -exec sed -i.bak "s|{|\n{|g" {} \;
    you can remove the "-i.bak" part, if you are not paranoid and do not want to create backup copies of all files that sed edits.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •