Append Text to File from Command Line Without Using Io Redirection

Append text to file from command line without using io redirection

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 line
  • a means append a line after
  • \ is simply used as a delimiter

How do I append text to a file?


cat >> filename
This is text, perhaps pasted in from some other source.
Or else entered at the keyboard, doesn't matter.
^D

Essentially, you can dump any text you want into the file. CTRL-D sends an end-of-file signal, which terminates input and returns you to the shell.

Append file(s) to a file without redirection ( ) in Bash

With GNU sed, you could use in-place editing and the r command:

sed -i '$r file2' file

which on the last line of file will append (r for "read") the contents of file2.

As for an Ansible module, I don't know Ansible well at all, but blockinfile might work?

How to send entire output in command prompt to log file to display time change and errors

Achieving this, is quite easy. There are two ways: do it inside a batch file:

setlocal EnableDelayedExpansion
(
echo Logged time = !time! !date!

@echo off
echo !time

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyMonitor
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyMonitorToastTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyRefreshTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Power Efficiency Diagnostics\AnalyzeSystem"
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Maps\MapsToastTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Maps\MapsUpdateTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Mobile Broadband Accounts\MNO Metadata Parser"
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Windows Error
Reporting\QueueReporting"
timeout 5 > NUL
echo !time!

schtasks /Change /disable/TN \Microsoft\Windows\Defrag\ScheduledDefrag
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Windows MediaSharing\UpdateLibrary"
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Maintenance\WinSAT
timeout 5 > NUL
echo !time!
)>>TaskSetBatchScripts.log 2>&1

Not sure what you need here: append (>>) or redirect (>) [erase previous content].

The second way is to run the file from cmd like this:

(filename.bat)>>TaskSetBatchScripts.log 2>&1

Not sure again what you need.

(A 3rd way would be to append each command's output to a file adding 2>&1).

$(info) command io redirection

Make variables and functions are expanded by make before it invokes the shell to run the recipe. The results of expansion are passed to the shell.

2> is a shell construct, it's not interpreted by make.

The info make function is defined to write its arguments to stdout, and expand to the empty string. So, when make expands this recipe line:

$(info $(CC) -o $(target) $^ $(DEBUG)) 2> /tmp/compilation.out | cat

First it expands the arguments to info and writes the results to stdout, then it replaces the info function with the empty string, then it invokes the shell with the results; so the shell sees:

 2> /tmp/compilation.out | cat

The short answer is no, there is no way to get make's info function to write output to a file. You'll have to use something the shell knows about, like echo:

@echo '$(CC) -o $(target) $^ $(DEBUG)' 2> /tmp/compilation.out | cat

windows cmd dos not show complete output in a text file

Redirecting a command's output to a file via > somefile.txt only redirects the data written to "standard output" stream to the file, it does not include data written to "standard error" stream, which is where the "Access is denied." message is written in your case. To capture all output, you need to append 2>&1 to the end of the command, e.g.:

copy D:\1.txt C:\ > d:\output.txt 2>&1

This can be understood as an instruction to send data written to standard error (stream 2) to standard output (stream 1).

You can also redirect standard error to a different file to standard output with e.g.:

copy D:\1.txt C:\ > d:\output.txt 2> d:\errors.txt

Windows batch redirect output to console

This is more a workaround.

The idea is to a use a pipe program in order to redirect the output of the Windows program to the console:

SomeWinProg SomeArgs 2>>&1 | SomePipeProg PipeProgArgs

As a pipe program you may use a program that passes throug everything, like:

SomeWinProg SomeArgs 2>>&1 | findstr /r "/c:.*"

If this works or not depends on the Windows program.

Concerning the timing:

There may be some trouble when you have a long time running Windows program which produces sporadic output or when the output is done always to the same line (no line feeds, only carriage returns) and you want to have the output in real time.

In these cacses Windows programs like findstr or find are a little bit weak.

On github there is an implementation of mtee thats fits better:

SomeWinProg SomeArgs 2>>&1 | mtee nul


Related Topics



Leave a reply



Submit