Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Sed

  • 22-04-2013 10:41am
    #1
    Registered Users, Registered Users 2 Posts: 632 ✭✭✭


    Hi All,

    I found this command that removes any white space at the start of a line.
    sed -e 's/^ [ \t]*//'
    It works grand but could anyone explain to me how it works?

    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    it's a regular expression. They are worth books, but here's a quick overview of that one:

    s/change this/to this/

    The s bit is "substitute".
    / = start of match part
    ^ means match start of line
    then there's a space
    then there's a grouping of space or \t (tab)
    the * means any number of times
    / = end of match part, start of substitute part
    / = end of substitute part

    So, putting that all together: Do a substitution. Match the start of a line followed by a space, followed by any number of spaces or tabs, replace with nothing (i.e. delete).


  • Registered Users, Registered Users 2 Posts: 632 ✭✭✭jimmyendless


    Thanks for that Khannie. It's pointless using it if I have no idea how it works.


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    No problem. :) Regular expressions are a slight pain in the arse to learn, and intimidating to read, but they have saved me hundreds of hours over the years. They're an amazing tool.


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    There is only one book, Jeffrey Friedl's Mastering regular expressions

    Aside from grep, sed and awk which use regexes to great effect at the command line they are also a "mini-game" within every programming language and are worth learning.

    Because so many language ape Perl's regex library (PCRE, Perl Compatible Regular Expressions) it's probably worth learning how to use them in Perl (particularly if you need to manipulate text.


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie



    I read (most of) it years ago. Really good book.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 5,238 ✭✭✭humbert




  • Registered Users, Registered Users 2 Posts: 8,074 ✭✭✭10-10-20


    Khannie wrote: »
    it's a regular expression. They are worth books, but here's a quick overview of that one:

    s/change this/to this/

    The s bit is "substitute".
    / = start of match part
    ^ means match start of line
    then there's a space
    then there's a grouping of space or \t (tab)
    the * means any number of times
    / = end of match part, start of substitute part
    / = end of substitute part

    So, putting that all together: Do a substitution. Match the start of a line followed by a space, followed by any number of spaces or tabs, replace with nothing (i.e. delete).

    I gotta say, that's the first time I have ever understood any statement which follows the name of the utility. Did you ever think of writing a book...?


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Haha. Glad to be of service. That approach to regex's (match this, then match this, then match this) is something I probably picked up from the book mentioned earlier in the thread.


Advertisement