Rebalance shell script

The #1 community for Gun Owners in Indiana

Member Benefits:

  • Fewer Ads!
  • Discuss all aspects of firearm ownership
  • Discuss anti-gun legislation
  • Buy, sell, and trade in the classified section
  • Chat with Local gun shops, ranges, trainers & other businesses
  • Discover free outdoor shooting areas
  • View up to date on firearm-related events
  • Share photos & video with other members
  • ...and so much more!
  • revsaxon

    Master
    Rating - 100%
    2   0   0
    Feb 21, 2010
    1,954
    38
    Plano, TX
    I know we have a decent chunk of IT types on here, so it seemed like a good place to ask....

    I was recently asked to update a large chunk of scripts (a combination of perl and bash), and in doing so discovered that they are unreadable due to the atrocious amount of white space used. For example, in one section there is 143 spaces before the actual line starts. If this was just one script that would be one thing, but across this many...

    I know I could do something like "sed 's/^[ \t]*//;s/[ \t]*$//'", but that leaves me with a mess as well. Anyone know of a way/utility/script/anything to rebalance these files?
     

    misconfig

    Master
    Rating - 100%
    28   0   1
    Apr 1, 2009
    2,495
    38
    Avon
    The Perl interpreter doesn't care about the whitespace, I'm assuming this is purely aesthetic.
    The easiest way is a beautiful Perl 1 liner:

    The original file will not be altered ( for safety's sake ), redirect all output to a brand new file.

    perl -pe 's/^\s+//' /path/to/file > /path/to/newfile.txt

    If you have a glob of files to work with, wrap it with a bash script:

    for i in file1 file2 file3 fileN;do perl -pe 's/^\s+//' $i > $i.out;done

    TEST CASE:

    ORG FILE:
    dsf asdfasd dsafasdf asdfa sdf
    d



    asdfasdfasdfasdf asdfasdfasd


    asdfasdf
    OUTFILE:
    dsf asdfasd dsafasdf asdfa sdf
    d
    asdfasdfasdfasdf asdfasdfasd
    asdfasdf
    *NOTE*

    This script does *NOT* strip whitespace in the middle of the string, only before and after - also carriage returns hold true.
     

    revsaxon

    Master
    Rating - 100%
    2   0   0
    Feb 21, 2010
    1,954
    38
    Plano, TX
    That would work, but I was looking more for something that would result in

    abcdguuh {
    __ehdhudn
    __dghhdu {
    ____ difij
    __}
    }

    For some reason the quote stripped teh whitespace, just assume "_" == " "

    I know I could write a script to go line by line to ballance it based on ^{, but I was hoping someone already had something like that written :D
     

    misconfig

    Master
    Rating - 100%
    28   0   1
    Apr 1, 2009
    2,495
    38
    Avon
    That would work, but I was looking more for something that would result in



    For some reason the quote stripped teh whitespace, just assume "_" == " "

    I know I could write a script to go line by line to ballance it based on ^{, but I was hoping someone already had something like that written :D

    This is your best bet:

    The Perltidy Home Page

    SHebang perltidy from VIM

    $!/sbin/perltidy

    It will pretty everything up for you, it's a nice pretty-parser that is waaay more robust than a script.
     
    Top Bottom