Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Prepending text to the begining of a file

  1. #1
    Join Date
    Dec 2009
    Location
    South Devon, UK
    Beans
    30
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Prepending text to the begining of a file

    Hi All

    From the command line you can append text to a file. For example:-
    Code:
    uptime >> uptimeStats.txt
    This will append the output of uptime to the file uptimeStats.txt

    Is there an equally simple way of adding the text to the begining of the file? "prepending", if you will?. The original contents of the file must persist.

    Many thanks
    Best wishes
    Roly

  2. #2
    Join Date
    Sep 2009
    Beans
    160
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Prepending text to the begining of a file

    Not really. You could dump the text you want to prepend into a temporary file (A), append the other file (B) to it, delete or rename B, rename A to B's original name.

  3. #3
    Join Date
    Dec 2009
    Location
    South Devon, UK
    Beans
    30
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Prepending text to the begining of a file

    Hi peculiar penguin

    Thank you for your reply, and..

    Yeah, kind of what I was expecting.

    Many thanks
    Roly

  4. #4
    Join Date
    Feb 2007
    Location
    West Hills CA
    Beans
    10,044
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Prepending text to the begining of a file

    man cat
    man tac

    cat firstfile.txt secondfile.txt > combinedfile.txt
    -------------------------------------
    Oooh Shiny: PopularPages

    Unumquodque potest reparantur. Patientia sit virtus.

  5. #5
    Join Date
    Dec 2009
    Location
    South Devon, UK
    Beans
    30
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Prepending text to the begining of a file

    Hi tgalati4

    Thank you very much. That has got me a lot further.

    All I am actually trying to do is enter a new "note" from the command line and have it added to the start of a text file - full of notes. There is no order or sorting, other than chronological, to the text file.

    I tried using ...
    Code:
    cat - file2 > combined
    i.e. the dash (-) taking input from the command line. But when I press ctrl+c to finish providing my input, the entire command quits and the combined file is not updated correctly. I hope that makes some sort of sense!?

    Then I tried...
    Code:
    echo "My new note, directed at the head of an existing text file" | cat - file2 > combined
    This DID work. So that's great, thank you. Any more bright ideas to make it even easier?

    Many thanks
    roly

    edit: obviously I still then need to rename "combined" to "file2"
    Last edited by pararoly; May 8th, 2010 at 03:49 PM. Reason: increase clarity

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Prepending text to the begining of a file

    Quote Originally Posted by pararoly View Post

    I tried using ...
    Code:
    cat - file2 > combined
    i.e. the dash (-) taking input from the command line. But when I press ctrl+c to finish providing my input, the entire command quits and the combined file is not updated correctly. I hope that makes some sort of sense!?
    Yes, ^C causes the active program to receive a SIGINT signal.

    You have to press ^D to indicate the EOF.

    http://en.wikipedia.org/wiki/Control-C

    http://en.wikipedia.org/wiki/Control-D

  7. #7
    Join Date
    Dec 2009
    Location
    South Devon, UK
    Beans
    30
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Prepending text to the begining of a file

    Hi sisco311

    Awesome - I suspected there must be a different key combination.

    thank you (all) for your help

    Best wishes
    Roly

    - thread solved

  8. #8
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Prepending text to the begining of a file

    You are welcome!

    If you want to add multiple lines, instead of piping echo to the cat command you can use something like:
    Code:
    cat - file << EOF > newfile
    line1
    line2
    line3
    EOF

  9. #9
    Join Date
    Dec 2009
    Location
    South Devon, UK
    Beans
    30
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Prepending text to the begining of a file

    Hi sisco311

    Just tried that - wow, the more I learn about linux the more i love it! it works great!

    Can I ask another question....

    What is the "<<" operator called. If it has a name? I know that:-

    > re-directs output, and
    >> appends output, so
    << this waits until the EOF (or whatever) marker is reached?

    One day, I hope to become really good at linux - but it is going to take a while to learn it all! haha!

    Thank you very much once more.
    Best wishes
    roly

  10. #10
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Prepending text to the begining of a file

    Quote Originally Posted by pararoly View Post
    All I am actually trying to do is enter a new "note" from the command line and have it added to the start of a text file - full of notes.
    If this is something that will be done on a regular basis, you may want to add a function to your bashrc file. I have included a simple example below that is based on your command line. With this you would simply type the name of the function (adt in my example) followed by a space followed by the text to be added to the start of the text file. The text does not have to be quoted. This assumes, of course, that the name of the combined file does not change. You could dress this up in various ways--for example to insert newlines.

    Code:
    adt () {
    echo "$*" | cat - combined.txt >> .temp.txt
    mv .temp.txt combined.txt
    }
    Last edited by kaibob; May 8th, 2010 at 10:42 PM.

Page 1 of 2 12 LastLast

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
  •