Awk: verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
 
(4 tussenliggende versies door dezelfde gebruiker niet weergegeven)
Regel 1: Regel 1:
== Voorbeelden ==
+
''Awk'' is a scripting language for manipulating data in files - Maybe what I would call a ''parser'' or ''text file parser''. Around 2021-2022, I Occasionally switch between data in spreadsheets and in associative Bash arrays.
 +
 
 +
So maybe I've just been looking for Awk? It is thanks to this video: [https://www.youtube.com/watch?v=jJ02kEETw70 - Gary Exlains - Everyone needs to learn a little bit of AWK] that I came across this.
 +
 
 +
== Examples ==
 +
 
 +
Create a sample file:
 +
 
 +
<pre>
 +
cd Desktop
 +
ls /usr/bin -s > ls_usr_bin.txt
 +
</pre>
  
 
Output a file to screen:
 
Output a file to screen:
  
 
<pre>
 
<pre>
awk '{print}' bestand.txt
+
awk '{print}' ls_usr_bin.txt
 
</pre>
 
</pre>
 
of
 
of
  
 
<pre>
 
<pre>
awk '{print $0}' bestand.txt
+
awk '{print $0}' ls_usr_bin.txt
 
</pre>
 
</pre>
  
waarbij <code>$0</code> voor de hele regel staat.
+
where <code>$0</code> represents the entire line.
  
Alleen de eerste kolom van het bestand afdrukken (waarbij velden gescheiden zijn door een spatie);
+
Print only the first column of the file (where fields are separated by a space);
 +
<pre>
 +
awk '{print $1}' ls_usr_bin.txt
 +
</pre>
 +
 
 +
Regular expressions: Include all lines containing <code>gcc</code>:
  
 
<pre>
 
<pre>
awk '{print $1}' bestand.txt
+
$ awk '/gcc/ {print}' ls_usr_bin.txt
 +
 
 +
    4 c89-gcc
 +
    4 c99-gcc
 +
    0 gcc
 +
    0 gcc-9
 +
    0 gcc-ar
 +
    0 gcc-ar-9
 +
    0 gcc-nm
 +
    0 gcc-nm-9
 +
    0 gcc-ranlib
 +
    0 gcc-ranlib-9
 +
    0 x86_64-linux-gnu-gcc
 +
1132 x86_64-linux-gnu-gcc-9
 +
    0 x86_64-linux-gnu-gcc-ar
 +
  36 x86_64-linux-gnu-gcc-ar-9
 +
    0 x86_64-linux-gnu-gcc-nm
 +
  36 x86_64-linux-gnu-gcc-nm-9
 +
    0 x86_64-linux-gnu-gcc-ranlib
 +
  36 x86_64-linux-gnu-gcc-ranlib-9
 
</pre>
 
</pre>
  
Filter regels ahv. een reguliere expressie: Alle regels die beginnen met de string 'w':
+
Filter rules according to a regular expression: All lines starting with the string 'w' → DOESN'T WORK!?
  
 
<pre>
 
<pre>
awk '/^w/ {print $1}' bestand.txt
+
awk '/^w/ {print $1}' ls_usr_bin.txt
 
</pre>
 
</pre>
  
== Zie ook ==
+
== See also ==
  
 +
* [[Associative arrays (Bash)]]
 
* [[Find & replace in text files (Bash)]]
 
* [[Find & replace in text files (Bash)]]
 
* [[Sed]]
 
* [[Sed]]
  
== Bronnen ==
+
== Sources ==
  
 
* https://www.youtube.com/watch?v=jJ02kEETw70 - Gary Exlains - Everyone needs to learn a little bit of AWK
 
* https://www.youtube.com/watch?v=jJ02kEETw70 - Gary Exlains - Everyone needs to learn a little bit of AWK

Huidige versie van 28 sep 2022 om 17:36

Awk is a scripting language for manipulating data in files - Maybe what I would call a parser or text file parser. Around 2021-2022, I Occasionally switch between data in spreadsheets and in associative Bash arrays.

So maybe I've just been looking for Awk? It is thanks to this video: - Gary Exlains - Everyone needs to learn a little bit of AWK that I came across this.

Examples

Create a sample file:

cd Desktop
ls /usr/bin -s > ls_usr_bin.txt

Output a file to screen:

awk '{print}' ls_usr_bin.txt

of

awk '{print $0}' ls_usr_bin.txt

where $0 represents the entire line.

Print only the first column of the file (where fields are separated by a space);

awk '{print $1}' ls_usr_bin.txt

Regular expressions: Include all lines containing gcc:

$ awk '/gcc/ {print}' ls_usr_bin.txt

    4 c89-gcc
    4 c99-gcc
    0 gcc
    0 gcc-9
    0 gcc-ar
    0 gcc-ar-9
    0 gcc-nm
    0 gcc-nm-9
    0 gcc-ranlib
    0 gcc-ranlib-9
    0 x86_64-linux-gnu-gcc
 1132 x86_64-linux-gnu-gcc-9
    0 x86_64-linux-gnu-gcc-ar
   36 x86_64-linux-gnu-gcc-ar-9
    0 x86_64-linux-gnu-gcc-nm
   36 x86_64-linux-gnu-gcc-nm-9
    0 x86_64-linux-gnu-gcc-ranlib
   36 x86_64-linux-gnu-gcc-ranlib-9

Filter rules according to a regular expression: All lines starting with the string 'w' → DOESN'T WORK!?

awk '/^w/ {print $1}' ls_usr_bin.txt

See also

Sources