Awk: verschil tussen versies
Naar navigatie springen
Naar zoeken springen
Regel 1: | Regel 1: | ||
− | ''Awk'' is a scripting language for manipulating data in files. Around 2021-2022, I Occasionally switch between data in spreadsheets and in associative Bash arrays. So maybe I've just been looking for Awk? | + | ''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 == | == 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}' | + | awk '{print}' ls_usr_bin.txt |
</pre> | </pre> | ||
of | of | ||
<pre> | <pre> | ||
− | awk '{print $0}' | + | awk '{print $0}' ls_usr_bin.txt |
</pre> | </pre> | ||
Regel 18: | Regel 27: | ||
Print only the first column of the file (where fields are separated by a space); | Print only the first column of the file (where fields are separated by a space); | ||
<pre> | <pre> | ||
− | awk '{print $1}' | + | awk '{print $1}' ls_usr_bin.txt |
</pre> | </pre> | ||
Regel 24: | Regel 33: | ||
<pre> | <pre> | ||
− | awk '/^w/ {print $1}' | + | awk '/^w/ {print $1}' ls_usr_bin.txt |
</pre> | </pre> | ||
Versie van 28 sep 2022 17:28
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
Filter rules according to a regular expression: All lines starting with the string 'w':
awk '/^w/ {print $1}' ls_usr_bin.txt
See also
Sources
- https://www.youtube.com/watch?v=jJ02kEETw70 - Gary Exlains - Everyone needs to learn a little bit of AWK