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

时间:2021-08-14 02:56:10

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 '{打印$2}'它从文本中提取出来。现在我想把输出保存为另一个文本。谢谢

1 个解决方案

#1


20  

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

> => This will redirect STDOUT to 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 $1 }' | 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 tee for redirection. The command tee will redirect STDOUT to a specified file as well as the terminal screen

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

For more about shell redirection goto following link:

更多关于壳牌重定向后到以下链接:

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

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

#1


20  

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

> => This will redirect STDOUT to 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 $1 }' | 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 tee for redirection. The command tee will redirect STDOUT to a specified file as well as the terminal screen

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

For more about shell redirection goto following link:

更多关于壳牌重定向后到以下链接:

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

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