awk - the most common and will be found on most Unix-like systems, oldest version and inferior to newer ones.
mawk - fast AWK implementation which it's code base is based on a byte-code interpreter.
nawk - while the AWK language was being developed the authors released a new version (hence the n - new awk) to avoid confusion. Think of it like the Python 3.0 of AWK.
gawk - abbreviated from GNU awk. The only version in which the developers attempted to add i18n support. Allowed users to write their own C shared libraries to extend it with their own "plug-ins". This version is the standard implementation for Linux, original AWK was written for Unix v7.
There are other versions like jawk (java implementation), bwk (Brian W. Kernighan's implementation) and so on.
Create a file with the following code. You could save it as:
insertfile.awk (or whatever you wish)
BEGIN{
while ( (getline < outerfile) > 0 )
{
if ($0 == matchline)
{
if ("after" == includematch)
{
print
}
if ("before" == includematch)
{
holdline = $0
}
while ( (getline < innerfile) > 0)
{
print
}
close(innerfile)
if ("before" == includematch)
{
print holdline
holdline = ""
}
}
else
{
print
}
}
close(outerfile)
}
The awk command line parameters used are:
-v outerfile="file1.txt" This is the name of the file you are searching (and printing).
-v innerfile="file2.txt" This is the name of the file you will insert when you file a match
-v matchline="Search Text" This is what you will search for as a line in file1.txt
-v includematch="before" Optional: insert the file, before the matchline
-v includematch="after" Optional: insert the file, after the matchline
-v includematch="" If "includematch" is any other value, or empty, or not present,
then insert the file, REPLACING matchline.
-f "insertfile.awk" This is the name of the awk command file.
Then, to use it you call awk like this:
awk -v outerfile="file1.txt" -v innerfile="file2.txt" -v matchline="cat" -f "insertfile.awk"
(Read and print "file1.txt". Search for line containing only "cat". REPLACE "cat" lines with "file2.txt"
awk -v outerfile="file1.txt" -v innerfile="file2.txt" -v matchline="dog" -v includematch="before" -f "insertfile.awk"
(Read and print "file1.txt". Search for line containing only "dog". Insert "file2.txt" Before matched line.
awk -v outerfile="file1.txt" -v innerfile="file2.txt" -v matchline="bird" -v includematch="after" -f "insertfile.awk"
(Read and print "file1.txt". Search for line containing only "bird". Insert "file2.txt" After matched line.
In the awk script, you can edit it like this:
Change $0 to $1 or $2 or other to match a specific word instead of the whole line.
"hard-code" the file-names instead of outerfile and innerfile if you wish.
If you want to "pipe" the input data into the script instead of getting it from a file, edit the insertfile.awk script like this:
{
if ($0 == matchline)
{
if ("after" == includematch)
{
print
}
if ("before" == includematch)
{
holdline = $0
}
while ( (getline < innerfile) > 0)
{
print
}
close(innerfile)
if ("before" == includematch)
{
print holdline
holdline = ""
}
}
else
{
print
}
close(outerfile)
}
Then, to use it you call awk like this:
type "somefile.txt" | awk -v innerfile="file2.txt" -v matchline="cat" -f "insertfile.awk"
(Read and print STDIN. Search for line containing only "cat". REPLACE "cat" lines with "file2.txt"
type "anyfile.txt" | awk -v innerfile="file2.txt" -v matchline="dog" -v includematch="before" -f "insertfile.awk"
(Read and print STDIN. Search for line containg only "dog". Insert "file2.txt" Before matched line.
type "otherfile.txt" | awk -v innerfile="file2.txt" -v matchline="bird" -v includematch="after" -f "insertfile.awk"
(Read and print STDIN. Search for line containg only "bird". Insert "file2.txt" After matched line.
Best Answer
In this case,
man awk
shows us:In my case,