Prepend Data from One File to Another

Prepend data from one file to another

The following command will take the two files and merge them into one

cat file1.txt file2.txt > file3.txt; mv file3.txt file2.txt

Append data from one file to another windows command line

not sure what your end goal really is, but this is the best I can think of without using a for loop and batch file.

If you want the result on one line

set /p one=<file1.txt & set /p two=<file2.txt & echo %one% '%two%'>File1.txt

If you want the result on separate lines

set /p two=<file2.txt & echo '%two%'>>file1.txt

Append a file in the middle of another file in bash

Could you please try following and let me know if this helps you.

awk 'FNR==3{system("cat file2.txt")} 1' file1.txt

Output will be as follows.


Explanation: Checking here if line number is 3 while reading Input_file named file1.txt, if yes then using system utility of awk which will help us to call shell's commands, then I am printing the file2.txt with use of cat command. Then mentioning 1 will be printing all the lines from file1.txt. Thus we could concatenate lines from file2.txt into file1.txt.

How to append contents of multiple files into one file

You need the cat (short for concatenate) command, with shell redirection (>) into your output file

cat 1.txt 2.txt 3.txt > 0.txt

Append data from 1 file to another using AWK

All from file2 and just ignore file1 input, but I have to have the file1 name in awk command.

If you must use file1 and file2 in arguments to awk command and want to output content from file2 only then you can just use:

awk 'BEGIN {delete ARGV[1]} 1' file1 file2 > file3


delete ARGV[1] will delete first argument from argument list.

How to append data at a particular line in a file using sed , where data is from another file

Why not have sed write its own script?

sed -e "$(sed -e 's|^\(.*\)_data_to_be_appended=\(.*\)|/^\1=.*/ s//\& \2/|' cfg)" script

Inner command reads the config file and emits /^flag=.*/ s//& xyz/
which is then applied to the script file.


flag=abc xyz
echo $flag

The two escaped parenthesis pairs capture key and value as \1 and \2.
In s//& \2/ the // is the null regex which matches the last
regex used (in /^…/) and replaces the entire match (&) followed
by the captured value.

C#: Appending *contents* of one text file to another text file

No, I don't think there's anything which does this.

If the two files use the same encoding and you don't need to verify that they're valid, you can treat them as binary files, e.g.

using (Stream input = File.OpenRead("file1.txt"))
using (Stream output = new FileStream("file2.txt", FileMode.Append,
FileAccess.Write, FileShare.None))
input.CopyTo(output); // Using .NET 4

Note that if file1.txt contains a byte order mark, you should skip past this first to avoid having it in the middle of file2.txt.

If you're not using .NET 4 you can write your own equivalent of Stream.CopyTo... even with an extension method to make the hand-over seamless:

public static class StreamExtensions
public static void CopyTo(this Stream input, Stream output)
if (input == null)
throw new ArgumentNullException("input");
if (output == null)
throw new ArgumentNullException("output");
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
output.Write(buffer, 0, bytesRead);

Related Topics

Leave a reply