Andy Burnelli
2022-12-19 07:43:43 UTC
How can one most easily insert contents of a file (or a line of text)
at a given line-number location inside each of hundreds of text files?
It seems that vpngate.net vpn configuration files now require a username
(of vpn) and a password (of vpn), which isn't a big deal for most people
who don't use automation, as they'd just type the login/password in when
asked by their openvpn client graphical user interface.
But I change the Time Zone & VPN server every few minutes via batch files.
It's easy to _manually (aurgh!) add at line 103 the "password.txt" line,
but what might you think is an easy way to add that line to _every_ file?
Here's what I do manually (which, admittedly, is a PITA albeit simple):
1. cd directory-containing-hundreds-of-openvpn-config-files
2. vim *.ovpn
3. :103
4. ~r include.txt
5. :write (abbreviated as :w)
6. :next (abbreviated as :n)
1. That manual process starts in a directory of many files
2. and then edits, one by one, each of a few hundred *.ovpn files;
3. and then it jumps to line 103 (it's always in the same spot);
4. and then I include the file, which contains the following line only:
auth-user-pass C:\\path\\config\\passwd.txt
5. and then I write the *.ovpn file;
6. and then I skip to the next *.ovpn file.
Where the contents of the c:\path\config\passwd.txt file is two lines:
vpn
vpn
The first being the username & the latter being the password as described
in the <http://vpngate.net> home page (this is a brand new requirement).
If I were to simply append the line to the _end_ of the *.ovpn files,
instead of inserting thatline at line 103, I could use a Windows command
line foreach loop to append the "include.txt" file to the *.ovpn file:
for %f in (*.ovpn) do type include.txt >> %f
But how can I _insert+ that one-line include.txt file at line 103?
How can one most easily insert contents of a file (or a line of text)
at a given line-number location inside each of hundreds of text files?
at a given line-number location inside each of hundreds of text files?
It seems that vpngate.net vpn configuration files now require a username
(of vpn) and a password (of vpn), which isn't a big deal for most people
who don't use automation, as they'd just type the login/password in when
asked by their openvpn client graphical user interface.
But I change the Time Zone & VPN server every few minutes via batch files.
It's easy to _manually (aurgh!) add at line 103 the "password.txt" line,
but what might you think is an easy way to add that line to _every_ file?
Here's what I do manually (which, admittedly, is a PITA albeit simple):
1. cd directory-containing-hundreds-of-openvpn-config-files
2. vim *.ovpn
3. :103
4. ~r include.txt
5. :write (abbreviated as :w)
6. :next (abbreviated as :n)
1. That manual process starts in a directory of many files
2. and then edits, one by one, each of a few hundred *.ovpn files;
3. and then it jumps to line 103 (it's always in the same spot);
4. and then I include the file, which contains the following line only:
auth-user-pass C:\\path\\config\\passwd.txt
5. and then I write the *.ovpn file;
6. and then I skip to the next *.ovpn file.
Where the contents of the c:\path\config\passwd.txt file is two lines:
vpn
vpn
The first being the username & the latter being the password as described
in the <http://vpngate.net> home page (this is a brand new requirement).
If I were to simply append the line to the _end_ of the *.ovpn files,
instead of inserting thatline at line 103, I could use a Windows command
line foreach loop to append the "include.txt" file to the *.ovpn file:
for %f in (*.ovpn) do type include.txt >> %f
But how can I _insert+ that one-line include.txt file at line 103?
How can one most easily insert contents of a file (or a line of text)
at a given line-number location inside each of hundreds of text files?