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 ...
- 08-07-2007 #1Just Joined!
- Join Date
- Jun 2006
- Posts
- 40
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.
- 08-07-2007 #2
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:
This takes the 4th line, and "replaces" the beginning of the line with "pawan", in effect prepending "pawan" to the line.Code:sed -e '{ 4 s/^/pawan/ }'DISTRO=Arch
Registered Linux User #388732
- 08-07-2007 #3Just 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.
this should have sed replace the empty string at the beginning of line 4 with the string pawan in file gggCode:sed ' 4 { s/^/pawan/ }' -i ggg
Hope That Helps
- 08-07-2007 #4
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:
Now, I want to insert the value of "$lines" between the lines below from file ggg:Code:Time1 Time2 Time3 Time4 Time5
Kindly help, in what all ways we can efficiently achieve this.Code:PROLOGINITSIZE is 200M DATABASE CONFIG FILE: config/spserver-nggfstatic
Last edited by devils casper; 08-08-2007 at 03:02 AM. Reason: code tags
- 08-07-2007 #5
Cabhan- Can you pls provide how to add pawan if it is stored in a variable...
>>>> '"$val"' ????
- 08-08-2007 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
To insert the variable after the 10th line:
To insert the variable after the searchline "PROLOGINITSIZE is 200M":Code:sed "10 s/$/\n$val/" ggg
RegardsCode:sed "s/"PROLOGINITSIZE is 200M"/&\n$val/" ggg
- 08-10-2007 #7Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
- 08-10-2007 #8Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
You're right, it ought not to be allowed, thanks for your attention, I apologize.

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


Reply With Quote
