Results 1 to 8 of 8

Thread: newbie question

  1. #1
    Join Date
    Jan 2004
    Location
    Greece
    Posts
    23

    Unanswered: newbie question

    HI all,

    How can i replace the nth line of a file with the value of a variable ?

    which utility should i use?

  2. #2
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hi subwrc,

    what is a "nth line".
    Please give an example to us.
    Greetings from germany
    Peter F.

  3. #3
    Join Date
    Oct 2003
    Location
    Slovakia
    Posts
    482
    Originally posted by fla5do
    Hi subwrc,

    what is a "nth line".
    Please give an example to us.
    I guess he means n-th line (where n means whatever number).

  4. #4
    Join Date
    Jul 2003
    Location
    Edinburgh
    Posts
    35

    Re: newbie question

    A bit of quoting hell, but this works in bash:

    n=2
    VAR="a text variable"
    sed $n's/^.*$/'"$VAR"'/' < $FILE

  5. #5
    Join Date
    Jan 2004
    Location
    Greece
    Posts
    23
    Originally posted by fla5do
    Hi subwrc,

    what is a "nth line".
    Please give an example to us.
    Sorry fla5do,as Ika said i mean the n-nth line where n=is a number

    Thanks Chillies I 'm going to try it and repost
    Last edited by subwrc; 01-14-04 at 05:07.

  6. #6
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525

    Re: newbie question

    Originally posted by chillies
    A bit of quoting hell, but this works in bash:

    n=2
    VAR="a text variable"
    sed $n's/^.*$/'"$VAR"'/' < $FILE
    Or like this...

    sed "${n}s/.*/${VAR}/" $FILE

  7. #7
    Join Date
    Jul 2003
    Location
    Edinburgh
    Posts
    35

    Re: newbie question

    I was worried that the .* could, in pathological circumstatnces, get expanded by the shell to a filename.

    Now I'm confused:

    $ n=2
    $ VAR=a
    $ mkdir -p 2s/.foo/a
    $ touch 2s/.foo/a/bar
    $ ls ${n}s/.*/${VAR}/
    bar
    $ ls "${n}s/.*/${VAR}/"
    ls: 2s/.*/a/: No such file or directory

    I suppose only variables are expanded in double quotes ...

  8. #8
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525

    Re: newbie question

    Originally posted by chillies
    I suppose only variables are expanded in double quotes ...
    Correct. For a simpler example try...

    echo *
    echo "*"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •