Is there a nifty way of adding or inserting text into a line - Setup

This is a discussion on Is there a nifty way of adding or inserting text into a line - Setup ; Is there a nifty Linux trick of adding or inserting text into a particular line of a file. Insert text at the beginning of the first line of a file will do for now. - voipfc...

+ Reply to Thread
Results 1 to 12 of 12

Thread: Is there a nifty way of adding or inserting text into a line

  1. Is there a nifty way of adding or inserting text into a line


    Is there a nifty Linux trick of adding or inserting text into a
    particular line of a file.

    Insert text at the beginning of the first line of a file will do for
    now.

    - voipfc


  2. Re: Is there a nifty way of adding or inserting text into a line

    voipfc wrote:
    > Is there a nifty Linux trick of adding or inserting text into a
    > particular line of a file.


    I wrote a script that does this:

    http://www.comp.eonworks.com/scripts...-20010623.html

    > Insert text at the beginning of the first line of a file will do for
    > now.


    In this case you can simply do:

    cat > new_file.txt

    --
    Dawid Michalczyk
    http://www.comp.eonworks.com _Linux SysAdmin and Webmaster scripts_

  3. Re: Is there a nifty way of adding or inserting text into a line

    voipfc wrote:
    > Is there a nifty Linux trick of adding or inserting text into a
    > particular line of a file.
    >
    > Insert text at the beginning of the first line of a file will do for
    > now.
    >
    > - voipfc
    >

    Vi?

  4. Re: Is there a nifty way of adding or inserting text into a line

    On Jul 17, 9:12 am, Dawid Michalczyk wrote:
    > voipfc wrote:
    > > Is there a nifty Linux trick of adding or inserting text into a
    > > particular line of a file.

    >
    > I wrote a script that does this:
    >
    > http://www.comp.eonworks.com/scripts...-20010623.html
    >
    > > Insert text at the beginning of the first line of a file will do for
    > > now.

    >
    > In this case you can simply do:
    >
    > cat > new_file.txt
    >
    > --
    > Dawid Michalczykhttp://www.comp.eonworks.com _Linux SysAdmin and Webmaster scripts_


    I think I failed to express myself properly.

    What I actually meant is to join the text you want to add to an
    existing line, either at the start of the line, or a chosen character
    position within the line, rather than inserting.

    Thanks for your script all the same.

    - voipfc


  5. Re: Is there a nifty way of adding or inserting text into a line

    On Jul 17, 7:23 am, voipfc wrote:
    > Is there a nifty Linux trick of adding or inserting text into a
    > particular line of a file.
    >
    > Insert text at the beginning of the first line of a file will do for
    > now.
    >
    > - voipfc


    I tried something like this:

    echo "a new file with text" >> afile
    echo "inserted text " >> inserted
    cat afile >> inserted
    cat inserted

    content of inserted
    ---------------------------
    inserted text
    a new file with text

    The problem is that a newline character is added to the inserted text,
    when the result I want is "inserted texta new file with text"

    Is there a way around this?

    In case you want to know what it is for, it is to add restrictions to
    ssh key files


  6. Re: Is there a nifty way of adding or inserting text into a line

    voipfc wrote:
    > On Jul 17, 7:23 am, voipfc wrote:
    >> Is there a nifty Linux trick of adding or inserting text into a
    >> particular line of a file.
    >>
    >> Insert text at the beginning of the first line of a file will do for
    >> now.
    >>
    >> - voipfc

    >
    > I tried something like this:
    >
    > echo "a new file with text" >> afile
    > echo "inserted text " >> inserted
    > cat afile >> inserted
    > cat inserted
    >
    > content of inserted
    > ---------------------------
    > inserted text
    > a new file with text
    >
    > The problem is that a newline character is added to the inserted text,
    > when the result I want is "inserted texta new file with text"
    >
    > Is there a way around this?
    >
    > In case you want to know what it is for, it is to add restrictions to
    > ssh key files
    >


    Maybe awk grep and cut and so on will do it.

  7. Re: Is there a nifty way of adding or inserting text into a line

    voipfc wrote:
    > On Jul 17, 7:23 am, voipfc wrote:
    >> Is there a nifty Linux trick of adding or inserting text into a
    >> particular line of a file.
    >>
    >> Insert text at the beginning of the first line of a file will do for
    >> now.
    >>
    >> - voipfc

    >
    > I tried something like this:
    >
    > echo "a new file with text" >> afile
    > echo "inserted text " >> inserted
    > cat afile >> inserted
    > cat inserted
    >
    > content of inserted
    > ---------------------------
    > inserted text
    > a new file with text
    >
    > The problem is that a newline character is added to the inserted text,
    > when the result I want is "inserted texta new file with text"
    >
    > Is there a way around this?


    use: echo -n
    This will turn off the newline character. For more info:
    help echo

    --
    Dawid Michalczyk
    http://www.comp.eonworks.com _Linux SysAdmin and Webmaster scripts_

  8. Re: Is there a nifty way of adding or inserting text into a line

    On 2007-07-17, voipfc wrote:
    > On Jul 17, 7:23 am, voipfc wrote:
    >> Is there a nifty Linux trick of adding or inserting text into a
    >> particular line of a file.
    >>
    >> Insert text at the beginning of the first line of a file will do for
    >> now.
    >>
    >> - voipfc

    >
    > I tried something like this:
    >
    > echo "a new file with text" >> afile
    > echo "inserted text " >> inserted
    > cat afile >> inserted
    > cat inserted
    >
    > content of inserted
    > ---------------------------
    > inserted text
    > a new file with text
    >
    > The problem is that a newline character is added to the inserted text,
    > when the result I want is "inserted texta new file with text"
    >
    > Is there a way around this?


    Yes, don't print a newline.

    {
    printf "%s" "inserted text"
    cat afile
    } > inserted

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  9. Re: Is there a nifty way of adding or inserting text into a line

    voipfc wrote:
    > Is there a nifty Linux trick of adding or inserting text into a
    > particular line of a file.
    >
    > Insert text at the beginning of the first line of a file will do for
    > now.


    create the new line as a file

    cat file oldfile > oldfile

    should do it.

    Without testing

    cat `echo 'new text to file'` oldfile > oldfile

    should also do it. Note the backwards ` on the key to the left of the 1 key and
    the other being the single quote.

    --
    Al Qaeda is back its pre-911 strength of three hundred. I am so frightened I
    can only laugh to relieve the anxiety. 300 is the highest US government
    estimate of their numbers ever made public.
    -- The Iron Webmaaster, 3831
    nizkor http://www.giwersworld.org/nizkook/nizkook.phtml
    http://www.giwersworld.org

  10. Re: Is there a nifty way of adding or inserting text into a line

    On 2007-07-18, Matt Giwer wrote:
    > voipfc wrote:
    >> Is there a nifty Linux trick of adding or inserting text into a
    >> particular line of a file.
    >>
    >> Insert text at the beginning of the first line of a file will do for
    >> now.

    >
    > create the new line as a file
    >
    > cat file oldfile > oldfile
    >
    > should do it.
    >
    > Without testing
    >
    > cat `echo 'new text to file'` oldfile > oldfile


    That is the same as:

    cat new text to file oldfile > oldfile

    Not what you want at all. This is more like it:

    cat <(echo 'new text to file') oldfile > oldfile

    > should also do it. Note the backwards ` on the key to the left of the 1 key and
    > the other being the single quote.
    >



    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  11. Re: Is there a nifty way of adding or inserting text into a line

    On Jul 17, 1:29 pm, Dawid Michalczyk wrote:
    > voipfc wrote:
    > > On Jul 17, 7:23 am, voipfc wrote:
    > >> Is there a nifty Linux trick of adding or inserting text into a
    > >> particular line of a file.

    >
    > >> Insert text at the beginning of the first line of a file will do for
    > >> now.

    >
    > >> - voipfc

    >
    > > I tried something like this:

    >
    > > echo "a new file with text" >> afile
    > > echo "inserted text " >> inserted
    > > cat afile >> inserted
    > > cat inserted

    >
    > > content of inserted
    > > ---------------------------
    > > inserted text
    > > a new file with text

    >
    > > The problem is that a newline character is added to the inserted text,
    > > when the result I want is "inserted texta new file with text"

    >
    > > Is there a way around this?

    >
    > use: echo -n
    > This will turn off the newline character. For more info:
    > help echo
    >
    > --
    > Dawid Michalczykhttp://www.comp.eonworks.com _Linux SysAdmin and Webmaster scripts_


    It worked perfectly

    Thanks


  12. Re: Is there a nifty way of adding or inserting text into a line

    On Jul 17, 4:26 pm, "Chris F.A. Johnson" wrote:
    > On 2007-07-17, voipfc wrote:
    > > On Jul 17, 7:23 am, voipfc wrote:
    > >> Is there a nifty Linux trick of adding or inserting text into a
    > >> particular line of a file.

    >
    > >> Insert text at the beginning of the first line of a file will do for
    > >> now.

    >
    > >> - voipfc

    >
    > > I tried something like this:

    >
    > > echo "a new file with text" >> afile
    > > echo "inserted text " >> inserted
    > > cat afile >> inserted
    > > cat inserted

    >
    > > content of inserted
    > > ---------------------------
    > > inserted text
    > > a new file with text

    >
    > > The problem is that a newline character is added to the inserted text,
    > > when the result I want is "inserted texta new file with text"

    >
    > > Is there a way around this?

    >
    > Yes, don't print a newline.
    >
    > {
    > printf "%s" "inserted text"
    > cat afile
    >
    > } > inserted
    >
    > --
    > Chris F.A. Johnson, author |
    > Shell Scripting Recipes: | My code in this post, if any,
    > A Problem-Solution Approach | is released under the
    > 2005, Apress | GNU General Public Licence


    really nifty


+ Reply to Thread