- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Auto embedding one file in another
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
12-21-2006 01:17 AM
12-21-2006 01:17 AM
Looking for a quick-n-dirty method to embed a 6 line file (script) into the middle of another file (script). I'd like some way to do this via another script as I have appx 1000+ files (scripts) that need to have these lines added.
Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2006 01:31 AM
- Tags:
- source
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2006 01:36 AM
12-21-2006 01:36 AM
Re: Auto embedding one file in another
Since you say "script" without further qualification, I'll asssume a shell script. In that case:
# cat /home/john/sh
#!/usr/bin/sh
X=john
. /home/john/include
Y=guardian
# cat /home/john/guardian
Z="I am!"
echo "${X} ${Y} ${Z}"
Now, when you run '/home/john/sh' the file '/home/john/include' is included "in the middle" and you get:
john guardian I am!
Note that the technique is properly called "sourcing". You use a dot (".") operator followed by a blank (space), followed by the name of the file to include.
Regards!
...JRF...
john
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2006 01:37 AM
12-21-2006 01:37 AM
Re: Auto embedding one file in another
I'm trying to eliminate as much time as possible in mod'ing 1000+ scripts IN ANY WAY...
Otherwise, I'll just create a quick script to open the 1000+ files and brute force the lines of code from script 2 into script one...
In all cases, the six lines need to be embedded into lines 2-n of all of the 1000+ scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2006 01:38 AM
12-21-2006 01:38 AM
Re: Auto embedding one file in another
you can either source the script withg . script within your first file.
Or if you want to actually include the code from file2 in file1:
sed '/regexec/r file2' file1 > new_file1
where line is the unique keyword within file1, after which you want to include the code from file2.
Example:
$ cat a2.sh
#!/usr/bin/sh
test1
this is the insert
test2
$ cat a.sh
#!/usr/bin/sh
test1
test2
$ cat b.sh
this is the insert
$ sed '/test1/r b.sh' a.sh > a2.sh
$ cat a2.sh
#!/usr/bin/sh
test1
this is the insert
test2
$
Other options of sed may give other ideas on how to position where you want the code.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2006 01:44 AM
12-21-2006 01:44 AM
Re: Auto embedding one file in another
An example is probably better. You have a file foo which you want to import into the middle of every file, straight after a line which reads "insert after here". Each file is called something.sh, so for example: -
for file in *.sh
do
ed $file <
r foo
w
q
EOF
done
You'll have to amend depending on exactly where you want the file inserting, but basically use "ed" to find the line you want, then use the "r" command to read it in, then write it out.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2006 02:40 AM
12-21-2006 02:40 AM
Re: Auto embedding one file in another
OK, I've had coffee and read you post again, noting that:
> In all cases, the six lines need to be embedded into lines 2-n of all of the 1000+ scripts.
You can do this easily with Perl. The following script inserts a file called '/tmp/insertme' into the name of the file passed following line-2 (counting one relative). That is, immediately before line-3.
You run the filter by passing as many filenames as you need. Each file is processed, the contents of '/tmp/insertme' added to the output stream at record-2, and a new file created.
The '-i.old' switch at the top of the Perl program causes a backup copy of every file passed as an argument to be made with the extension of ".old". The modified file is left in place of the original.
# cat ./myfilter
#!/usr/bin/perl -i.old
use strict;
use warnings;
my $file = "/tmp/insertme";
open( FH, "<", "$file" ) or die "Can't open include: $!\n";
while (<>) {
if ( $. == 2 ) {
local $_;
while (
print;
}
seek( FH, 0, 0 );
}
print;
close ARGV if eof;
}
1;
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2006 02:42 AM
12-21-2006 02:42 AM
Re: Auto embedding one file in another
If you'd like the file embedded at the same line number in every .sh file in the pwd, you can give this script a shot. Call it like this:
$ ./embed-file
#!/usr/bin/sh
EMBED_FILE=$1
typeset -i POS1=$2
typeset -i POS2=$(( ${POS1} + $(wc -l ${EMBED_FILE} | awk '{print $1}') -1 ))
for FILE in $(ls *.sh)
do
sed -n "1,${POS1}p" ${FILE} > ${FILE}.tmp
cat ${EMBED_FILE} >> ${FILE}.tmp
sed -n "${POS2},\$p" ${FILE} >> ${FILE}.tmp
mv ${FILE}.tmp $FILE
done
exit 0
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2006 02:43 AM
12-21-2006 02:43 AM
Re: Auto embedding one file in another
for FILE in $(ls -1
do
ex -s +"1 r source_file | wq" $FILE
done
The above command will loop thru each of the 1000+ files (assuming they are in a single dir) and embed the source file containing those 6 lines into each of them starting at line 2.
- Tags:
- ex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2006 02:52 AM
12-21-2006 02:52 AM
Re: Auto embedding one file in another
for FILE in $(ls -1
do
ex -s +"1 r source_file | wq" $FILE
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2006 03:09 AM
12-21-2006 03:09 AM
Re: Auto embedding one file in another
Here's a faster Perl solution than my first one. This one slurps (reads once) the file that you want to insert into your n-scripts and thus avoids repetitive I/O (although the buffer cache would probably reduce that overhead too).
As before, an automatic backup of your original file is made and the new file substitited in-place. That too, is an advantage of these Perl solutions.
# cat ./myfilter
#!/usr/bin/perl -i.old
use strict;
use warnings;
my $addendum;
{
my $file = "/tmp/insertme";
open( FH, "<", "$file" ) or die "Can't open include: $!\n";
local $/;
$addendum =
}
while (<>) {
print $addendum if ( $. == 3 );
print;
close ARGV if eof;
}
1;
Run this like:
# ./myfilter file1 file2 file3 ...
Backup copies will be found retained as:
file1.old, file2.old, file3.old ...
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2006 07:05 AM
12-28-2006 07:05 AM