How can we append text in a file via a one-line command without using io redirection?
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Error building gcc 4.8.3 from source: libstdc++.so
- Why should we check WIFEXITED after wait in order
- Null-terminated string, opening file for reading
You can use the
--append
feature oftee
:Or shorter,
I assume the request for no redirection (
>>
) comes from the need to use this inxargs
or similar. So if that doesn't count, you can mute the output with>/dev/null
.You can use Vim in Ex mode:
a
append textx
save and closeIf you don't mind using sed then,
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 delimiterIf 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".