Operating System - HP-UX
1828332 Members
5045 Online
109976 Solutions
New Discussion

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.
Hein van den Heuvel
Honored Contributor
Solution

Re: Using VI to insert line from bash script

>> 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 ?

yes, but WHY?

awk '/^17,10:1/ {gsub (/,/, ",9.5/E\n,")} {print} ' x

>> It's work, Any more powerful Ideas ?

Only if you describe in a powerful way where the 17 and the 9.1 (9.5) and such come from.
Are they really just fixed strings, always the same? That is a totally boring case. Just pick a solution and be happy.

Cheers,
Hein.
AZayed
Super Advisor

Re: Using VI to insert line from bash script

Hi Hein,

My customer changed his idea so I had to change my work. And yes, it's easy one.

But in your awk command, you didn't insert ",10:1" value.

Last thing before I close this case, do you recommend a useful awk and sed from personal experiences?

Thanks :)
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 Hein,

It seems your line working great. although, I didn't notice that you wrote ",10:1" in your line. But your awk line doing what I want do do. It's amazing :)

I want to learn how to use awk because this line is more powerful than complex bash script.

Thanks
Success seems to be connected with action. Successful people keep moving. They make mistakes, but they don't quit.
James R. Ferguson
Acclaimed Contributor

Re: Using VI to insert line from bash script

Hi:

If you want to start with 'awk' you could use this free, but excellent guide:

http://www.gnu.org/software/gawk/manual/gawk.html

Be aware that the GNU variant is a bit more powerful than HP's or AIX's standard.

Regards!

...JRF...
AZayed
Super Advisor

Re: Using VI to insert line from bash script

Thanks JRF,

Thanks folks for your support.

Have a nice day.
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

Yes,

It's awk command. It's powerful.

awk '/^17,10:1$/ { gsub (/,/, ",9:5/E\n,") } { print }'
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

>> It seems your line working great. although, I didn't notice that you wrote ",10:1" in your line. But your awk line doing what I want do do. It's amazing :)

I did't write ,10:1. That was already there :-).
I just stuck a new-line ( \n ) in front of it.

I wrote my solution in anticipation of future, other needs. That's was seperates solutions from hacks.
My assumption was that you did not want "10:1" but "whatever was on the old line after the comma"... which happens to be 10:1 for now.
Similar for the 17. I left the value itself on the line, using it only as a simple regular expression untill you explained better where the 17 came from.
I only replaced the comma, not the values.

Try this:

$ awk '/^[0-9]+,/ {gsub (/,/, ",9.5/E\n,")} {print} ' x

... no 17, no 10, and yet...


>> I want to learn how to use awk because this line is more powerful than complex bash script.


I was hoping that, and that's why I providede the blow-by-blow breakdown in my first example. Try to follow that!

Cheers,
Hein.
Dennis Handly
Acclaimed Contributor

Re: Using VI to insert line from bash script

As Duncan said, you can't use vi. You can use sed(1), awk(1), perl(1), ed(1) or ex(1).

Your example above would be:
ex a << EOF
i
insert data
.
wq
EOF