How to append multiple lines to a fileAdd new line character in a text file using sedInsert block of text in...
How can this older-style irrigation tee be replaced?
A continuous water "planet" ring around a star
Why isn’t SHA-3 in wider use?
Double redundancy for the Saturn V LVDC computer memory, how were disagreements resolved?
Is there a standardised way to check fake news?
Write an interpreter for *
Can "être sur" mean "to be about" ?
Two matrices that are not similar have (almost) same eigenvalues
The cat ate your input again!
If a digital camera can be "hacked" in the ransomware sense, how best to protect it?
TreeView class in WPF class
Why does the standard fingering / strumming for a D maj chord leave out the 5th string?
Can this limit be evaluated without L'Hopital rule?
DeclareMathOperator and widearcarrow with kpfonts
Email address etiquette - Which address should I use to contact professors?
Why are Tucker and Malcolm not dead?
What is my malfunctioning AI harvesting from humans?
If an Animated Object is given a task that takes longer than a minute to do, does the object stay animated longer than a minute?
Why would humanity have left Earth 2 so quickly?
Loading military units into ships optimally, using backtracking
Should I ask for permission to write an expository post about someone else's research?
Annotating a table with arrows
Trying to write a shell script that keeps testing a server remotely, but it keeps falling in else statement when I logout
Using double offset in QGIS Expression String Builder?
How to append multiple lines to a file
Add new line character in a text file using sedInsert block of text in fileHow to append multiple lines to a file with bash, with “--” in front of stringShell script: How can I write multiline content to file if the file doesn't exist?How to prepend multiple lines to a fileRemove end of line characters from stdout? Multiple lines into a single lineHow to produce a file encoded in Mac OS RomanTokenize string from $REPLY in bash scriptHow bash treats “> >()”Join lines of text with repeated beginningBash script to edit text in csv-formatLinux Mint - Right click URL in bash, select “open link”. And I get this errorUsing multiple commands with command substitution to populate an array bad?How to remove a group of lines from a file?Why use cat to view a file?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I am writing a bash script to look for a file if it doesn't exist then create it and append this to it:
Host localhost
ForwardAgent yes
So "line then new line 'tab' then text"
I think its a sensitive format.
I know you can do this:
cat temp.txt >> data.txt
But it seems weird since its two lines. Is there a way to append that in this format:
echo "hello" >> greetings.txt
shell text-processing io-redirection
add a comment |
I am writing a bash script to look for a file if it doesn't exist then create it and append this to it:
Host localhost
ForwardAgent yes
So "line then new line 'tab' then text"
I think its a sensitive format.
I know you can do this:
cat temp.txt >> data.txt
But it seems weird since its two lines. Is there a way to append that in this format:
echo "hello" >> greetings.txt
shell text-processing io-redirection
add a comment |
I am writing a bash script to look for a file if it doesn't exist then create it and append this to it:
Host localhost
ForwardAgent yes
So "line then new line 'tab' then text"
I think its a sensitive format.
I know you can do this:
cat temp.txt >> data.txt
But it seems weird since its two lines. Is there a way to append that in this format:
echo "hello" >> greetings.txt
shell text-processing io-redirection
I am writing a bash script to look for a file if it doesn't exist then create it and append this to it:
Host localhost
ForwardAgent yes
So "line then new line 'tab' then text"
I think its a sensitive format.
I know you can do this:
cat temp.txt >> data.txt
But it seems weird since its two lines. Is there a way to append that in this format:
echo "hello" >> greetings.txt
shell text-processing io-redirection
shell text-processing io-redirection
edited Apr 14 '17 at 14:04
don_crissti
54.3k17 gold badges149 silver badges177 bronze badges
54.3k17 gold badges149 silver badges177 bronze badges
asked May 27 '13 at 14:49
TheLegendTheLegend
1,7623 gold badges10 silver badges14 bronze badges
1,7623 gold badges10 silver badges14 bronze badges
add a comment |
add a comment |
10 Answers
10
active
oldest
votes
# possibility 1:
echo "line 1" >> greetings.txt
echo "line 2" >> greetings.txt
# possibility 2:
echo "line 1
line 2" >> greetings.txt
# possibility 3:
cat <<EOT >> greetings.txt
line 1
line 2
EOT
If sudo (other user privileges) is needed to write to the file, use this:
# possibility 1:
echo "line 1" | sudo tee -a greetings.txt > /dev/null
# possibility 3:
sudo tee -a greetings.txt > /dev/null <<EOT
line 1
line 2
EOT
2
@TheLegend That is called a "here document". Have a look at that paragraph in the man page.
– Hauke Laging
May 27 '13 at 15:00
1
One more possibility is( echo "line 1" ; echo "line 2" ) >>greetings.txt
.
– ott--
May 27 '13 at 15:38
2
@ott-- You don't need a real subshell (i.e. can save one new process), this is enough:{ echo "line 1" ; echo "line 2"; } >>greetings.txt
– Hauke Laging
May 27 '13 at 16:10
2
whts the difference between EOT and EOL?
– cikatomo
Jan 9 '16 at 18:34
15
@cikatomo Incat <<EOT
theEOT
is just a random string. Could becat <<FOO
, too.
– Hauke Laging
Jan 9 '16 at 20:37
|
show 6 more comments
printf '%sn %sn' 'Host localhost' 'ForwardAgent yes' >> file.txt
Or, if it's a literal tab that you want (rather than the four spaces in your question):
printf '%snt%sn' 'Host localhost' 'ForwardAgent yes' >> file.txt
You can achieve the same effect with echo
, but exactly how varies from implementation to implementation, whereas printf
is constant.
add a comment |
echo -e "Hello nWorld n" >> greetings.txt
add a comment |
Another approach is to use tee
tee -a ~/.ssh/config << END
Host localhost
ForwardAgent yes
END
A few choice lines from tee
's man page:
The tee utility copies standard input to standard output, making a
copy in zero or more files.
-a - Append the output to the files rather than overwriting them.
add a comment |
SED can append a line to the end of a file like so:sed -i '$ a text to be inserted' fileName.file
$
selects end of file, the a
tells it to append, and after this comes the text that is to be inserted. Then of course the file name.
Source:
http://www.yourownlinux.com/2015/04/sed-command-in-linux-append-and-insert-lines-to-file.html
==========EDIT============
Does this approach have any added benefit than other solutions?
Yes, this approach has the added benefit of appending to any files return in a search, such as this:
find . -name "*.html" -exec sed -i '$ a </html>' {} ;
I used the above example to insert the ending html tag that was missing on every html page within a number of directories.
===================================
1
If you would show the syntax for appending multiple lines to a file, then this would be an answer to the question (although not a very useful one). All the other answers simply write the new text. This answer reads the entire file and rewrites it, plus the added text. Does this approach have any benefit over the others?
– G-Man
Mar 18 '16 at 20:09
This only works with GNUsed
. BSDsed
bails with the error:command a expects followed by text
. So don't use this for a scripted solution; it's liable to break. (Or do it properly as the BSD required syntax is POSIX compatible and will work on GNUsed
as well.
– Wildcard
Mar 18 '16 at 20:28
The other answers can be used withfind
as follows:find . -name "*.txt" -exec sh -c 'printf "%snt%sn" "Host localhost" "ForwardAgent yes" >> "$1"' sh {} ";"
.
– G-Man
Mar 19 '16 at 4:12
1
This is a very good solution, as it does not do stream redirection (>
,>>
) and thus does not suffer from their problems such as runningsudo echo 'something' > /file
. For me it perfectly allows to append a line to a file in lxd container like that:lxc exec c sed -- '$ a newline' /myfile
.
– Draco Ater
Aug 23 '16 at 19:19
Theecho "a new line" >> foo.file
will not create a new line when the file is not end of new line, butsed -i '$ a a new line' foo.file
will do, sosed
is better especially you want always append a new line to the file.
– zhouji
Sep 12 '16 at 10:27
add a comment |
I used sed because it can be used with sudo. For example:
sudo sed -i '$ a text to be inserted' fileName.file
the alternative is very ugly like :
sudo bash -c "echo a text to be inserted >> fileName.file"
and even uglier when done with ssh.
1
Why was this answer downvoted? This looks like the solution I need. Is there any problem with it? If not, I'll use it and upvote it.
– MountainX
Jul 23 '16 at 8:20
Does yoursed -i
not take an extension as argument? Your example may have incorrect parameters.
– protometa
Jan 4 '17 at 19:57
add a comment |
Another one liner is:
echo "Greetings 1" >> greetings.txt && echo "Greetings 2" >> greetings.txt
I'd prefer the -e
option thought, as it gives more control:
echo -e "Greeting 1nGreetings 2n" >> greetings.txt
add a comment |
One can emulate cat >> out.txt
with either Perl or Python to achieve same effect. Perl:
perl -e 'open(fh,">>","./out.txt");while(<>){printf(fh "%s",$_)};close(fh);'
And Python:
python -c 'import sys;f=open(sys.argv[1],"a");l=[i for i in sys.stdin];f.write("".join(l))' out.txt
Note that for python you'll have to hit Ctrl+D twice. See related question on stackoverflow for more info.
add a comment |
Here is an example to append multiple lines in a file:
{
echo ' directory "/var/cache/bind";'
echo ' listen-on { 127.0.0.1; };'
echo ' listen-on-v6 { none; };'
echo ' version "";'
echo ' auth-nxdomain no;'
echo ' forward only;'
echo ' forwarders { 8.8.8.8; 8.8.4.4; };'
echo ' dnssec-enable no;'
echo ' dnssec-validation no;'
} >> your_file.txt
add a comment |
In addition to main answer, in case the file needs super user permissions, just adding sudo
in front of echo
won't work.
This is because shell breaks the commands and though echo
did run as root, but >>
ran with normal privileges.
This will work for super user:
sudo su -c "echo 'Line 3' >> greetings.txt"
You don't need thesu
here (sudo su
is not good). But you're right in that you need something do manage the shell redirection; perhapssudo sh -c "echo stuff >> file.txt"
. No net saving but a better practice.
– roaima
Aug 8 at 13:51
add a comment |
protected by Archemar Aug 8 at 13:24
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
# possibility 1:
echo "line 1" >> greetings.txt
echo "line 2" >> greetings.txt
# possibility 2:
echo "line 1
line 2" >> greetings.txt
# possibility 3:
cat <<EOT >> greetings.txt
line 1
line 2
EOT
If sudo (other user privileges) is needed to write to the file, use this:
# possibility 1:
echo "line 1" | sudo tee -a greetings.txt > /dev/null
# possibility 3:
sudo tee -a greetings.txt > /dev/null <<EOT
line 1
line 2
EOT
2
@TheLegend That is called a "here document". Have a look at that paragraph in the man page.
– Hauke Laging
May 27 '13 at 15:00
1
One more possibility is( echo "line 1" ; echo "line 2" ) >>greetings.txt
.
– ott--
May 27 '13 at 15:38
2
@ott-- You don't need a real subshell (i.e. can save one new process), this is enough:{ echo "line 1" ; echo "line 2"; } >>greetings.txt
– Hauke Laging
May 27 '13 at 16:10
2
whts the difference between EOT and EOL?
– cikatomo
Jan 9 '16 at 18:34
15
@cikatomo Incat <<EOT
theEOT
is just a random string. Could becat <<FOO
, too.
– Hauke Laging
Jan 9 '16 at 20:37
|
show 6 more comments
# possibility 1:
echo "line 1" >> greetings.txt
echo "line 2" >> greetings.txt
# possibility 2:
echo "line 1
line 2" >> greetings.txt
# possibility 3:
cat <<EOT >> greetings.txt
line 1
line 2
EOT
If sudo (other user privileges) is needed to write to the file, use this:
# possibility 1:
echo "line 1" | sudo tee -a greetings.txt > /dev/null
# possibility 3:
sudo tee -a greetings.txt > /dev/null <<EOT
line 1
line 2
EOT
2
@TheLegend That is called a "here document". Have a look at that paragraph in the man page.
– Hauke Laging
May 27 '13 at 15:00
1
One more possibility is( echo "line 1" ; echo "line 2" ) >>greetings.txt
.
– ott--
May 27 '13 at 15:38
2
@ott-- You don't need a real subshell (i.e. can save one new process), this is enough:{ echo "line 1" ; echo "line 2"; } >>greetings.txt
– Hauke Laging
May 27 '13 at 16:10
2
whts the difference between EOT and EOL?
– cikatomo
Jan 9 '16 at 18:34
15
@cikatomo Incat <<EOT
theEOT
is just a random string. Could becat <<FOO
, too.
– Hauke Laging
Jan 9 '16 at 20:37
|
show 6 more comments
# possibility 1:
echo "line 1" >> greetings.txt
echo "line 2" >> greetings.txt
# possibility 2:
echo "line 1
line 2" >> greetings.txt
# possibility 3:
cat <<EOT >> greetings.txt
line 1
line 2
EOT
If sudo (other user privileges) is needed to write to the file, use this:
# possibility 1:
echo "line 1" | sudo tee -a greetings.txt > /dev/null
# possibility 3:
sudo tee -a greetings.txt > /dev/null <<EOT
line 1
line 2
EOT
# possibility 1:
echo "line 1" >> greetings.txt
echo "line 2" >> greetings.txt
# possibility 2:
echo "line 1
line 2" >> greetings.txt
# possibility 3:
cat <<EOT >> greetings.txt
line 1
line 2
EOT
If sudo (other user privileges) is needed to write to the file, use this:
# possibility 1:
echo "line 1" | sudo tee -a greetings.txt > /dev/null
# possibility 3:
sudo tee -a greetings.txt > /dev/null <<EOT
line 1
line 2
EOT
edited 1 hour ago
Isaac
14.4k1 gold badge23 silver badges62 bronze badges
14.4k1 gold badge23 silver badges62 bronze badges
answered May 27 '13 at 14:54
Hauke LagingHauke Laging
59.7k12 gold badges93 silver badges140 bronze badges
59.7k12 gold badges93 silver badges140 bronze badges
2
@TheLegend That is called a "here document". Have a look at that paragraph in the man page.
– Hauke Laging
May 27 '13 at 15:00
1
One more possibility is( echo "line 1" ; echo "line 2" ) >>greetings.txt
.
– ott--
May 27 '13 at 15:38
2
@ott-- You don't need a real subshell (i.e. can save one new process), this is enough:{ echo "line 1" ; echo "line 2"; } >>greetings.txt
– Hauke Laging
May 27 '13 at 16:10
2
whts the difference between EOT and EOL?
– cikatomo
Jan 9 '16 at 18:34
15
@cikatomo Incat <<EOT
theEOT
is just a random string. Could becat <<FOO
, too.
– Hauke Laging
Jan 9 '16 at 20:37
|
show 6 more comments
2
@TheLegend That is called a "here document". Have a look at that paragraph in the man page.
– Hauke Laging
May 27 '13 at 15:00
1
One more possibility is( echo "line 1" ; echo "line 2" ) >>greetings.txt
.
– ott--
May 27 '13 at 15:38
2
@ott-- You don't need a real subshell (i.e. can save one new process), this is enough:{ echo "line 1" ; echo "line 2"; } >>greetings.txt
– Hauke Laging
May 27 '13 at 16:10
2
whts the difference between EOT and EOL?
– cikatomo
Jan 9 '16 at 18:34
15
@cikatomo Incat <<EOT
theEOT
is just a random string. Could becat <<FOO
, too.
– Hauke Laging
Jan 9 '16 at 20:37
2
2
@TheLegend That is called a "here document". Have a look at that paragraph in the man page.
– Hauke Laging
May 27 '13 at 15:00
@TheLegend That is called a "here document". Have a look at that paragraph in the man page.
– Hauke Laging
May 27 '13 at 15:00
1
1
One more possibility is
( echo "line 1" ; echo "line 2" ) >>greetings.txt
.– ott--
May 27 '13 at 15:38
One more possibility is
( echo "line 1" ; echo "line 2" ) >>greetings.txt
.– ott--
May 27 '13 at 15:38
2
2
@ott-- You don't need a real subshell (i.e. can save one new process), this is enough:
{ echo "line 1" ; echo "line 2"; } >>greetings.txt
– Hauke Laging
May 27 '13 at 16:10
@ott-- You don't need a real subshell (i.e. can save one new process), this is enough:
{ echo "line 1" ; echo "line 2"; } >>greetings.txt
– Hauke Laging
May 27 '13 at 16:10
2
2
whts the difference between EOT and EOL?
– cikatomo
Jan 9 '16 at 18:34
whts the difference between EOT and EOL?
– cikatomo
Jan 9 '16 at 18:34
15
15
@cikatomo In
cat <<EOT
the EOT
is just a random string. Could be cat <<FOO
, too.– Hauke Laging
Jan 9 '16 at 20:37
@cikatomo In
cat <<EOT
the EOT
is just a random string. Could be cat <<FOO
, too.– Hauke Laging
Jan 9 '16 at 20:37
|
show 6 more comments
printf '%sn %sn' 'Host localhost' 'ForwardAgent yes' >> file.txt
Or, if it's a literal tab that you want (rather than the four spaces in your question):
printf '%snt%sn' 'Host localhost' 'ForwardAgent yes' >> file.txt
You can achieve the same effect with echo
, but exactly how varies from implementation to implementation, whereas printf
is constant.
add a comment |
printf '%sn %sn' 'Host localhost' 'ForwardAgent yes' >> file.txt
Or, if it's a literal tab that you want (rather than the four spaces in your question):
printf '%snt%sn' 'Host localhost' 'ForwardAgent yes' >> file.txt
You can achieve the same effect with echo
, but exactly how varies from implementation to implementation, whereas printf
is constant.
add a comment |
printf '%sn %sn' 'Host localhost' 'ForwardAgent yes' >> file.txt
Or, if it's a literal tab that you want (rather than the four spaces in your question):
printf '%snt%sn' 'Host localhost' 'ForwardAgent yes' >> file.txt
You can achieve the same effect with echo
, but exactly how varies from implementation to implementation, whereas printf
is constant.
printf '%sn %sn' 'Host localhost' 'ForwardAgent yes' >> file.txt
Or, if it's a literal tab that you want (rather than the four spaces in your question):
printf '%snt%sn' 'Host localhost' 'ForwardAgent yes' >> file.txt
You can achieve the same effect with echo
, but exactly how varies from implementation to implementation, whereas printf
is constant.
edited Oct 5 '14 at 9:42
kyrias
3,4151 gold badge18 silver badges30 bronze badges
3,4151 gold badge18 silver badges30 bronze badges
answered May 27 '13 at 14:57
evilsoupevilsoup
4,5592 gold badges20 silver badges37 bronze badges
4,5592 gold badges20 silver badges37 bronze badges
add a comment |
add a comment |
echo -e "Hello nWorld n" >> greetings.txt
add a comment |
echo -e "Hello nWorld n" >> greetings.txt
add a comment |
echo -e "Hello nWorld n" >> greetings.txt
echo -e "Hello nWorld n" >> greetings.txt
answered Jul 30 '14 at 16:21
kendotwillkendotwill
4294 silver badges2 bronze badges
4294 silver badges2 bronze badges
add a comment |
add a comment |
Another approach is to use tee
tee -a ~/.ssh/config << END
Host localhost
ForwardAgent yes
END
A few choice lines from tee
's man page:
The tee utility copies standard input to standard output, making a
copy in zero or more files.
-a - Append the output to the files rather than overwriting them.
add a comment |
Another approach is to use tee
tee -a ~/.ssh/config << END
Host localhost
ForwardAgent yes
END
A few choice lines from tee
's man page:
The tee utility copies standard input to standard output, making a
copy in zero or more files.
-a - Append the output to the files rather than overwriting them.
add a comment |
Another approach is to use tee
tee -a ~/.ssh/config << END
Host localhost
ForwardAgent yes
END
A few choice lines from tee
's man page:
The tee utility copies standard input to standard output, making a
copy in zero or more files.
-a - Append the output to the files rather than overwriting them.
Another approach is to use tee
tee -a ~/.ssh/config << END
Host localhost
ForwardAgent yes
END
A few choice lines from tee
's man page:
The tee utility copies standard input to standard output, making a
copy in zero or more files.
-a - Append the output to the files rather than overwriting them.
answered Aug 8 '17 at 11:09
user206934
add a comment |
add a comment |
SED can append a line to the end of a file like so:sed -i '$ a text to be inserted' fileName.file
$
selects end of file, the a
tells it to append, and after this comes the text that is to be inserted. Then of course the file name.
Source:
http://www.yourownlinux.com/2015/04/sed-command-in-linux-append-and-insert-lines-to-file.html
==========EDIT============
Does this approach have any added benefit than other solutions?
Yes, this approach has the added benefit of appending to any files return in a search, such as this:
find . -name "*.html" -exec sed -i '$ a </html>' {} ;
I used the above example to insert the ending html tag that was missing on every html page within a number of directories.
===================================
1
If you would show the syntax for appending multiple lines to a file, then this would be an answer to the question (although not a very useful one). All the other answers simply write the new text. This answer reads the entire file and rewrites it, plus the added text. Does this approach have any benefit over the others?
– G-Man
Mar 18 '16 at 20:09
This only works with GNUsed
. BSDsed
bails with the error:command a expects followed by text
. So don't use this for a scripted solution; it's liable to break. (Or do it properly as the BSD required syntax is POSIX compatible and will work on GNUsed
as well.
– Wildcard
Mar 18 '16 at 20:28
The other answers can be used withfind
as follows:find . -name "*.txt" -exec sh -c 'printf "%snt%sn" "Host localhost" "ForwardAgent yes" >> "$1"' sh {} ";"
.
– G-Man
Mar 19 '16 at 4:12
1
This is a very good solution, as it does not do stream redirection (>
,>>
) and thus does not suffer from their problems such as runningsudo echo 'something' > /file
. For me it perfectly allows to append a line to a file in lxd container like that:lxc exec c sed -- '$ a newline' /myfile
.
– Draco Ater
Aug 23 '16 at 19:19
Theecho "a new line" >> foo.file
will not create a new line when the file is not end of new line, butsed -i '$ a a new line' foo.file
will do, sosed
is better especially you want always append a new line to the file.
– zhouji
Sep 12 '16 at 10:27
add a comment |
SED can append a line to the end of a file like so:sed -i '$ a text to be inserted' fileName.file
$
selects end of file, the a
tells it to append, and after this comes the text that is to be inserted. Then of course the file name.
Source:
http://www.yourownlinux.com/2015/04/sed-command-in-linux-append-and-insert-lines-to-file.html
==========EDIT============
Does this approach have any added benefit than other solutions?
Yes, this approach has the added benefit of appending to any files return in a search, such as this:
find . -name "*.html" -exec sed -i '$ a </html>' {} ;
I used the above example to insert the ending html tag that was missing on every html page within a number of directories.
===================================
1
If you would show the syntax for appending multiple lines to a file, then this would be an answer to the question (although not a very useful one). All the other answers simply write the new text. This answer reads the entire file and rewrites it, plus the added text. Does this approach have any benefit over the others?
– G-Man
Mar 18 '16 at 20:09
This only works with GNUsed
. BSDsed
bails with the error:command a expects followed by text
. So don't use this for a scripted solution; it's liable to break. (Or do it properly as the BSD required syntax is POSIX compatible and will work on GNUsed
as well.
– Wildcard
Mar 18 '16 at 20:28
The other answers can be used withfind
as follows:find . -name "*.txt" -exec sh -c 'printf "%snt%sn" "Host localhost" "ForwardAgent yes" >> "$1"' sh {} ";"
.
– G-Man
Mar 19 '16 at 4:12
1
This is a very good solution, as it does not do stream redirection (>
,>>
) and thus does not suffer from their problems such as runningsudo echo 'something' > /file
. For me it perfectly allows to append a line to a file in lxd container like that:lxc exec c sed -- '$ a newline' /myfile
.
– Draco Ater
Aug 23 '16 at 19:19
Theecho "a new line" >> foo.file
will not create a new line when the file is not end of new line, butsed -i '$ a a new line' foo.file
will do, sosed
is better especially you want always append a new line to the file.
– zhouji
Sep 12 '16 at 10:27
add a comment |
SED can append a line to the end of a file like so:sed -i '$ a text to be inserted' fileName.file
$
selects end of file, the a
tells it to append, and after this comes the text that is to be inserted. Then of course the file name.
Source:
http://www.yourownlinux.com/2015/04/sed-command-in-linux-append-and-insert-lines-to-file.html
==========EDIT============
Does this approach have any added benefit than other solutions?
Yes, this approach has the added benefit of appending to any files return in a search, such as this:
find . -name "*.html" -exec sed -i '$ a </html>' {} ;
I used the above example to insert the ending html tag that was missing on every html page within a number of directories.
===================================
SED can append a line to the end of a file like so:sed -i '$ a text to be inserted' fileName.file
$
selects end of file, the a
tells it to append, and after this comes the text that is to be inserted. Then of course the file name.
Source:
http://www.yourownlinux.com/2015/04/sed-command-in-linux-append-and-insert-lines-to-file.html
==========EDIT============
Does this approach have any added benefit than other solutions?
Yes, this approach has the added benefit of appending to any files return in a search, such as this:
find . -name "*.html" -exec sed -i '$ a </html>' {} ;
I used the above example to insert the ending html tag that was missing on every html page within a number of directories.
===================================
edited Mar 18 '16 at 23:46
answered Mar 18 '16 at 19:40
OB7DEVOB7DEV
591 silver badge2 bronze badges
591 silver badge2 bronze badges
1
If you would show the syntax for appending multiple lines to a file, then this would be an answer to the question (although not a very useful one). All the other answers simply write the new text. This answer reads the entire file and rewrites it, plus the added text. Does this approach have any benefit over the others?
– G-Man
Mar 18 '16 at 20:09
This only works with GNUsed
. BSDsed
bails with the error:command a expects followed by text
. So don't use this for a scripted solution; it's liable to break. (Or do it properly as the BSD required syntax is POSIX compatible and will work on GNUsed
as well.
– Wildcard
Mar 18 '16 at 20:28
The other answers can be used withfind
as follows:find . -name "*.txt" -exec sh -c 'printf "%snt%sn" "Host localhost" "ForwardAgent yes" >> "$1"' sh {} ";"
.
– G-Man
Mar 19 '16 at 4:12
1
This is a very good solution, as it does not do stream redirection (>
,>>
) and thus does not suffer from their problems such as runningsudo echo 'something' > /file
. For me it perfectly allows to append a line to a file in lxd container like that:lxc exec c sed -- '$ a newline' /myfile
.
– Draco Ater
Aug 23 '16 at 19:19
Theecho "a new line" >> foo.file
will not create a new line when the file is not end of new line, butsed -i '$ a a new line' foo.file
will do, sosed
is better especially you want always append a new line to the file.
– zhouji
Sep 12 '16 at 10:27
add a comment |
1
If you would show the syntax for appending multiple lines to a file, then this would be an answer to the question (although not a very useful one). All the other answers simply write the new text. This answer reads the entire file and rewrites it, plus the added text. Does this approach have any benefit over the others?
– G-Man
Mar 18 '16 at 20:09
This only works with GNUsed
. BSDsed
bails with the error:command a expects followed by text
. So don't use this for a scripted solution; it's liable to break. (Or do it properly as the BSD required syntax is POSIX compatible and will work on GNUsed
as well.
– Wildcard
Mar 18 '16 at 20:28
The other answers can be used withfind
as follows:find . -name "*.txt" -exec sh -c 'printf "%snt%sn" "Host localhost" "ForwardAgent yes" >> "$1"' sh {} ";"
.
– G-Man
Mar 19 '16 at 4:12
1
This is a very good solution, as it does not do stream redirection (>
,>>
) and thus does not suffer from their problems such as runningsudo echo 'something' > /file
. For me it perfectly allows to append a line to a file in lxd container like that:lxc exec c sed -- '$ a newline' /myfile
.
– Draco Ater
Aug 23 '16 at 19:19
Theecho "a new line" >> foo.file
will not create a new line when the file is not end of new line, butsed -i '$ a a new line' foo.file
will do, sosed
is better especially you want always append a new line to the file.
– zhouji
Sep 12 '16 at 10:27
1
1
If you would show the syntax for appending multiple lines to a file, then this would be an answer to the question (although not a very useful one). All the other answers simply write the new text. This answer reads the entire file and rewrites it, plus the added text. Does this approach have any benefit over the others?
– G-Man
Mar 18 '16 at 20:09
If you would show the syntax for appending multiple lines to a file, then this would be an answer to the question (although not a very useful one). All the other answers simply write the new text. This answer reads the entire file and rewrites it, plus the added text. Does this approach have any benefit over the others?
– G-Man
Mar 18 '16 at 20:09
This only works with GNU
sed
. BSD sed
bails with the error: command a expects followed by text
. So don't use this for a scripted solution; it's liable to break. (Or do it properly as the BSD required syntax is POSIX compatible and will work on GNU sed
as well.– Wildcard
Mar 18 '16 at 20:28
This only works with GNU
sed
. BSD sed
bails with the error: command a expects followed by text
. So don't use this for a scripted solution; it's liable to break. (Or do it properly as the BSD required syntax is POSIX compatible and will work on GNU sed
as well.– Wildcard
Mar 18 '16 at 20:28
The other answers can be used with
find
as follows: find . -name "*.txt" -exec sh -c 'printf "%snt%sn" "Host localhost" "ForwardAgent yes" >> "$1"' sh {} ";"
.– G-Man
Mar 19 '16 at 4:12
The other answers can be used with
find
as follows: find . -name "*.txt" -exec sh -c 'printf "%snt%sn" "Host localhost" "ForwardAgent yes" >> "$1"' sh {} ";"
.– G-Man
Mar 19 '16 at 4:12
1
1
This is a very good solution, as it does not do stream redirection (
>
, >>
) and thus does not suffer from their problems such as running sudo echo 'something' > /file
. For me it perfectly allows to append a line to a file in lxd container like that: lxc exec c sed -- '$ a newline' /myfile
.– Draco Ater
Aug 23 '16 at 19:19
This is a very good solution, as it does not do stream redirection (
>
, >>
) and thus does not suffer from their problems such as running sudo echo 'something' > /file
. For me it perfectly allows to append a line to a file in lxd container like that: lxc exec c sed -- '$ a newline' /myfile
.– Draco Ater
Aug 23 '16 at 19:19
The
echo "a new line" >> foo.file
will not create a new line when the file is not end of new line, but sed -i '$ a a new line' foo.file
will do, so sed
is better especially you want always append a new line to the file.– zhouji
Sep 12 '16 at 10:27
The
echo "a new line" >> foo.file
will not create a new line when the file is not end of new line, but sed -i '$ a a new line' foo.file
will do, so sed
is better especially you want always append a new line to the file.– zhouji
Sep 12 '16 at 10:27
add a comment |
I used sed because it can be used with sudo. For example:
sudo sed -i '$ a text to be inserted' fileName.file
the alternative is very ugly like :
sudo bash -c "echo a text to be inserted >> fileName.file"
and even uglier when done with ssh.
1
Why was this answer downvoted? This looks like the solution I need. Is there any problem with it? If not, I'll use it and upvote it.
– MountainX
Jul 23 '16 at 8:20
Does yoursed -i
not take an extension as argument? Your example may have incorrect parameters.
– protometa
Jan 4 '17 at 19:57
add a comment |
I used sed because it can be used with sudo. For example:
sudo sed -i '$ a text to be inserted' fileName.file
the alternative is very ugly like :
sudo bash -c "echo a text to be inserted >> fileName.file"
and even uglier when done with ssh.
1
Why was this answer downvoted? This looks like the solution I need. Is there any problem with it? If not, I'll use it and upvote it.
– MountainX
Jul 23 '16 at 8:20
Does yoursed -i
not take an extension as argument? Your example may have incorrect parameters.
– protometa
Jan 4 '17 at 19:57
add a comment |
I used sed because it can be used with sudo. For example:
sudo sed -i '$ a text to be inserted' fileName.file
the alternative is very ugly like :
sudo bash -c "echo a text to be inserted >> fileName.file"
and even uglier when done with ssh.
I used sed because it can be used with sudo. For example:
sudo sed -i '$ a text to be inserted' fileName.file
the alternative is very ugly like :
sudo bash -c "echo a text to be inserted >> fileName.file"
and even uglier when done with ssh.
edited May 24 '16 at 18:55
sam
15.2k3 gold badges16 silver badges27 bronze badges
15.2k3 gold badges16 silver badges27 bronze badges
answered May 24 '16 at 18:34
Fred MehrdadFred Mehrdad
211 bronze badge
211 bronze badge
1
Why was this answer downvoted? This looks like the solution I need. Is there any problem with it? If not, I'll use it and upvote it.
– MountainX
Jul 23 '16 at 8:20
Does yoursed -i
not take an extension as argument? Your example may have incorrect parameters.
– protometa
Jan 4 '17 at 19:57
add a comment |
1
Why was this answer downvoted? This looks like the solution I need. Is there any problem with it? If not, I'll use it and upvote it.
– MountainX
Jul 23 '16 at 8:20
Does yoursed -i
not take an extension as argument? Your example may have incorrect parameters.
– protometa
Jan 4 '17 at 19:57
1
1
Why was this answer downvoted? This looks like the solution I need. Is there any problem with it? If not, I'll use it and upvote it.
– MountainX
Jul 23 '16 at 8:20
Why was this answer downvoted? This looks like the solution I need. Is there any problem with it? If not, I'll use it and upvote it.
– MountainX
Jul 23 '16 at 8:20
Does your
sed -i
not take an extension as argument? Your example may have incorrect parameters.– protometa
Jan 4 '17 at 19:57
Does your
sed -i
not take an extension as argument? Your example may have incorrect parameters.– protometa
Jan 4 '17 at 19:57
add a comment |
Another one liner is:
echo "Greetings 1" >> greetings.txt && echo "Greetings 2" >> greetings.txt
I'd prefer the -e
option thought, as it gives more control:
echo -e "Greeting 1nGreetings 2n" >> greetings.txt
add a comment |
Another one liner is:
echo "Greetings 1" >> greetings.txt && echo "Greetings 2" >> greetings.txt
I'd prefer the -e
option thought, as it gives more control:
echo -e "Greeting 1nGreetings 2n" >> greetings.txt
add a comment |
Another one liner is:
echo "Greetings 1" >> greetings.txt && echo "Greetings 2" >> greetings.txt
I'd prefer the -e
option thought, as it gives more control:
echo -e "Greeting 1nGreetings 2n" >> greetings.txt
Another one liner is:
echo "Greetings 1" >> greetings.txt && echo "Greetings 2" >> greetings.txt
I'd prefer the -e
option thought, as it gives more control:
echo -e "Greeting 1nGreetings 2n" >> greetings.txt
answered Jun 27 '17 at 15:15
Erdal G.Erdal G.
1113 bronze badges
1113 bronze badges
add a comment |
add a comment |
One can emulate cat >> out.txt
with either Perl or Python to achieve same effect. Perl:
perl -e 'open(fh,">>","./out.txt");while(<>){printf(fh "%s",$_)};close(fh);'
And Python:
python -c 'import sys;f=open(sys.argv[1],"a");l=[i for i in sys.stdin];f.write("".join(l))' out.txt
Note that for python you'll have to hit Ctrl+D twice. See related question on stackoverflow for more info.
add a comment |
One can emulate cat >> out.txt
with either Perl or Python to achieve same effect. Perl:
perl -e 'open(fh,">>","./out.txt");while(<>){printf(fh "%s",$_)};close(fh);'
And Python:
python -c 'import sys;f=open(sys.argv[1],"a");l=[i for i in sys.stdin];f.write("".join(l))' out.txt
Note that for python you'll have to hit Ctrl+D twice. See related question on stackoverflow for more info.
add a comment |
One can emulate cat >> out.txt
with either Perl or Python to achieve same effect. Perl:
perl -e 'open(fh,">>","./out.txt");while(<>){printf(fh "%s",$_)};close(fh);'
And Python:
python -c 'import sys;f=open(sys.argv[1],"a");l=[i for i in sys.stdin];f.write("".join(l))' out.txt
Note that for python you'll have to hit Ctrl+D twice. See related question on stackoverflow for more info.
One can emulate cat >> out.txt
with either Perl or Python to achieve same effect. Perl:
perl -e 'open(fh,">>","./out.txt");while(<>){printf(fh "%s",$_)};close(fh);'
And Python:
python -c 'import sys;f=open(sys.argv[1],"a");l=[i for i in sys.stdin];f.write("".join(l))' out.txt
Note that for python you'll have to hit Ctrl+D twice. See related question on stackoverflow for more info.
answered Dec 3 '17 at 2:55
Sergiy KolodyazhnyySergiy Kolodyazhnyy
11.2k4 gold badges27 silver badges69 bronze badges
11.2k4 gold badges27 silver badges69 bronze badges
add a comment |
add a comment |
Here is an example to append multiple lines in a file:
{
echo ' directory "/var/cache/bind";'
echo ' listen-on { 127.0.0.1; };'
echo ' listen-on-v6 { none; };'
echo ' version "";'
echo ' auth-nxdomain no;'
echo ' forward only;'
echo ' forwarders { 8.8.8.8; 8.8.4.4; };'
echo ' dnssec-enable no;'
echo ' dnssec-validation no;'
} >> your_file.txt
add a comment |
Here is an example to append multiple lines in a file:
{
echo ' directory "/var/cache/bind";'
echo ' listen-on { 127.0.0.1; };'
echo ' listen-on-v6 { none; };'
echo ' version "";'
echo ' auth-nxdomain no;'
echo ' forward only;'
echo ' forwarders { 8.8.8.8; 8.8.4.4; };'
echo ' dnssec-enable no;'
echo ' dnssec-validation no;'
} >> your_file.txt
add a comment |
Here is an example to append multiple lines in a file:
{
echo ' directory "/var/cache/bind";'
echo ' listen-on { 127.0.0.1; };'
echo ' listen-on-v6 { none; };'
echo ' version "";'
echo ' auth-nxdomain no;'
echo ' forward only;'
echo ' forwarders { 8.8.8.8; 8.8.4.4; };'
echo ' dnssec-enable no;'
echo ' dnssec-validation no;'
} >> your_file.txt
Here is an example to append multiple lines in a file:
{
echo ' directory "/var/cache/bind";'
echo ' listen-on { 127.0.0.1; };'
echo ' listen-on-v6 { none; };'
echo ' version "";'
echo ' auth-nxdomain no;'
echo ' forward only;'
echo ' forwarders { 8.8.8.8; 8.8.4.4; };'
echo ' dnssec-enable no;'
echo ' dnssec-validation no;'
} >> your_file.txt
answered Apr 12 '18 at 7:44
user2753331user2753331
1
1
add a comment |
add a comment |
In addition to main answer, in case the file needs super user permissions, just adding sudo
in front of echo
won't work.
This is because shell breaks the commands and though echo
did run as root, but >>
ran with normal privileges.
This will work for super user:
sudo su -c "echo 'Line 3' >> greetings.txt"
You don't need thesu
here (sudo su
is not good). But you're right in that you need something do manage the shell redirection; perhapssudo sh -c "echo stuff >> file.txt"
. No net saving but a better practice.
– roaima
Aug 8 at 13:51
add a comment |
In addition to main answer, in case the file needs super user permissions, just adding sudo
in front of echo
won't work.
This is because shell breaks the commands and though echo
did run as root, but >>
ran with normal privileges.
This will work for super user:
sudo su -c "echo 'Line 3' >> greetings.txt"
You don't need thesu
here (sudo su
is not good). But you're right in that you need something do manage the shell redirection; perhapssudo sh -c "echo stuff >> file.txt"
. No net saving but a better practice.
– roaima
Aug 8 at 13:51
add a comment |
In addition to main answer, in case the file needs super user permissions, just adding sudo
in front of echo
won't work.
This is because shell breaks the commands and though echo
did run as root, but >>
ran with normal privileges.
This will work for super user:
sudo su -c "echo 'Line 3' >> greetings.txt"
In addition to main answer, in case the file needs super user permissions, just adding sudo
in front of echo
won't work.
This is because shell breaks the commands and though echo
did run as root, but >>
ran with normal privileges.
This will work for super user:
sudo su -c "echo 'Line 3' >> greetings.txt"
answered Jun 7 '18 at 19:50
SJ00SJ00
101
101
You don't need thesu
here (sudo su
is not good). But you're right in that you need something do manage the shell redirection; perhapssudo sh -c "echo stuff >> file.txt"
. No net saving but a better practice.
– roaima
Aug 8 at 13:51
add a comment |
You don't need thesu
here (sudo su
is not good). But you're right in that you need something do manage the shell redirection; perhapssudo sh -c "echo stuff >> file.txt"
. No net saving but a better practice.
– roaima
Aug 8 at 13:51
You don't need the
su
here (sudo su
is not good). But you're right in that you need something do manage the shell redirection; perhaps sudo sh -c "echo stuff >> file.txt"
. No net saving but a better practice.– roaima
Aug 8 at 13:51
You don't need the
su
here (sudo su
is not good). But you're right in that you need something do manage the shell redirection; perhaps sudo sh -c "echo stuff >> file.txt"
. No net saving but a better practice.– roaima
Aug 8 at 13:51
add a comment |
protected by Archemar Aug 8 at 13:24
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?