I have the following problem:
I'm trying to write a script where two files (file1.txt and file2.txt) should be combined into 1 file with a text passage in between. The output should be written in another file (e.g. output.txt).
The output.txt file should be:
[content of file1.txt]
text passage
[content of file2.txt]
After some research on the internet I found the following and it works fine in the terminal:
cat file1.txt <(echo "text passage") file2.txt > output.txt
However, it does not work in my script:
#!/bin/sh
cat file1.txt <(echo "text passage") file2.txt > output.txt
If I execute the script nothing happens (the output.txt isn't written).
Why doesn't this line work in a script and what can I do to make it work?
Thank you for your help!
Stephan
You can just do this:
cat file1.txt > output.txt
echo "Text message" >> output.txt
cat file2.txt >> output.txt
the >> operator means add it to the end of the file, rather than overwriting the contents.
You can also group the commands using brackets:
(cat file1.txt
echo "Text message"
cat file2.txt) > output.txt
Related
I want to write "<" or ">" symbol to a text files.
There's an examle of what I tried:
echo echo. >file2.txt>file1.txt
But that doesn't work.
What I want is to make the batch file make file2.txt with this text below:
echo. >file1.txt
Does anybody know how to do that?
Escape the < or > using ^. Note to actually redirect to a file, you'll need an extra redirect operator.
Here's an example using > - the same solution works for < (and % as well).
echo .^> > file.txt
So to get the results you want (a text file containing echo. file2.txt > file1.txt) in a text file named redir.txt:
echo echo. file2.txt ^> file1.txt > redir.txt
The end result in redir.txt:
c:\temp>type redir.txt
echo. file2.txt > file.txt
Is this DOS? Look here:
http://www.robvanderwoude.com/escapechars.php
you have to use a caret "^". echo ^>
PHP use a \
http://php.net/manual/en/regexp.reference.escape.php
Is it Unix? Use quotes.
I am trying write an output to a text file with added text. For example i know
executable.exe 2> output.txt
will store the output of the executable in output.txt but what if in the text file i want something like:
The output of the executable is: (add some spaces) executable.exe's output
How about two commands? First one to prepare file:
echo|set /p=The output of the executable is: > output.txt
And then:
executable.exe 2>> output.txt
basically I want to run the following command on every text-file automatically:
awk -f myScript.awk file1.txt > new\file1.txt
awk -f myScript.awk file2.txt > new\file2.txt
...
Then move the processed files to the folder \old.
move *.txt \old
should work for that part.
How do I create the correct for-loop, so that the output of the awk program has the same name as the input, just in the new folder?
OK, try this:
for %%i in (*.txt) do awk -f myScript.awk "%%~fi" > "new\%%~nxi"
I have the following code for finding a string in a file then delete the line which contains that string.
echo `sed /$string/d file.txt` > file.txt
the problem is that if initially file.txt contains:
a
b
c
after deleting "a" (string=a) file.txt will become
b c
instead of
b
c
can any one help me?
This is because of the backticks. Do this instead:
sed -i /$string/d file.txt
Note, if you want to do this in-place, you need to use -i to sed as > will destroy the file before sed can read it.
You do not need the echo wrap, simply try:
sed -i '/a/d' file.txt
You need to quote the command's output:
echo -n "sed /$string/d file.txt" > file.txt
sed has an in-place editing option. It's more proper to use that in your senario.
e.g.
sed -i /$string/d file.txt
For the problem of your case, as the output of `` is not enclosed in double quotes, word splitting is done, by bash. And the newlines are removed.
To use echo in this case, do it like this:
echo "`sed /$string/d file.txt`" > file.txt
I am making a .bat file, and I would like it to write ASCII art into a text file.
I was able to find the command to append a new line to the file when echoing text, but when I read that text file, all I see is a layout-sign and not a space. I think it would work by opening that file with Word or even WordPad, but I would like it to work on any computer, even if that computer only has Notepad (which is mostly the case).
How can I open the text file in a certain program (i.e. WordPad) or write a proper space character to the file?
EDIT:
I found that it is the best way to use:
echo <line1> > <filename>
echo <line2> >> <filename>
P.S. I used | in my ASCII art, so it crashed, Dumb Dumb Dumb :)
echo Hello, > file.txt
echo. >>file.txt
echo world >>file.txt
and you can always run:
wordpad file.txt
on any version of Windows.
On Windows 2000 and above you can do:
( echo Hello, & echo. & echo world ) > file.txt
Another way of showing a message for a small amount of text is to create file.vbs containing:
Msgbox "Hello," & vbCrLf & vbCrLf & "world", 0, "Message"
Call it with
cscript /nologo file.vbs
Or use wscript if you don't need it to wait until they click OK.
The problem with the message you're writing is that the vertical bar (|) is the "pipe" operator. You'll need to escape it by using ^| instead of |.
P.S. it's spelled Pwned.
You can easily append to the end of a file, by using the redirection char twice (>>).
This will copy source.txt to destination.txt, overwriting destination in the process:
type source.txt > destination.txt
This will copy source.txt to destination.txt, appending to destination in the process:
type source.txt >> destination.txt
Maybe this is what you want?
echo foo > test.txt
echo. >> test.txt
echo bar >> test.txt
results in the following within test.txt:
foo
bar
echo "text to echo" > file.txt
Use the following:
echo (text here) >> (name here).txt
Ex. echo my name is jeff >> test.txt
test.txt
my name is jeff
You can use it in a script too.
I always use copy con to write text, It so easy to write a long text
Example:
C:\COPY CON [drive:][path][File name]
.... Content
F6
1 file(s) is copied