onlyforbopi
3/10/2017 - 10:43 AM

AWK SUBSTITUTION

AWK SUBSTITUTION

Substitute (find and replace) "foo" with "bar" on each line.					awk '{ sub(/foo/,"bar"); print }'						
Substitute (find and replace) "foo" with "bar" on each line.					awk '{ gsub(/foo/,"bar"); print }'						
Substitute (find and replace) "foo" with "bar" on each line. (4th match only)					gawk '{ $0 = gensub(/foo/,"bar",4); print }'						
Substitute "foo" with "bar" only on lines that contain "baz".					awk '/baz/ { gsub(/foo/, "bar") }; { print }'						
Substitute "foo" with "bar" only on lines that do not contain "baz".					awk '!/baz/ { gsub(/foo/, "bar") }; { print }'						
Change "scarlet" or "ruby" or "puce" to "red".					awk '{ gsub(/scarlet|ruby|puce/, "red"); print}'						
Reverse order of lines (emulate "tac").					awk '{ a[i++] = $0 } END { for (j=i-1; j>=0;) print a[j--] }'						
Join a line ending with a backslash with the next line.					awk '/\\$/ { sub(/\\$/,""); getline t; print $0 t; next }; 1'