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

前提是你 提交于 2019-11-26 22:00:16

问题


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


回答1:


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

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

For more about shell redirection goto following link:

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




回答2:


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

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

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

➜ awk '{print $2 >"text.out"}' text.txt

➜ cat text.out
1
2
three
4


来源:https://stackoverflow.com/questions/14660079/how-to-save-the-output-of-this-awk-command-to-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!