Operating System - HP-UX
1748159 Members
3815 Online
108758 Solutions
New Discussion юеВ

Re: Using VI to insert line from bash script

 
SOLVED
Go to solution
AZayed
Super Advisor

Using VI to insert line from bash script

Dears,

Good day,

I would like to write a sh script that insert a line in a file BUT I keep getting "Input read error"

This is my try :

vi a << EOF
:i
insert
^[
EOF

Thanks
Success seems to be connected with action. Successful people keep moving. They make mistakes, but they don't quit.
17 REPLIES 17

Re: Using VI to insert line from bash script

vi is I suspect, not the tool for this job...more likely is that some other tool such as sed will do what you require, but more information is required first... where does the line get inserted in the file - at the start? at the end? somewhere else? Please describe you problem in a little more detail.

HTH

Duncan

I am an HPE Employee
Accept or Kudo
AZayed
Super Advisor

Re: Using VI to insert line from bash script

Hi,

Thanks for reply,

I have a file containing certain value X. About this value in four lines there is value Y I want to take Y and put instead X. And shift the lines one line below.

For Example :

1 junk data
2 junk data
3 Y
4 junk data
5 junk data
6 junk data
7 X
8 junk data

I want to do the following :

1 junk data
2 junk data
3 Y
4 junk data
5 junk data
6 junk data
7 Y
8 OLD X Updated
9 junk data

So I wrote the following as part of my shell script : "I have Y value in variable Z"

vi FILE << EOF
:7
:O
$Z
^[
:j
:xx
:wq!
EOF

-----------------

This is what I want. but it seams not working even if I want only to do this :

vi FILE << EOF
:7
:O
$Z
^[
:ZZ
EOF

Any help?

Thanks
Success seems to be connected with action. Successful people keep moving. They make mistakes, but they don't quit.
H.Merijn Brand (procura
Honored Contributor

Re: Using VI to insert line from bash script

Replace X with the correct pattern

$ perl -pi -e'/^X$/ and $_ .= "\n$ENV{Z}\n"' FILE

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Hein van den Heuvel
Honored Contributor

Re: Using VI to insert line from bash script

Here is an example solution using AWK.

$ awk '/X/{print line[(NR-4)%10]; $0 = "old " $0} {line[NR%10] = $0; print}' x


In slow motion:

/X/ { # if you see a line marked 'X' then
print line[(NR-4)%10]; # Pick up 4 lines ago from secret stash, and
$0 = "old " $0} # munge current line

{ # for each line
line[NR%10] = $0; # put in stash (circular buffer), and
print} # print current line (may be modified)
' x # input from a file called x

I used a 10 element circular buffer using a MODULUS function just for grins, to make clear where the '4 lines above' goes.
Using 5 elements would of course be enough.

hth,
Hein.
AZayed
Super Advisor

Re: Using VI to insert line from bash script

Hi folks,

I have to try them tomorrow. I have only one point to mention that X value it's not unique, it will be repeated over and over in the file.

Thanks
Success seems to be connected with action. Successful people keep moving. They make mistakes, but they don't quit.
Hein van den Heuvel
Honored Contributor

Re: Using VI to insert line from bash script

My awk solution will replace any line with the X with the line 4 lines earlier to that occurance of X, and add a line with the munged current X line.

Is that what was intended, Did you want to pick up only a first 'Y' and repeat that?

Of course you should replace 'X' Regular Expression which does nto generate false positive. Anchoring the string often helps a lot with that: For example /^X/ to recognize X only when at the very start of a line.

To get better help, please try to explain more clearly how to recognize the lines with 'X' and 'Y'.
By contents?
By contents of lines around them?
By (relative) record number?

Cheers,
Hein.
H.Merijn Brand (procura
Honored Contributor

Re: Using VI to insert line from bash script

if it is repeated, do you want everey X replaced? Both Hein's awk solution and my perl will do that.

If you want only the first, you need a guard

$ perl -pi -e'/^X$/&&$ENV{Z}and$_.="\n".delete$ENV{Z}."\n"' FILE

Enjoy, Have FUN! H.Merijn [ who loves dirty trickery and one-liners ]
Enjoy, Have FUN! H.Merijn
AZayed
Super Advisor

Re: Using VI to insert line from bash script

Hi,

Thanks for reply, I'm sorry but the hole concept changed today,

this is a real data
,10:1
,11:1
,14:1001
17,10:1
,11:0
,12:0

I want to replace each "17,10:1" to "17,9:1/E" and insert a new line under it with a value ",10:1"

So, how can I do this with awk ?

Regards,
Success seems to be connected with action. Successful people keep moving. They make mistakes, but they don't quit.
AZayed
Super Advisor

Re: Using VI to insert line from bash script

Hi,

I wrote this in sed, I have to put it in sh script.

sed '
/^17,10:1$/ a\
,10:1
s#^17,10:1$#17,9:5/E#g'


It's work, Any more powerful Ideas ?
Success seems to be connected with action. Successful people keep moving. They make mistakes, but they don't quit.