Difference between revisions of "Awk"
Jump to navigation
Jump to search
↑ https://serverfault.com/questions/72744/command-to-prepend-string-to-each-line
↑ https://catonmat.net/awk-one-liners-explained-part-two
↑ https://stackoverflow.com/questions/17908555/printing-with-sed-or-awk-a-line-following-a-matching-pattern
(40 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{lowercase}} | ||
+ | [[wikipedia:AWK]] ([[1977]]) | ||
+ | * https://man7.org/linux/man-pages/man1/awk.1p.html | ||
+ | == Examples == | ||
+ | * <code>cat file.txt | [[awk]] '{print $1}'</code> | ||
+ | |||
+ | |||
+ | * '''Prepend''' a variable string ($STRING_TO_PREPEND) at the beginning of every line | ||
+ | :<code>cat file.txt | awk '{print "'$STRING_TO_PREPEND' "$0}'<ref>https://serverfault.com/questions/72744/command-to-prepend-string-to-each-line</ref></code> | ||
− | = | + | In case text to prepend is a variable ($PREPEND_TEXT_VAR): |
+ | :<code>echo "$ORIGINAL_TEXT" | awk -v AWK_PREPEND_VAR="$PREPEND_TEXT_VAR" '{print AWK_PREPEND_VAR" "$0}'</code> | ||
+ | Prepend time before every line | ||
+ | :<code>echo "$ORIGINAL_TEXT" | awk -v time="$(date)" '{print time" "$0}'</code> | ||
+ | * Remove leading spaces at the beginning of a line: | ||
+ | :<code>awk ' {$1=$1} 1 '</code><ref>https://catonmat.net/awk-one-liners-explained-part-two</ref> | ||
* Print lines if field fith value ($5) is smaller than 100: | * Print lines if field fith value ($5) is smaller than 100: | ||
Line 10: | Line 24: | ||
:Example: <code>awk '$5<100'</code> | :Example: <code>awk '$5<100'</code> | ||
+ | * Print '''all lines after''' some text<ref>https://stackoverflow.com/questions/17908555/printing-with-sed-or-awk-a-line-following-a-matching-pattern</ref> | ||
+ | :<code>awk '/TEXT_TO_SEARCH/{f=1}f' file</code> | ||
+ | |||
+ | * Print every 3 lines: | ||
+ | :<code>awk 'NR%3==1' your-file.txt</code> | ||
+ | |||
+ | * Print last field from a file | ||
+ | :<code>awk '{[[print $NF]]}' your-file.txt</code> | ||
+ | |||
+ | * Print last but one field from a file | ||
+ | :<code>awk '{print $(NF-1)}' your-file.txt</code> | ||
+ | |||
+ | * Print line only if a field meet a condition, bigger or smaller than, example field 4 bigger that 134: | ||
+ | :<code>awk '$4 > 134 {print}' your-file.txt</code> | ||
+ | |||
+ | [[ps --no-headers]] -o "rss,cmd" -C php-fpm7.2 | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }' | ||
+ | |||
+ | == Errors == | ||
+ | * <code>awk: calling undefined function [[gensub]]</code> | ||
+ | == Related == | ||
+ | * <code>[[sed]]</code>, <code>[[tr]]</code> | ||
+ | * <code>[[lineinfile (Ansible)]]</code> | ||
+ | * <code>[[gawk]]</code> | ||
+ | * <code>[[tcl]]</code> | ||
+ | * <code>[[a2]]</code> alias | ||
+ | |||
+ | == Activities == | ||
+ | * Read https://lists.gnu.org/archive/html/info-gnu/2020-04/msg00007.html | ||
+ | * Read https://stackoverflow.com/questions/tagged/awk?tab=Votes | ||
== See also == | == See also == | ||
− | *{{grep}} | + | * {{awk}} |
− | + | * {{grep}} | |
+ | * {{vi}} | ||
+ | [[Category:AWK]] | ||
[[Category:Linux]] | [[Category:Linux]] |
Latest revision as of 18:28, 26 February 2024
Contents
Examples[edit]
cat file.txt | awk '{print $1}'
- Prepend a variable string ($STRING_TO_PREPEND) at the beginning of every line
cat file.txt | awk '{print "'$STRING_TO_PREPEND' "$0}'[1]
In case text to prepend is a variable ($PREPEND_TEXT_VAR):
echo "$ORIGINAL_TEXT" | awk -v AWK_PREPEND_VAR="$PREPEND_TEXT_VAR" '{print AWK_PREPEND_VAR" "$0}'
Prepend time before every line
echo "$ORIGINAL_TEXT" | awk -v time="$(date)" '{print time" "$0}'
- Remove leading spaces at the beginning of a line:
awk ' {$1=$1} 1 '
[2]
- Print lines if field fith value ($5) is smaller than 100:
awk '$FIELD_NUMER>VALUE'
- Example:
awk '$5<100'
- Print all lines after some text[3]
awk '/TEXT_TO_SEARCH/{f=1}f' file
- Print every 3 lines:
awk 'NR%3==1' your-file.txt
- Print last field from a file
awk '{print $NF}' your-file.txt
- Print last but one field from a file
awk '{print $(NF-1)}' your-file.txt
- Print line only if a field meet a condition, bigger or smaller than, example field 4 bigger that 134:
awk '$4 > 134 {print}' your-file.txt
ps --no-headers -o "rss,cmd" -C php-fpm7.2 | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
Errors[edit]
awk: calling undefined function gensub
Related[edit]
Activities[edit]
- Read https://lists.gnu.org/archive/html/info-gnu/2020-04/msg00007.html
- Read https://stackoverflow.com/questions/tagged/awk?tab=Votes
See also[edit]
awk, awk -F, cut, $NF
ack, ag, grep
,egrep, fgrep
,agrep
,ngrep
,pgrep
,awk
,sed
,strings
,tr
,tail
,mtail
,git grep
,wc
,uniq
,LogQL
,findstr (Windows)
,rg, git-grep, cut
- Editors:
vi, vim
,sed
,awk
, emacs,lineinfile (Ansible)
, Atom,paste
, Neovim, Rider,visudo, vipw
,update-alternatives
,nano
,more, less
,.vimrc
,set (vi)
zed
Advertising: