How to Insert a Text At the Beginning of a File

How to insert a text at the beginning of a file?

sed can operate on an address:

$ sed -i '1s/^/<added text> /' file

What is this magical 1s you see on every answer here? Line addressing!.

Want to add <added text> on the first 10 lines?

$ sed -i '1,10s/^/<added text> /' file

Or you can use Command Grouping:

$ { echo -n '<added text> '; cat file; } >file.new
$ mv file{.new,}

Insert a new line at the beginning of a file

Here's a way to add a line to the beginning of a file:

sed -i '1s/^/line_to_be_added\n/' file

Then, you could use the code above with find to achieve your ultimate goal:

find . -type f -name '*.js' -exec sed -i '1s/^/line_to_be_added\n/' {} \;

Note: this answer was tested and works with GNU sed.

Edit: the code above would not work properly on a .js file that is empty, as the sed command above does not work as expected on empty files. A workaround to that would be to test if the file is empty, and if it is, add the desired line via echo, otherwise add the desired line via sed. This all can be done in a (long) one-liner, as follows:

find . -type f -name '*.js' -exec bash -c 'if [ ! -s "{}" ]; then echo "line_to_be_added" >> {}; else sed -i "1s/^/line_to_be_added\n/" {}; fi' \;

Edit 2: As user Sarkis Arutiunian pointed out, we need to add '' before the expression and \'$' before \n to make this work properly in MacOS sed. Here an example

sed -i '' '1s/^/line_to_be_added\'$'\n/' filename.js

Edit 3: This also works, and editors will know how to syntax highlight it:

sed -i '' $'1s/^/line_to_be_added\\\n/' filename.js

How do I insert text to the 1st line of a file using sed?

Suppose you have a file like this:

one
two

Then to append to the first line:

$ sed '1 s_$_/etc/example/live/example.com/fullchain.pem;_' file
one/etc/example/live/example.com/fullchain.pem;
two

To insert before the first line:

$ sed '1 i /etc/example/live/example.com/fullchain.pem;' file
/etc/example/live/example.com/fullchain.pem;
one
two

Or, to append after the first line:

$ sed '1 a /etc/example/live/example.com/fullchain.pem;' file
one
/etc/example/live/example.com/fullchain.pem;
two

Note the number 1 in those sed expressions - that's called the address in sed terminology. It tells you on which line the command that follows is to operate.

If your file doesn't contain the line you're addressing, the sed command won't get executed. That's why you can't insert/append on line 1, if your file is empty.

Instead of using stream editor, to append (to empty files), just use a shell redirection >>:

echo "content" >> file

How to insert a text at the beginning of multiple file?

If files are in same directory, try this:

sed -i '1s/^/id,compressedString,url,categorie,date,name\n /' x*

How do you insert a string at the beginning of a .txt file in C++

You can't insert data at the beginning of a file. You need to read the entire file into memory, insert data at the beginning, and write the entire thing back to disk. (I am assuming the file isn't too large).

Try this program.

#include <fstream>
#include <sstream>

int main()
{
std::stringstream stream;
stream << "First line\n"; // Add your line here!

{
std::ifstream file("filename.txt");
stream << file.rdbuf();
}

std::fstream new_file("filename.txt", std::ios::out);
new_file << stream.rdbuf();

return 0;
}

Easiest way to add a text to the beginning of another text file in Command Line (Windows)

echo "my line" > newFile.txt
type myOriginalFile.txt >> newFile.txt
type newFile.txt > myOriginalFile.txt

Untested.
Double >> means 'append'

Prepend line to beginning of a file

In modes 'a' or 'a+', any writing is done at the end of the file, even if at the current moment when the write() function is triggered the file's pointer is not at the end of the file: the pointer is moved to the end of file before any writing. You can do what you want in two manners.

1st way, can be used if there are no issues to load the file into memory:

def line_prepender(filename, line):
with open(filename, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write(line.rstrip('\r\n') + '\n' + content)

2nd way:

def line_pre_adder(filename, line_to_prepend):
f = fileinput.input(filename, inplace=1)
for xline in f:
if f.isfirstline():
print line_to_prepend.rstrip('\r\n') + '\n' + xline,
else:
print xline,

I don't know how this method works under the hood and if it can be employed on big big file. The argument 1 passed to input is what allows to rewrite a line in place; the following lines must be moved forwards or backwards in order that the inplace operation takes place, but I don't know the mechanism



Related Topics



Leave a reply



Submit