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
Line 3: | Line 3: | ||
== Examples == | == Examples == | ||
+ | * '''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> | ||
Line 9: | Line 20: | ||
:<code>awk '$FIELD_NUMER>VALUE'</code> | :<code>awk '$FIELD_NUMER>VALUE'</code> | ||
:Example: <code>awk '$5<100'</code> | :Example: <code>awk '$5<100'</code> | ||
− | |||
− | |||
== See also == | == See also == |
Revision as of 07:42, 6 January 2020
Examples
- 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'
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
Advertising: