Unix command to add text to a file

Is there a Unix command to add some string data to a text file?

Something like:

prepend "to be prepended" text.txt 
+104
command-line unix prepend utilities
May 14 '12 at 16:45
source share
16 answers
 sed -i.old '1s;^;to be prepended;' inFile 
  • -i writes the change in place and backs up if any extension is specified. (In this case .old )
  • 1s;^;to be prepended; replaces the beginning of the first line with the specified replacement string using ; as a command separator.
+89
May 14 '12 at 17:01
source share
 printf '%s\n%s\n' "to be prepended" "$(cat text.txt)" >text.txt 
+150
Jan 14 '15 at 18:52
source share

Process replacement

I am surprised that no one has mentioned this.

 cat <(echo "before") text.txt > newfile.txt 

which is perhaps more natural than the accepted answer (listing something and passing it to the substitution command is lexicographically illogical).

... and the hijacking said by Ryan above, with sponge you don't need a temporary file:

 sudo apt-get install moreutils <<(echo "to be prepended") < text.txt | sponge text.txt 

UPDATE: this doesn't seem to work in Bourne Shell /bin/sh




Here's the line (zsh only)

Using here, the string is <<< , you can do:

 <<< "to be prepended" < text.txt | sponge text.txt 
+28
Mar 15 '17 at 22:55
source share

This is one of the possibilities:

 (echo "to be prepended"; cat text.txt) > newfile.txt 

it is probably not easy for you to bypass the intermediate file.

Alternatives (can be cumbersome when shielding the shell):

 sed -i '0,/^/s//to be prepended/' text.txt 
+27
May 14, '12 at 16:50
source share

This will work to shape the result. - means the standard input, which is fed through the tube from the echo.

 echo -e "to be prepended \n another line" | cat - text.txt 

To rewrite a file, a temporary file is required because it cannot go back to the input file.

 echo "to be prepended" | cat - text.txt > text.txt.tmp mv text.txt.tmp text.txt 
+16
May 14 '12 at 16:52
source share

I prefer Adam to answer

We can simplify the use of sponges . Now we do not need to create a temporary file and rename it with

 echo -e "to be prepended \n another line" | cat - text.txt | sponge text.txt 
+14
Dec 25 '15 at 8:32
source share

Probably nothing built-in, but you could write your own quite easily, for example:

 #!/bin/bash echo -n "$1" > /tmp/tmpfile.$$ cat "$2" >> /tmp/tmpfile.$$ mv /tmp/tmpfile.$$ "$2" 

Something like that at least ...

+7
May 14 '12 at 16:49
source share

If it is permissible to replace the input file :

Note. This may have unexpected side effects , especially when replacing a symbolic link to a regular file, possibly ending with different file permissions and changing the file creation (birth) date .

sed -i , as in Prince John Wesley's answer, is trying to at least restore the original permissions, but other restrictions apply .

  { printf 'line 1\nline 2\n'; cat text.txt; } > tmp.txt && mv tmp.txt text.txt 

Note. Using a group command { ...; ... } { ...; ... } more efficient than using a subshell ( (...; ...) ).




If the input file needs to be edited in place (keeping your inode with all its attributes) :

Using the venerable ed POSIX Utility :

Note: ed always reads the entire input file into memory.

 ed -s text.txt <<'EOF' 1i line 1 line 2 . w EOF 
  • -s suppressed ed status messages.
  • Notice how the commands are provided by ed as a multi -line document here ( <<'EOF' ... EOF ), i.e. via STDIN.
  • 1i makes 1 (1st row) the current row and starts insert mode ( i ).
  • The following lines are the text to be inserted before the current line, ending . in its line.
  • w writes the result back to the input file (for testing, replace w with ,p only to print the result without changing the input file).
+6
May 22, '15 at 3:00
source share

In some cases, preliminary text may only be available from stdin. Then this combination will work.

 echo "to be prepended" | cat - text.txt | tee text.txt 

If you want to omit tee output, add > /dev/null .

+6
Aug 11 '17 at 1:29 on
source share

Decision:

 printf '%s\n%s' 'text to prepend' "$(cat file.txt)" > file.txt 

Please note that this is safe for all types of inputs because there are no extensions. For example, if you want to add !@#$%^&*()ugly text\n\t\n , it will work:

 printf '%s\n%s' '!@#$%^&*()ugly text\n\t\n' "$(cat file.txt)" > file.txt 

The last part left for consideration is the removal of spaces at the end of the file during the substitution of the command "$(cat file.txt)" . All workarounds for this are relatively complex. If you want to keep new lines at the end of file.txt, see This: https://stackoverflow.com/a/16829/

+6
Nov 20 '17 at 19:36
source share

Another way: sed :

 sed -i.old '1 {i to be prepended }' inFile 

If the line to be added is multi-line:

 sed -i.old '1 {i\ to be prepended\ multiline }' inFile 
+3
Oct 06 '14 at 12:48
source share

As tested in Bash (in Ubuntu), if you start with a test file through;

echo "Original Line" > test_file.txt

you can execute;

echo "$(echo "New Line"; cat test_file.txt)" > test_file.txt

or, if the bash version is too old for $ (), you can use backticks;

echo "'echo "New Line"; cat test_file.txt'" > test_file.txt

and get the following contents of "test_file.txt";

 New Line Original Line 

There is no intermediate file, just bash / echo.

+3
Feb 07 '19 at 19:15
source share

Another pretty straightforward solution:

  $ echo -e "string\n" $(cat file) 
+2
Jul 16 '15 at 14:21
source share

If you like vi / vim, this might be more than your style.

 printf '0i\n%s\n.\nwq\n' prepend-text | ed file 
+1
Jan 21 '15 at 23:09
source share
 # create a file with content.. echo foo > /tmp/foo # prepend a line containing "jim" to the file sed -i "1s/^/jim\n/" /tmp/foo # verify the content of the file has the new line prepened to it cat /tmp/foo 
0
Apr 16 '15 at 21:34
source share

I would recommend defining a function and then importing and using it where necessary.

 prepend_to_file() { file=$1 text=$2 if ! [[ -f $file ]] then touch $file fi echo "$text" | cat - $file > $file.new mv -f $file.new $file } 

Then use it like this:

 prepend_to_file test.txt "This is first" prepend_to_file test.txt "This is second" 

In this case, the contents of your file will be:

 This is second This is first 

I am going to use this approach to implement a change log update tool.

0
Jun 21 '18 at 16:43
source share



All Articles