How can we append text in a file via a one-line command without using io redirection?
-
3this sounds like an XY problem (perlmonks.org/index.pl?node_id=542341), why do you need append without redirection?– Joel BergerJan 9, 2011 at 16:32
-
There are plenty of tools for file manipulaton including Perl, sed (see below), awk, tee -a, etc.– Joel BergerJan 9, 2011 at 16:37
-
1@Joel : could be true. I asked the question for a colleague, and really don't know what the exact problem is.– apoorv020Jan 9, 2011 at 16:43
-
6Typically useful when manipulating files with sudo (the IO are under the user's environment), and you sometimes have a limited set of tools allowed via sudo.– Thomas BDXJul 19, 2013 at 13:39
5 Answers
If you don't mind using sed then,
$ cat test this is line 1 $ sed -i '$ a\this is line 2 without redirection' test $ cat test this is line 1 this is line 2 without redirection
As the documentation may be a bit long to go through, some explanations :
-i
means an inplace transformation, so all changes will occur in the file you specify$
is used to specify the last linea
means append a line after\
is simply used as a delimiter
-
I get
sed: -i may not be used with stdin
on macOS 13.3 bash shell. Probably cos of stackoverflow.com/a/21243111/259453– volvoxApr 5, 2018 at 20:52
If you just want to tack something on by hand, then the sed
answer will work for you. If instead the text is in file(s) (say file1.txt and file2.txt):
Using Perl:
perl -e 'open(OUT, ">>", "outfile.txt"); print OUT while (<>);' file*.txt
N.B. while the >>
may look like an indication of redirection, it is just the file open mode, in this case "append".
You can use the --append
feature of tee
:
cat file01.txt | tee --append bothFiles.txt
cat file02.txt | tee --append bothFiles.txt
Or shorter,
cat file01.txt file02.txt | tee --append bothFiles.txt
I assume the request for no redirection (>>
) comes from the need to use this in xargs
or similar. So if that doesn't count, you can mute the output with >/dev/null
.
-
Piping will also step out of xargs.. So not much better than
>>
, the best approach here is usingsh -c
– TofandelApr 21, 2021 at 21:05 -
@Tofandel, I meant before
xargs
. You can pipe as much as you want beforexargs
; but>>
will require you to end the pipe, which then can't continue toxargs
. Aug 2, 2022 at 21:46
You can use Vim in Ex mode:
ex -sc 'a|BRAVO' -cx file
a
append textx
save and close
-
I like the approach in this answer, but it doesn't seem to append to the file. A simple example like this: for fidx in $(seq 1 6); do ex -sc "a|Something${fidx}" -cx test.txt; done produces text that is not ordered:cat test.txt Something2 Something1 Something3 Something4 Something5 Something6. How do I make it go to the end of the file before appending? Interestingly, it only seems to happen for the first two lines of an empty file. If I run the example multiple times everything is ordered correctly. Jan 19, 2018 at 1:40
-
1Try putting a $ before the a, which references the end of the file: ex -sc "$a|Something${fidx}" -cx test.txt– AndyApr 6, 2020 at 21:44
On Linux/GNU systems, the simplest and cleanest solution is:
dd of=oldfile oflag=append conv=notrunc
Simple and clean, because no quoting or backslashitis is required.
Unfortunately, this also doesn't work on BSD (and so, on Darwin), because their dd
has no oflag
. Argh! Can anyone suggest how to do it with the BSD dd
?