prints
column 1
|
awk
'{print $1}'
|
filters
lines with "abc" (same as grep abc)
|
awk
'/'abc'/'
|
filters
lines with "abc" and prints column 1
|
awk
'/'abc'/ {print $1}'
|
prints
column 2 and 3
|
awk
'{print $2,$3}'
|
prints
all columns
|
awk
'{print $0}'
|
gets
instructions from a file
|
awk
-f script.awk in putfile
|
executes
a program using data from input file
|
awk
'program' inputfile
|
classic
hello world
|
awk
"BEGIN {print \"Hello World\"}"
|
Print
what's entered on the command line until EOF (^D)
|
awk
'{print}'
|
awk
script for the classic "Hello, world!"
(make
it executable with chmod and run it as-is)
|
#!
/bin/awk -f
BEGIN
{ print "Hello, world!" }
|
Comments
in awk scripts
|
#
This is a program that prints \
"Hello,
world!"
# and
exits
|
prints
first column on a : delimited file
|
awk
-F: '{print $1}' /etc/passwd
|
prints
length of each line
|
cat
/etc/passwd | awk '{print length($0)}'
|
prints
the length of the longest line
|
cat
/etc/passwd | awk '{if (length($0)>max) max=length($0)} END {print max}'
|
shows
all lines longer than 80
|
cat
passwd.sample | awk 'length($0)>80'
|
same as
above but instead of printing the lines,
this
prints the lengths of those lines
|
cat
passwd.sample | awk 'length($0)>80 {print length($0)}'
|
prints
line w/ more than 2 fields (NF stands for Number
of
Fields)
|
awk
'NF>2' file.txt
|
Print
seven random numbers from 0 to 100
|
awk
'BEGIN { for (i = 1; i <= 7; i++)
print int(101 * rand()) }' |
prints
the sum of file sizes (in bytes) in the current
directory
|
ls -l
| awk '{sum=sum+$5} END {print sum}'
|
Print
the total number of kilobytes used by files in
the
current directory
|
ls -l
. | awk '{ x += $5 } ; END { print "total kilobytes: " (x +
1023)/1024 }'
|
print
number of lines (NR stands for Number of Rows)
|
awk
'END {print NR}' passwd.sample
|
prints
lines whose field 3 is equal to "root"
|
ls -l
| awk '$3=="root"'
|
prints
bcd
|
echo
aaaabcd | awk '{ sub(/a+/, ""); print }'
|
prints
even-numbered lines
|
awk
'NR%2==0' file.txt
|
weird
stuff
|
ls
-lh | awk '{ owner = $3 ; $3 = $3 " 0wnz"; print $3 }' | uniq
|
prints
number of lines
|
awk
'END {print NR}' passwd.sample
|
extract
username and uid from inputfile
|
awk
-F: '{print "Username: " $1"\t\tUID: "$3}' passwd.sample
|
prints
only those lines containing "abc"
|
awk
'/abc/ {print}' myfile.txt
|
uses a
more complicated regular expression
|
awk
'/[0-9]+\.[0-9]*/ {print}' inputfile
|
prints
lines whose 2nd field is equal to "2"
|
ls -l
| awk '$2==2 {print}'
* In this example, $2==2 is the condition. If that is not
satisfied, it will not execute the action
which is {print}
|
uses
another regular expression
|
ls -l
| awk '$3 ~ /abc/ {print}'
|
prints
all lines that doesn't have "abc"
|
ls -l
| awk '! /abc/ {print}'
|
uses
&& operator for a more precise selection
|
awk
'$3=="abc" && $5==4096 {print}' ls.sample
|
Thursday, June 14, 2018
Useful AWK Commands
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment