Results 1 to 5 of 5

Thread: bash swap the conten of string using multiple character delimiter

  1. #1
    Join Date
    Apr 2010
    Location
    Slovakia
    Beans
    87
    Distro
    Ubuntu 12.04 Precise Pangolin

    bash swap the conten of string using multiple character delimiter

    hi

    an example of my problem is the best way to understand what i can't figure out

    I have a string for example str
    Code:
    str="something1 next_colum something2"
    And I want to take the "next_colum" substring to be my delimiter and swap the "something1" with the "something2" and after that replace the "next_colum" with for example ">>>" characters

    I was thinking maybe awk would do it however so far I haven't been able to achive anything. So I am asking you for your help

    Thanks
    Acer Aspire 5750G i5 2410m, 4GB ram, 640GB hdd, intel graphics

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: bash swap the conten of string using multiple character delimiter

    Quote Originally Posted by Mr.Pytagoras View Post
    hi

    an example of my problem is the best way to understand what i can't figure out

    I have a string for example str
    Code:
    str="something1 next_colum something2"
    And I want to take the "next_colum" substring to be my delimiter and swap the "something1" with the "something2" and after that replace the "next_colum" with for example ">>>" characters

    I was thinking maybe awk would do it however so far I haven't been able to achive anything. So I am asking you for your help

    Thanks
    Something like

    Code:
    sed -e 's/\(.*\)__\(.*\)/\2++\1/'
    (where "__" and "++" can be replaced by the required delimiters)

  3. #3
    Join Date
    Apr 2010
    Location
    Slovakia
    Beans
    87
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash swap the conten of string using multiple character delimiter

    thanks

    I have figured it out using awk

    But thanks for you help, my awk solution is on 3 lines so your solution is better i guess
    Acer Aspire 5750G i5 2410m, 4GB ram, 640GB hdd, intel graphics

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: bash swap the conten of string using multiple character delimiter

    seriously?
    Code:
    $ echo something1 next_colum something2 | awk 'BEGIN { FS=" next_colum "; OFS=" >>> " }; { print $2, $1 }'
    something2 >>> something1
    Last edited by Vaphell; October 21st, 2011 at 09:59 PM.

  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: bash swap the conten of string using multiple character delimiter

    Quote Originally Posted by Mr.Pytagoras View Post
    thanks

    I have figured it out using awk

    But thanks for you help, my awk solution is on 3 lines so your solution is better i guess
    I don't know much awk, but there could be a one-liner in awk too. All these regexp-based programs are very similar.

Tags for this Thread

Bookmarks

Posting Permissions

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