Tuesday, July 28, 2020

How to cat multiline content into a file with shell

Sometimes we want to copy a paragraph then paste into or append into a file.
We can use a text editor, such as vi to do the copy paste.

It will be convenient to do it in command line with just cat and >.

Here is the example, the normal way is echo some thing followed by >. 
In case of multi-line, we need to use <<EOF to mark the end of the file.
EOF won't be a literal string that enters the final result, it is the marker of End Of File instead.

here is the example code:

demo>cat > demo.txt <<EOF
> adfd
> dfadfad
> adfafa
> EOF
demo>cat demo.txt
adfd
dfadfad
adfafa
demo>

The file content is now get updated with your strings.


No comments:

Post a Comment

Why I stopped publishing blog posts as information provider

Now the AI can generate content. Does that mean the web publishing industry reaches the end? ChatGPT said: ChatGPT Not at all. While AI can ...