Find the answer to your Linux question:
Results 1 to 8 of 8
Hi I am trying to insert a string at the begining of 4th line into a file. $ vi ppp.sh #! /bin/bash a="pawan" echo "$a" | `:3,5w >> ggg` On ...
  1. #1
    Just Joined!
    Join Date
    Jun 2006
    Posts
    40

    Unhappy How to append a stirng from Vi editor to a file at 4th line.

    Hi

    I am trying to insert a string at the begining of 4th line into a file.


    $ vi ppp.sh
    #! /bin/bash

    a="pawan"
    echo "$a" | `:3,5w >> ggg`


    On executing the "sh ppp.sh", Iam getting below error.
    ppp.sh: line 1: :3,5w: command not found
    ppp.sh: line 2: `:3,5w >> ggg`: ambiguous redirect

    I think Iam doing something wrong. Can anybody suggest me the correct way to insert the string "pawan" at the 4th line number in "ggg" file.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Er...you've just written a script that invokes the Bash command ':3,5w', with stdin containing "pawan", and asking it to append to the file 'ggg'.

    If you're trying to do this with vi, just run "vi ggg", press "3G" in command mode, then "o", and you'll now have inserted a new line above the previously 4th line. Or if you want to insert it at the beginning of the 4th line (and not above it), press "4G", then "I".

    In vi, "#G" moves you to line number #, 'o' makes a new line below the current line and puts you into insert mode at the beginning of that line, and 'I' puts you into insert mode at the beginning of the current line.

    Run the command "vimtutor" to get a basic tutorial of vi. You can also Google for some good resources. vi is a very powerful editor, but it has a _very_ steep learning curve.


    If your intention is to add something to the 4th line of a file from the commandline, without using a text editor, you can use the sed utility. For instance, for your particular example:
    Code:
    sed -e '{ 4 s/^/pawan/ }'
    This takes the 4th line, and "replaces" the beginning of the line with "pawan", in effect prepending "pawan" to the line.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Jul 2007
    Posts
    41
    Hi,

    I'm not a good enough bash programmer to debug your code. I think this should acheive the same purpose though.

    Code:
    sed ' 4 { s/^/pawan/ }' -i ggg
    this should have sed replace the empty string at the beginning of line 4 with the string pawan in file ggg

    Hope That Helps

  4. #4
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101

    Red face How to append a variables value in a file at a particular line.

    File is:

    Code:
    [/E*Fare/testteam/public/pf3/nggfstatic/4k-pf3_3.case
    REGION1:
    /E*Fare/dist/src/nggfstaticbase/EFare/Server
    CODEBASE1:
    /dev_tools/LINUXMTP-4/EFS070718E/EFare/Server
    DATABASE1: nggfstatic
    SCRIPT: /efare1/admin/ezlcn/scripts/pf3_3_scriptlist.input
    PROLOGINITSIZE not yet set
    You are using: time
    PROLOGINITSIZE is  200M
    DATABASE CONFIG FILE: config/spserver-nggfstatic
    .
    .
    .
    .
    .
    .
    200 - 300 lines

    and value inside $lines is:
    Code:
    Time1
    Time2
    Time3
    Time4
    Time5
    Now, I want to insert the value of "$lines" between the lines below from file ggg:
    Code:
    PROLOGINITSIZE is  200M
    DATABASE CONFIG FILE: config/spserver-nggfstatic
    Kindly help, in what all ways we can efficiently achieve this.
    Last edited by devils casper; 08-08-2007 at 03:02 AM. Reason: code tags
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  5. #5
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    Cabhan- Can you pls provide how to add pawan if it is stored in a variable...
    >>>> '"$val"' ????
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    To insert the variable after the 10th line:

    Code:
    sed "10 s/$/\n$val/" ggg
    To insert the variable after the searchline "PROLOGINITSIZE is 200M":

    Code:
    sed "s/"PROLOGINITSIZE is  200M"/&\n$val/" ggg
    Regards

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by Franklin52 View Post
    To insert the variable after the 10th line:

    Code:
    sed "10 s/$/\n$val/" ggg
    To insert the variable after the searchline "PROLOGINITSIZE is 200M":

    Code:
    sed "s/"PROLOGINITSIZE is  200M"/&\n$val/" ggg
    Regards
    Have you actually tried this? Your use of quotes leaves much to be desired.

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    You're right, it ought not to be allowed, thanks for your attention, I apologize.
    This line:

    Code:
    sed "s/"PROLOGINITSIZE is  200M"/&\n$val/" ggg
    should be:

    Code:
    sed "s/PROLOGINITSIZE is  200M/&\n$val/" ggg
    Regards

Posting Permissions

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