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
Tags: Mobile web edit, Mobile edit |
Tags: Mobile web edit, Mobile edit |
||
Line 27: | Line 27: | ||
== Related commands == | == Related commands == | ||
− | * <code>[[sed]]</code> | + | * <code>[[sed]]</code>, <code>[[tr]]</code> |
* <code>[[lineinfile (Ansible)]]</code> | * <code>[[lineinfile (Ansible)]]</code> | ||
Revision as of 09:04, 9 July 2020
Examples
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
Related commands
Activities
See also
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: