Can linux cat command be used for writing text to

2019-03-07 20:13发布

Is something like this:

cat "Some text here." > myfile.txt

Possible? Such that the contents of myfile.txt would now be overwritten to:

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...

标签: linux cat
11条回答
看我几分像从前
2楼-- · 2019-03-07 20:49

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-03-07 20:54

Sounds like you're looking for a Here document

cat > outfile.txt <<EOF
>some text
>to save
>EOF
查看更多
来,给爷笑一个
4楼-- · 2019-03-07 20:57

That's what echo does:

echo "Some text here." > myfile.txt
查看更多
该账号已被封号
5楼-- · 2019-03-07 21:02

Here's another way -

cat > outfile.txt
>Enter text
>to save press ctrl-d
查看更多
不美不萌又怎样
6楼-- · 2019-03-07 21:04

I use the following code to write raw text to files, to update my CPU-settings. Hope this helps out! Script:

#!/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.

This code is saved as a file (cpu_update.sh) and to make it executable run:

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.

查看更多
登录 后发表回答