Common basic commands for Linux: Three musketeers command-awk ternary expression

Source: Internet
Author: User

awk 3-tuple expression, if...else structure can be overridden with a 3-tuple expression

[Email protected]:~/linux/awk$awk-V fs=":" '{type=$3>=1000? " Normal user ":" System User ";p rint $1,type}'/etc/passwdroot System User Daemon system User Bin system User sys system userSyncSystem User Games System userMansSystem UsersLPSystem User mail system User News system user UUCP system User Proxy system user www-Data System User backup system User list system user IRC system user gnats system user Nobody ordinary user systemd-timesync System User Systemd-Network System User Systemd-Resolve System User Systemd-bus-Proxy System User syslog system User _apt System User Messagebus System User Uuidd System User LIGHTDM System User Whoopsie system user Avahi-AUTOIPD System User Avahi System user DNSMASQ System User Colord System user speech-Dispatcher System User Hplip System User kernoops System User Pulse System User Rtkit System User saned System User Usbmux System user ghostwu Normal user MySQL system user Guest-Iilhaz System User Smmta System user Smmsp system User

Count the number of ordinary users and system users

[Email protected]:~/linux/awkawk -v fs=":"'{$3>=1000 ? user++: snumber++} end{print "user number:", User, "system number:", snumber}' /etc/passwd
    2

awk Prints Parity lines

1. In awk, if the action of the pattern is omitted, the default action is to print the entire line when the current row satisfies the pattern, which is {print $}

2. In awk, 0 or an empty string means "false", a non-0 value or a non-empty string means "true", when the pattern is false, the action does not execute, when the pattern is true, the action executes

[Email protected]:~/linux/awkawk'i=!i'1  3  5 7 9one line

When awk scans the first row, the I variable is initialized to null, the inverse becomes true, the whole pattern is true, so the first line is printed, the second line is reversed, and the inverse assignment becomes false .... And so on

[Email protected]:~/linux/awkawk'{i=!i;print i;} '  101010101 0 1

Reverse again, print the even line

[Email protected]:~/linux/awkawk'! i=!i)'246 8    Ten lines

Common basic commands for Linux: Three musketeers command-awk ternary expression

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.