Search code examples
regexbashcut

Extract a piece of a string in bash?


I have following string in BASH and I need the email address at the end of the line

TAG instance    i-1846f265  AdminEmail  dummy.email@domain.com

As far as I know the spacing between the words is always a single tab character, the email address at the end does not have a fixed length (could be one of any number of addresses)


Solution

  • You can use cut for this purpose:

    $ cut   -f4                -d$'\t'                  your_file
             print 4th field    set tab as delimiter
    

    Shorter:

    $ cut -f4 -d$'\t' your_file
    

    in case it is a string,

    $ echo "your_strin" | cut -f4 -d$'\t' your_file