- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Using VI to insert line from bash script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2008 06:00 AM
10-20-2008 06:00 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2008 06:24 AM
10-20-2008 06:24 AM
Re: Using VI to insert line from bash script
HTH
Duncan
I am an HPE Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2008 07:55 AM
10-20-2008 07:55 AM
Re: Using VI to insert line from bash script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2008 08:02 AM
10-20-2008 08:02 AM
Re: Using VI to insert line from bash script
$ perl -pi -e'/^X$/ and $_ .= "\n$ENV{Z}\n"' FILE
Enjoy, Have FUN! H.Merijn
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2008 10:16 AM
10-20-2008 10:16 AM
Re: Using VI to insert line from bash script
$ 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.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2008 10:21 AM
10-20-2008 10:21 AM
Re: Using VI to insert line from bash script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2008 10:37 AM
10-20-2008 10:37 AM
Re: Using VI to insert line from bash script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2008 12:59 PM
10-20-2008 12:59 PM
Re: Using VI to insert line from bash script
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 ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2008 02:12 AM
10-21-2008 02:12 AM
Re: Using VI to insert line from bash script
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2008 02:46 AM
10-21-2008 02:46 AM
Re: Using VI to insert line from bash script
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 ?
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2008 03:44 AM
10-21-2008 03:44 AM
Solution>> 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2008 04:06 AM
10-21-2008 04:06 AM
Re: Using VI to insert line from bash script
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 :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2008 04:32 AM
10-21-2008 04:32 AM
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 want to learn how to use awk because this line is more powerful than complex bash script.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2008 04:38 AM
10-21-2008 04:38 AM
Re: Using VI to insert line from bash script
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2008 04:50 AM
10-21-2008 04:50 AM
Re: Using VI to insert line from bash script
Thanks folks for your support.
Have a nice day.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2008 04:52 AM
10-21-2008 04:52 AM
Re: Using VI to insert line from bash script
It's awk command. It's powerful.
awk '/^17,10:1$/ { gsub (/,/, ",9:5/E\n,") } { print }'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2008 05:44 AM
10-21-2008 05:44 AM
Re: Using VI to insert line from bash script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2008 03:31 PM
10-21-2008 03:31 PM
Re: Using VI to insert line from bash script
Your example above would be:
ex a << EOF
i
insert data
.
wq
EOF
- Tags:
- ex