可以使用 linux cat 命令将文本写入文件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17115664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can linux cat command be used for writing text to file?
提问by IAmYourFaja
Is something like this:
是这样的:
cat "Some text here." > myfile.txt
Possible? Such that the contents of myfile.txt
would now be overwritten to:
可能的?这样 的内容myfile.txt
现在将被覆盖为:
Some text here.
This doesn't work for me, but also doesn't throw any errors.
这对我不起作用,但也不会引发任何错误。
Specifically interested in a cat
-based solution (not vim/vi/emacs, etc.). All examples online show cat
used in conjunction with file inputs, not raw text...
对cat
基于 -based 的解决方案(不是 vim/vi/emacs 等)特别感兴趣。所有在线示例都显示cat
与文件输入结合使用,而不是原始文本...
采纳答案by Carl Norum
That's what echo
does:
这就是echo
它的作用:
echo "Some text here." > myfile.txt
回答by gbrener
Sounds like you're looking for a Here document
听起来您正在寻找Here 文档
cat > outfile.txt <<EOF
>some text
>to save
>EOF
回答by stolen_leaves
Here's another way -
这是另一种方式——
cat > outfile.txt
>Enter text
>to save press ctrl-d
回答by user5260939
cat > filename.txt
enter the text until EOF for save the text use : ctrl+d
输入文本直到 EOF 以保存文本使用:ctrl+d
if you want to read that .txt file use
如果您想阅读该 .txt 文件,请使用
cat filename.txt
and one thing .txt is not mandatory, its for your reference.
有一件事 .txt 不是强制性的,供您参考。
回答by Leahcim
cat
can also be used following a |
to write to a file, i.e. pipe feeds cat a stream of data
cat
也可以在 a 之后|
用于写入文件,即管道馈送数据流
回答by Arahkun
I use the following code to write raw text to files, to update my CPU-settings. Hope this helps out! Script:
我使用以下代码将原始文本写入文件,以更新我的 CPU 设置。希望这会有所帮助!脚本:
#!/bin/sh
cat > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor <<EOF
performance
EOF
cat > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor <<EOF
performance
EOF
This writes the text "performance" to the two files mentioned in the script above. This example overwrite old data in files.
这会将文本“performance”写入上面脚本中提到的两个文件。此示例覆盖文件中的旧数据。
This code is saved as a file (cpu_update.sh) and to make it executable run:
此代码保存为文件 (cpu_update.sh) 并使其可执行运行:
chmod +x cpu_update.sh
After that, you can run the script with:
之后,您可以使用以下命令运行脚本:
./cpu_update.sh
IF you do not want to overwrite the old data in the file, switch out
如果您不想覆盖文件中的旧数据,请切换出
cat > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor <<EOF
with
和
cat >> /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor <<EOF
This will append your text to the end of the file without removing what other data already is in the file.
这会将您的文本附加到文件的末尾,而不会删除文件中已有的其他数据。
回答by JuanToroMarty
You can do it like this too:
你也可以这样做:
user@host: $ cat<<EOF > file.txt
$ > 1 line
$ > other line
$ > n line
$ > EOF
user@host: $ _
I believe there is a lot of ways to use it.
我相信有很多方法可以使用它。
回答by nsantana
Write multi-line text with environment variables using echo
:
使用环境变量编写多行文本echo
:
echo -e "
Home Directory: $HOME \n
hello world 1 \n
hello world 2 \n
line n... \n
" > file.txt
回答by Giannis Mpountouridis
simply pipeline echo with cat
简单的管道回声与猫
For example
例如
echo write something to file.txt | cat > file.txt
回答by Giannis Mpountouridis
Another way to write text to file using cat would be something like this
使用 cat 将文本写入文件的另一种方法是这样的
cat >file.txt <<< Write something here