Linux 如何将此 awk 命令的输出保存到文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14660079/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:54:10  来源:igfitidea点击:

How to save the output of this awk command to file?

linuxawkoutput

提问by user2034825

I wanna save this command to another text: awk '{print $2}' it extract's from text. now i wanna save output too another text. thanks

我想将此命令保存到另一个文本: awk '{print $2}' 它从文本中提取。现在我也想保存输出另一个文本。谢谢

采纳答案by Suku

awk '{ print  }' text.txt > outputfile.txt

>=> This will redirect STDOUTto a file. If file not exists, it will create it. If file exists it will clear out (in effect) the content and will write new data to it

>=> 这将重定向STDOUT到一个文件。如果文件不存在,它将创建它。如果文件存在,它将清除(实际上)内容并将新数据写入其中

>>=> This means same as above but if file exists, this will append new data to it.

>>=> 这与上面的意思相同,但如果文件存在,这将向其追加新数据。

Eg:

例如:

$ cat /etc/passwd | awk -F: '{ print  }' | tail -10 > output.txt
$ cat output.txt 
_warmd
_dovenull
_netstatistics
_avbdeviced
_krb_krbtgt
_krb_kadmin
_krb_changepw
_krb_kerberos
_krb_anonymous
_assetcache

Alternatively you can use the command teefor redirection. The command teewill redirect STDOUTto a specified file as well as the terminal screen

或者,您可以使用该命令tee进行重定向。该命令tee将重定向STDOUT到指定的文件以及终端屏幕

For more about shell redirection goto following link:

有关 shell 重定向的更多信息,请转到以下链接:

http://www.techtrunch.com/scripting/redirections-and-file-descriptors

http://www.techtrunch.com/scripting/redirections-and-file-descriptors

回答by Damo

There is a way to do this from within awk itself (docs)

有一种方法可以从 awk 本身内部执行此操作(文档

? cat text.txt
line 1
line 2
line three
line 4 4 4

? awk '{print }' text.txt
1
2
three
4

? awk '{print  >"text.out"}' text.txt

? cat text.out
1
2
three
4

回答by INNO TECH

try this command.

试试这个命令。

cat ORGFILENAME.TXT | awk '{print }' > SAVENAME.TXT

thx.

谢谢。