How to write a txt file in terminal without entering vim - vim

i have to write the file right in the terminal, not in vim. Something like this vim -someflag "text for my file" filename. Any ideas ?

In bash, just do:
$ echo "text for my file" >filename
To append, use:
$ echo "text for my file" >> filename

Why use vim if you want to write like this?
Use echo instead:
echo "text for my file" > filename
man echo for more information - documentation

'cat' or 'echo' would be the best options for this.
In the absence of an input argument, cat reads from the standard input:
cat > myfile.txt
[enter your text, then hit ctrl-d]
Alternatively, echo will write its arguments to a file with redirection:
echo "text for my file" > myfile.txt

Related

Automate a Vim script to write text to a file

I am writing a script that runs on the command line and I want to be able to automatically append some static text to a file using Vim.
This is a simplified version of what I have:
insert_text() {
vim -s ./text.txt new.txt;
}
Then it would run on the command line by typing insert_text. Inside text.txt I have tried things such as:
iSome Text:wq
This puts me in insert mode and writes the text but I don't know how to leave insert mode in this way. The :wq never works and is instead written to the new.txt.
If you just want to append text to a file you don't need any special tools. For instance you can just use:
echo "Some text" >> myfile.txt
or if you have a large block of text
>>myfile.txt cat <<EOF
some
long
block of text
EOF
If you want it at the start of a file you can use a temporary file to do this. For instance:
echo "Some Text" | cat - myfile.txt > /tmp/file && mv /tmp/file myfile.txt
This will append someText at the beginning of line 1:
vim -c "1 s/^/someText" -c "wq" test.txt
When you run a command from the terminal and you want Vim to exit after that, instead of :wq do +wq. For example:
vim +'SomeCommand' +qa

how to insert ctrl+d into my linux script?

I want to make the following commands:
cat > template.txt
[enter in the terminal]
text
[Ctrl+d in the terminal]
in a script.
Is there a way to tell the script to do enter\Ctrl d?
Is there a way to create a file and write to it in script?
I didn't find anything that worked for me.
Thanks.
A Here Document is kind of like a script version of what you're talking about, I think, although it is not entirely clear to me from your description.
#!/bin/bash
cat > template.txt <<- EOF
Here
is some
text.
EOF
Ctrl-D itself is the EOF character, ASCII 4.
When you want an interactieve user enter lines and add them to your file until the user enters an ^D you can use the next script:
echo "Please give input"
while read -r line; do
echo "Enter next line or ^D"
echo "${line}" >> template.txt
done
echo "After loop"
You do not have to check for ^D, that will be recognized by read without doing something extra.So you do not need to use CTRL-V CTRL-D in vi.
No, there isn't. How should the script know when your user is finished entering text?

Linux bash Shell scripting : Creating command using string Append how to do that?

Hi I want to achieve #cat sample.txt > abc.txt
But "> abc.txt" am getting as argument.
How to cascade these two strings and execute the combined one.
I tried the below steps
/home/root# export str=" > abc.txt;"
/home/root# echo $str
> abc.txt;
/home/root# echo "cat sample.txt $str"
cat sample.txt > abc.txt;
/home/root# `echo "cat sample.txt $str"`
Hello world
cat: can't open '>': No such file or directory
cat: can't open 'abc.txt;': No such file or directory
like this?
#!/bin/bash
args="> foo"
command="date"
eval "$command $args"
cat foo
use of eval is not really a recommended method but sometimes it comes in handy for doing things in a quick and dirty way. I will probably get downrated for suggesting this.
Be aware of its side effects.
If you need to do this from command line write :
cat sample.txt | tee $str

Formatting text in shell scripting

from the command line to redirect an ouptput to another file I am aware that I can do something like this
$ echo randomText > file.md
I am also aware that, if I want to append the output to the end of the file, I can do something like this
$ echo randomText >> file.md
Now if I cat the content of file.md I will see something like
randomText
randomText
Is there a way to format the output that is being sent to the file. Rather than appending to the end I am hoping to achieve something like this
randomText ----------------------------------- randomText
To do this, I used printf to format the ouput that was being sent to the file.
printf "%10s", "------------------------------------------" > file.md
To append to the same line, yuou could use printf to tab it.
While going through loop you can try this
echo -ne "randomText" >> logfile
# some other actions
echo -ne "--------------------------------------" >> logfile
echo -ne "randomText" >> logfile
In logfile you can now find
randomText--------------------------------------randomText

Cat redirection auto kill?

I'm trying to do the following :
cat > somefile "some text" <ctrl+d>; clear; some other program
but without having to press
<"ctrl + d">
so that line will create the file and then run some other program. I tried echo "some text" > somefile; but there are too many special chars for that. Anyone know a way around this
I think what you may be looking for is something along these lines:
pax> cat >tempfile ; tput clear ; someprog
Enter your data here for tempfile
<ctrl-d>
**screen clears and someprog runs**
The end-file CTRL-D isn't part of the command you enter, it's part of the input stream for the cat command.
If you don't want to use the input stream, you're either going to have to work out the echo variant (learn to embrace the wonderful world of shell escapes - you can get around most of them by just using single quotes instead of double ones), or simply create your input file in advance and use something like:
cp sourcefile tempfile ; tput clear ; someprog
If you wish to write some text in somefile in multiple lines or with special characters, you should try this. EOF is treated as a special string here that will terminate cat automatically when found but it could be anything else.
cat > somefile << EOF
some text
EOF
some other program

Resources