awk '{ print $1 }' /etc/passwd | head -n 3
awk -F: '{ print $1 }' /etc/passwd | head -n 3
cat awk.s
awk -f awk.s /etc/passwd
BEGIN, END: will be run once
FS: The input field separator, a space by default.
NR: The total number of input records seen so far.
OFS: The output field separator, a space by default.
cat awk.s
awk -f awk.s /etc/passwd
awk -F':' '{print toupper($1)}' /etc/passwd | head -3
awk -F':' '{print $1 $7}' /etc/passwd | head -3
awk -F':' '{print $1, $7}' /etc/passwd | head -3
comma , is field separator.
The default separator is space.
we can change it using OSF variable.
awk -F':' 'BEGIN { OFS=":" } { count++; print count, $1, $7, rand() }' /etc/passwd | head -3
last | grep milad | grep pts/7
last | awk "(/milad/ && /pts\/7/)"
last | grep milad | grep -e pts/6 -e pts/7
last | grep milad | grep -E "pts/6|pts/7"
last | awk "(/milad/ && (/pts\/6/||/pts\/7/))"
lastlog | grep -v "Never"
lastlog | awk "!(/Never/)"
awk "/milad/ && /syslog/" /etc/group
awk '(/milad/ || /syslog/) && !(/milad$/)' /etc/group
awk -F ':' '(/milad/ || /syslog/) && !(/milad$/) && $3 < 1000 { print $1 }' /etc/group
awk 'BEGIN {FS=":"; print "- - - - -"} (/milad/ || /syslog/) && !(/milad$/) && $3 < 1000 { cnt++; print cnt":"$1 }\
END { print "* * * * *\nTotal rows:" NR; print "Found:" cnt "\n" "Fields:" NF}' /etc/group
grep adm: /etc/group
cat awk.s
awk -f awk.s /etc/group
cat awk.s # same as above script
cat txt
awk 'BEGIN {RS="\n\n"} $0 ~ /sep/{ print }' txt
RS: Record separator, default is \n
awk '/sep/{ print }' txt
awk -F":" '$4 ~ /milad/ { if ($1 != "milad") a[$1]=gsub(",", $4)} END { for ( i in a) { print i, ":", ++a[i] } }' \
/etc/group # find number of users each group has
ipynb
format: https://github.com/ravexina/linux-notes. html
exports of project available at: https://ravexina.github.io/linux-notes.Linux Notes by Milad As (Ravexina) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.