Results 1 to 4 of 4

Thread: how do I add static text in addition to ls results when using tee command

  1. #1
    Join Date
    Sep 2014
    Location
    Oregon
    Beans
    24
    Distro
    Xubuntu

    Question how do I add static text in addition to ls results when using tee command

    I'm running the following:
    ls *.dbf | tee filename

    This creates a file called filename, and puts the results of ls *.dbf into that file, with a separate line for each file ls returns:

    example1.dbf
    test1.dbf
    here_it_is.dbf

    I then go in and manually add ~~~ at the beginning of each line in filename:

    ~~~example1.dbf
    ~~~test1.dbf
    ~~~here_it_is.dbf


    Is there a way to incorporate the ~~~ addition into the command so that every line in filename begins with ~~~?

    Please help, and thanks!
    Last edited by cris7; December 11th, 2014 at 06:27 PM.

  2. #2
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: how do I add static text in addition to ls results when using tee command

    Hi

    You could do something like this.

    Move into the directory you want and type.

    Code:
    for f in *.dbf; do printf "~~~%s\n" "$f"; done > filename
    This will not recurse into child directories.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  3. #3
    Join Date
    Sep 2014
    Location
    Oregon
    Beans
    24
    Distro
    Xubuntu

    Re: how do I add static text in addition to ls results when using tee command

    Thanks matt_symes!


    I also found that this worked...

    ls *.dbf | tee runfile | awk '$0="~~~"$0' > filename


    Not sure if your example or mine really make a difference, I'm still pretty new at this.

    I did turn my example above into an executable script and will now run it whenever I need to generate the file...

    Thanks again!

  4. #4
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: how do I add static text in addition to ls results when using tee command

    Hi

    Quote Originally Posted by cris7 View Post
    Not sure if your example or mine really make a difference, I'm still pretty new at this.
    One difference is that the pipeline is slightly slower as it forks more processes and this is expensive. This can make a real difference on large datasets and depending on how you code your scripts.

    For one file

    Code:
    matthew-laptop:/home/matthew:3 % ls *.dbf
    test.dbf
    matthew-laptop:/home/matthew:3
    Code:
    matthew-laptop:/home/matthew:3 % time ( for f in *.dbf; do printf "~~~%s\n" "$f"; done > filename )
    ( for f in *.dbf; do; printf "~~~%s\n" "$f"; done > filename; )  0.00s user 0.00s system 77% cpu 0.002 total
    matthew-laptop:/home/matthew:3 %
    Code:
    time (ls *.dbf | tee runfile | awk '$0="~~~"$0' > filename)
    ( ls -F --color=auto *.dbf | tee runfile | awk '$0="~~~"$0' > filename; )  0.00s user 0.01s system 94% cpu 0.007 total
    matthew-laptop:/home/matthew:3 %
    However in this case the difference is so small (at least for one file), we'll ignore it

    Obviously one would want an averaged time to get more accurate results.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

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
  •