sed question? anyone... - Linux

This is a discussion on sed question? anyone... - Linux ; hello - I have a file with 100 lines of text. I know my line number which needs to be replaced too. Say I need to replace line 70 with "I hate winters." I have tried "sed" but no luck.... ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: sed question? anyone...

  1. sed question? anyone...

    hello -

    I have a file with 100 lines of text. I know my line number which
    needs to be replaced too. Say I need to replace line 70 with "I hate
    winters." I have tried "sed" but no luck.... any ideas with sed or
    any syntax..?

    line_number=70
    replace_line_with="I hate winters"

    thanks

  2. Re: sed question? anyone...

    On 20 Feb 2004 22:53:20 -0800, hallian wrote:
    > hello -
    >
    > I have a file with 100 lines of text. I know my line number which
    > needs to be replaced too. Say I need to replace line 70 with "I hate
    > winters." I have tried "sed" but no luck.... any ideas with sed or
    > any syntax..?
    >
    > line_number=70
    > replace_line_with="I hate winters"



    Here is a dirty untested kludge.

    fn_2_chg=filename_here

    head -69 $fn_2_chg > top # get the first 69 lines
    echo "I hate winters" >> top # append my new line
    _max=`wc -l < $fn_2_chg` # get number of lines in a file
    _ln-`expr $_max - 71` # get remaining line count
    tail -$_ln >> top # append those lines to the top file.


  3. Re: sed question? anyone...

    On Sat, 21 Feb 2004 07:21:46 +0000, Bit Twister wrote:

    > On 20 Feb 2004 22:53:20 -0800, hallian wrote:
    >> hello -
    >>
    >> I have a file with 100 lines of text. I know my line number which
    >> needs to be replaced too. Say I need to replace line 70 with "I hate
    >> winters." I have tried "sed" but no luck.... any ideas with sed or
    >> any syntax..?
    >>
    >> line_number=70
    >> replace_line_with="I hate winters"

    >
    >
    > Here is a dirty untested kludge.
    >
    > fn_2_chg=filename_here
    >
    > head -69 $fn_2_chg > top # get the first 69 lines
    > echo "I hate winters" >> top # append my new line
    > _max=`wc -l < $fn_2_chg` # get number of lines in a file
    > _ln-`expr $_max - 71` # get remaining line count
    > tail -$_ln >> top # append those lines to the top file.


    If you're going to do it that way, then replace the last three lines with

    tail +71 $fn_2_chg >> top


  4. Re: sed question? anyone...

    On 20 Feb 2004 22:53:20 -0800, hallian wrote:
    >
    >
    > hello -
    >
    > I have a file with 100 lines of text. I know my line number which
    > needs to be replaced too. Say I need to replace line 70 with "I hate
    > winters." I have tried "sed" but no luck.... any ideas with sed or
    > any syntax..?
    >
    > line_number=70
    > replace_line_with="I hate winters"
    >
    > thanks


    Be easier to use ed for this.

    ed filename

    70c
    replacement text
    ..
    wq

    Or from the commandline:

    echo -e "70c\nreplacement text\n.\nwq" | ed -s filename

    or in a script:

    ed -s filename < 70c
    replacement text
    ..
    wq
    YYY

    (YYY can be any unique string)


    AC

    --
    ed(1) Check out the original tutorials by Brian W.
    Kernighan at the Ed Home Page http://tinyurl.com/2aa6g

  5. Re: sed question? anyone...

    "hallian" wrote in message
    news:a98477d4.0402202253.53da68f@posting.google.co m

    > I have a file with 100 lines of text. I know my line number which
    > needs to be replaced too. Say I need to replace line 70 with "I hate
    > winters." I have tried "sed" but no luck.... any ideas with sed or
    > any syntax..?
    >
    > line_number=70
    > replace_line_with="I hate winters"


    sed ' 70s/.*/\"I hate winters.\"/ ' filename > new_filename


    tony

    --
    use hotmail for any email replies


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

  6. Re: sed question? anyone...

    On Sat, 21 Feb 2004 09:58:26 +0000, Alan Connor wrote:

    > On 20 Feb 2004 22:53:20 -0800, hallian wrote:
    >>
    >>
    >> hello -
    >>
    >> I have a file with 100 lines of text. I know my line number which
    >> needs to be replaced too. Say I need to replace line 70 with "I hate
    >> winters." I have tried "sed" but no luck.... any ideas with sed or
    >> any syntax..?
    >>
    >> line_number=70
    >> replace_line_with="I hate winters"
    >>
    >> thanks

    >
    > Be easier to use ed for this.
    >
    > ed filename
    >
    > 70c
    > replacement text
    > .
    > wq
    >
    > Or from the commandline:
    >
    > echo -e "70c\nreplacement text\n.\nwq" | ed -s filename
    >
    > or in a script:
    >
    > ed -s filename < > 70c
    > replacement text
    > .
    > wq
    > YYY
    >
    > (YYY can be any unique string)
    >
    >
    > AC


    What color is the sky in your universe, in which the above is
    easier than sed -i -e '70s/^.*$/I hate winters/' filename ?


  7. Re: sed question? anyone...

    I wrote in message news:403736cb_4@corp.newsgroups.com

    > sed ' 70s/.*/\"I hate winters.\"/ ' filename > new_filename


    sed ' 70s/.*/"I hate winters."/ ' filename > new_filename

    --
    use hotmail for any email replies


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

  8. Re: sed question? anyone...

    hallian@hotmail.com (hallian) writes:

    > hello -
    >
    > I have a file with 100 lines of text. I know my line number which
    > needs to be replaced too. Say I need to replace line 70 with "I hate
    > winters." I have tried "sed" but no luck.... any ideas with sed or
    > any syntax..?



    Very simple. Your sed script file;

    70 c\
    I hate winters


    $ sed -f script input


    HTH


    >
    > line_number=70
    > replace_line_with="I hate winters"
    >
    > thanks


    --
    -------------------------------------------------------------------------------
    Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
    305 321-1144 (mobile http://www.JerrySievers.com/

+ Reply to Thread