Awk: Difference between revisions

From TÜ Narva Kolledži Wiki
Jump to navigation Jump to search
Content deleted Content added
Dzikajev (talk | contribs)
No edit summary
Dzikajev (talk | contribs)
No edit summary
Line 1: Line 1:
<h1>Команда AWK</h1>
Igor Džikajev
AWK - это скриптовый язык, который не требует компиляции и используется прямо в командной строке, применяемый при фильтрации и обработке текста.
<h1>What is <span style="background:gray;padding:2px;border-radius:5px;color:white;">awk</span> command?</h1>
<span style="background:gray;padding:2px;border-radius:5px;color:white;">awk</span> is a scripting language, and it is helpful when working in the command line. It's also a widely used command for text processing.
When using <span style="background:gray;padding:2px;border-radius:5px;color:white;">awk</span>, you are able to select data – one or more pieces of individual text – based on a pattern you provide.


Работает она таким образом, что читает файл по строчке за раз и выполняет указанное действие выводя результат.
For example, some of the operations you can do with <span style="background:gray;padding:2px;border-radius:5px;color:white;">awk</span> are searching for a specific word or pattern in a piece of text given, or even select a certain line or a certain column in a file you provide.
<h1>Использование команды AWK</h1>

<h1>The Basic Syntax of the <span style="background:gray;padding:2px;border-radius:5px;color:white;">awk</span> command</h1>
In its simplest form, the <span style="background:gray;padding:2px;border-radius:5px;color:white;">awk</span> command is followed by a set of single quotation marks and a set of curly braces, with the name of the file you want to search through mentioned last.

It looks something like this:
<p style="background:rgba(50,50,50,0.1);padding:10px;">awk '{action}' your_file_name.txt</p>
When you want to search for text that has a specific pattern or you're looking for a specific word in the text, the command would look something like this:
<p style="background:rgba(50,50,50,0.1);padding:10px;">awk '/regex pattern/{action}' your_file_name.txt</p>

With information.txt file looking like this:
<p style="background:rgba(50,50,50,0.1);padding:10px;white-space:pre;">fristName lastName age city ID

Thomas Shelby 30 Rio 400
Omega Night 45 Ontario 600
Wood Tinker 54 Lisbon N/A
Giorgos Georgiou 35 London 300
Timmy Turner 32 Berlin N/A</p>
<h6>We are able to</h6>
<ul style="list-style:none;>
<li>Print all the contents of the file using <span style="background:rgba(50,50,50,0.1);padding:5px;">awk '{print $0}' information.txt</span>
<li>Add incrementing number before each line using <span style="background:rgba(50,50,50,0.1);padding:5px;">awk '{print NR,$0}' information.txt</span>
<li>Print specific columns using <span style="background:rgba(50,50,50,0.1);padding:5px;">awk '{print $1}' information.txt</span> *number represents column* It is also possible to print out multiple by adding $n after $1 <span style="background:rgba(50,50,50,0.1);padding:5px;">awk '{print $1, $4}' information.txt</span> or $NF to print out last column
</span>
<li>Print specific lines of a column by adding <span style="background:rgba(50,50,50,0.1);padding:5px;">head or tail command to existing command // awk '{print $1}' information.txt | head -1 </span>
<li>Use regex <span style="background:rgba(50,50,50,0.1);padding:5px;">awk ' /io/{print $0}' information.txt</span> this will print out lines containing "io"
<li>Use comparisson operators <span style="background:rgba(50,50,50,0.1);padding:5px;">awk '$3 < 40 { print $0 }' information.txt</span> this will only print out lines where 3rd column (age) is <40
</ul>

Revision as of 13:21, 12 January 2023

Команда AWK

AWK - это скриптовый язык, который не требует компиляции и используется прямо в командной строке, применяемый при фильтрации и обработке текста.

Работает она таким образом, что читает файл по строчке за раз и выполняет указанное действие выводя результат.

Использование команды AWK