Results 1 to 6 of 6

Thread: bash - trans spaces to \n and then echo?

  1. #1
    Join Date
    Dec 2007
    Location
    Castle Rock, CO (Denver)
    Beans
    81
    Distro
    Ubuntu 14.04 Trusty Tahr

    bash - trans spaces to \n and then echo?

    Okay, now I've run into something subtle: I want to take a string of space-delimited words, translate the space-chars (" ") into newlines (\n) and just echo the result, which should output each word on a separate line, right? Like this:

    Code:
    echo -e $( dirs | tr " " "\n" )
    Actually, I want to reformat the output of dirs -- so let's say that it generates something like:

    Code:
    ~/dir3 ~/dir2 ~/dir1 ~
    ...and, just for the heck of it, I want it to print it this way:

    Code:
    ~/dir3
    ~/dir2
    ~/dir1
    ~
    But this code-frag spits out only the original single line, no matter what I do; even works this way with a printf instead of the echo command.

    I know that the tr is basically working, as I can demonstrate: replacing the "\n" with "X" produces:

    Code:
    ~/dir3X~/dir2X~/dir1X~
    I've tried other tricks/variants, all with the same result: output on one line.

    Any ideas or advice? Any bash experts out there? TIA

  2. #2
    Join Date
    Dec 2007
    Location
    Castle Rock, CO (Denver)
    Beans
    81
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: bash - trans spaces to \n and then echo?

    I just rendered the specific question somewhat moot -- a little online digging reminds me that dirs has a -v option, which prints DIRSTACK in vertical (multiline) format, what I wanted.

    But, putting dirs aside, what about the general question? Given a string of space-delimited workds, how to translate spaces into newlines and echo the result the way I want? I'm missing something here!... TIA

  3. #3
    Join Date
    Feb 2009
    Beans
    542
    Distro
    Kubuntu

    Re: bash - trans spaces to \n and then echo?

    I find that just
    Code:
    dirs | tr " " "\n"
    works without the echo. Echo seems only to put a newline in when there's an explicit escape sequence in the input. If there's an actual newline in the input it converts it to a space.

  4. #4
    Join Date
    May 2007
    Location
    USA
    Beans
    318
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: bash - trans spaces to \n and then echo?

    Quote Originally Posted by Lorin Ricker View Post
    ...and, just for the heck of it, I want it to print it this way:

    Code:
    ~/dir3
    ~/dir2
    ~/dir1
    ~
    Code:
    foo$ pushd /etc
    /etc ~/foo
    etc$ pushd /tmp
    /tmp /etc ~/foo
    tmp$ help dirs
    dirs: dirs [-clpv] [+N] [-N]
        Display the list of currently remembered directories.  Directories
        find their way onto the list with the `pushd' command; you can get
        back up through the list with the `popd' command.
    
        The -l flag specifies that `dirs' should not print shorthand versions
        of directories which are relative to your home directory.  This means
        that `~/bin' might be displayed as `/homes/bfox/bin'.  The -v flag
        causes `dirs' to print the directory stack with one entry per line,
        prepending the directory name with its position in the stack.  The -p
        flag does the same thing, but the stack position is not prepended.
        The -c flag clears the directory stack by deleting all of the elements.
    
        +N  displays the Nth entry counting from the left of the list shown by
            dirs when invoked without options, starting with zero.
    
        -N  displays the Nth entry counting from the right of the list shown by
            dirs when invoked without options, starting with zero.
    tmp$ dirs -p
    /tmp
    /etc
    ~/foo
    tmp$ dirs | tr ' ' '\n'
    /tmp
    /etc
    ~/foo
    tmp$
    HTH

  5. #5
    Join Date
    Dec 2007
    Location
    Castle Rock, CO (Denver)
    Beans
    81
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: bash - trans spaces to \n and then echo?

    Many thanks for the quick responses! Both are very helpful! -- L

  6. #6
    Join Date
    May 2007
    Location
    USA
    Beans
    318
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: bash - trans spaces to \n and then echo?

    Quote Originally Posted by Lorin Ricker View Post
    Many thanks for the quick responses! Both are very helpful! -- L
    NP. It gave me an excuse to review the Bash builtins

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
  •