Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

output

awk

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

like image 455
user2034825 Avatar asked Feb 02 '13 08:02

user2034825


People also ask

How do I save an awk output?

The third statement, i.e., close(cmd, "to"), closes the to process after competing its execution. The next statement cmd |& getline out stores the output into out variable with the aid of getline function. The next print statement prints the output and finally the close function closes the command.

How do I print output from awk?

To print a blank line, use print "" , where "" is the empty string. To print a fixed piece of text, use a string constant, such as "Don't Panic" , as one item. If you forget to use the double-quote characters, your text is taken as an awk expression, and you will probably get an error.

What is awk '{ print $4 }'?

The AWK language is useful for manipulation of data files, text retrieval and processing. -F <value> - tells awk what field separator to use. In your case, -F: means that the separator is : (colon). '{print $4}' means print the fourth field (the fields being separated by : ).

What is awk '{ print $1 }'?

If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.


2 Answers

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

like image 183
Suku Avatar answered Oct 14 '22 09:10

Suku


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 
like image 37
Damo Avatar answered Oct 14 '22 08:10

Damo