Results 1 to 3 of 3

Thread: Bash newbie help, swapping stuff around.

  1. #1
    Join Date
    Jan 2009
    Beans
    104
    Distro
    Ubuntu 11.04 Natty Narwhal

    Bash newbie help, swapping stuff around.

    I apologise for not being able to put what I mean into words very well. Is there an easy way to swap two pieces of text around?

    I have a monstrosity of a line I've written for bash, and inside it is this
    Code:
    cut -d ' ' -f 5,8
    Which you probably know shows the fifth and eighth element in a line of elements separated by spaces. What I'd like to know how to do though is to swap them around. I tried
    Code:
    cut -d ' ' -f 8,5
    But that doesn't swap them around at all.

    So, is there an easy way to do this?

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Bash newbie help, swapping stuff around.

    maybe cut can also do it somehow but I personally prefer awk for splitting.
    displaying 5 and 8 swapped would be:
    echo "1 2 3 4 5 6 7 8" | awk '{print $8" "$5}'

  3. #3
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Bash newbie help, swapping stuff around.

    Quote Originally Posted by oopsie View Post
    I'd like to know how to do though is to swap them around. I tried
    Code:
    cut -d ' ' -f 8,5
    But that doesn't swap them around at all.

    So, is there an easy way to do this?
    use awk, as shown, or you can just the shell without calling external commands.
    Code:
    var="1 2 3 4 5"
    set -- $var
    echo "$5 $3"

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
  •