Skip to main content

[linux] awk

Awk - Reporting Tool

Usage:

awk ‘/pattern/ { procedure }’ | PIPE | STDIN
Example file:
file.txt
one
two
three
one1
two10
three one
four two
one two three
- print entire line:
awk '{ print }' file.txt
or
awk '{ print $0 }' file.txt
- print specific columns ( $1, $2 .. $n):
awk '{ print $1}' file.txt
- print multiply columns:
awk '{ print $1; print $2 }' file.txt
awk '{ print $1, $2 }' file.txt
- print columns from lines containing pattern:
awk '/pattern/ { print $1 }' file.txt
- print columns from lines containing digits:
awk '/[0-9]/ { print $1 }' file.txt

Delimiters:

Default delimiter: white-space (space, tabs):
awk -F: '{ print $1 }' /etc/passwd
- support for character classes in setting the default delimiter:
awk -F "[:;.\t]"

Awk scripts:

Awk scripts consist of 3 parts:
1. Before (denoted using: BEFORE)
2. During (main Awk loop)
3. After (denoted using: END)
awk 'BEGIN { print "exmaple" }'
awk 'BEGIN { FS = ":"; print "Beginning" } $7 ~ /nologin/ { print $1, $7 } END { print "End" }' /etc/passwd
Example:
awk -f example.awk /etc/passwd
example.awk
# Component 1 - BEGIN
BEGIN { FS = ":"; print "Beginning" }

# Component 2 - Main Loop
$7 ~ /false/ { print $1, $7 }

# Component 3 - END
END { print "End" }

Awk variables:

Types:
1. System - i.e. FILENAME, RS, ORS…
awk '{ print; print "Number of fields on the line: " NF } END { print "Input file: " FILENAME }' file.txt
awk 'BEGIN { OFS="\t\t\t" }; { print $1, $2 }' file.txt
2. Scalars - i.e. a = 1
awk 'BEGIN { test_value = 10 } { print } { print test_value }' file.txt
- increment scalar variable “test_value” by one:
awk 'BEGIN { test_value = 10 } { print } { print test_value; ++test_value }' file.txt
3. Arrays - i.e. (variable_name[n]) test_value[0] = 10
awk '{ print $1, $2; class[NR] = $2 } END { for (i=1; i <= NR; i++) print "Class" i ": "class[i] }' file.txt

Awk operators:

1. Relational - ==, !=, <, >, <=, >=, ~ (RegEx matches), !~ (RegEx does NOT match)
- print lines with two or more records:
awk 'NF >=2 { print }' file.txt
- print lines where second field match pattern:
awk '$2 ~ /pattern/ { print }' file.txt
2. Boolen - || (OR), && (AND), ! (NOT)
-print records that have at least 2 fields and are positioned at record 6 and higher:
awk 'NF >=2 && NR >=6 { print }' file.txt
Awk ‘if’ statement:
awk '{ if ( $1 ~ /four/ ) print $2 }' file.txt
awk '{ if ( $1 == "four" ) print $2; else print $1 }' file.txt

Awk loops:

- while, do and for
-examples:
awk '{ for(i=1; i<=5; ++i) print $0,i }' file.txt
awk 'BEGIN { for (i=1; i <= 10; ++i) print i }'
awk 'BEGIN { for (i=1; i <= ARGV[1]; ++i) print i }' 10
awk 'BEGIN { max=ARGV[1]; for (i=1; i <= max; ++i) print i }' 10

Awk Printf formatting:

Usage:
printf (“format”, arguments)
Supported Printf formats:
1. “%c” - ASCII characters
2. “%d” - Decimals - NOT floating point values OR values to the right of the decimal point
3. “%f” - Floating point
4. “%s” - Strings
NOTE: Printf doesn’t print newline character(s)
NOTE: Default output is right-justified. Use ‘-‘ to indicate left-justification
General format section:
[-]width.precision[cdfs]
1. width - influence the actual width of the column to be output
2. precision - influence the number of places to the right of the decimal point
- print examples:
awk 'BEGIN { printf("test\n") }'
awk 'BEGIN { printf ("Output:\n") } { printf ("%s\n", $1) }' file.txt
awk 'BEGIN { printf ("Output:\n") } { printf ("%s\t%s\n", $1,$2 ) }' file.txt
- apply precision:
awk 'BEGIN { printf ("Output:\n") } { printf ("%.3s\n", $1 ) }' file.txt
- apply width:
awk 'BEGIN { printf ("Output:\n") } { printf ("%20s\t%20s\n", $1,$2 ) }' file.txt
- apply width (output left-justified)
awk 'BEGIN { printf ("Output:\n") } { printf ("%-20s\t%-20s\n", $1,$2 ) }' file.txt
Example file:
file2.txt
Ferrari 100000.67
Porshe 250000
Lamborgini 350000.99
awk 'BEGIN { printf ("Price list:\n\n") } { printf ("%-10s\t£%.2f\n", $1,$2 ) }' file2.txt
awk '{ cars[NR] = $1 } END { print "Total Command-line Arguments: " ARGC; for ( i=1; i <= NR; i++) printf ("%-12s %1d %-2s %-10s\n", "CARS", i, ": ", cars[i] ) }' file2.txt
- apply upper and lower-case formatting to Printf values:
awk '{ cars[NR] = $1 } END { for ( i=1; i <= NR; i++) printf ("%-12s %1d %-2s %-10s\n", "CARS", i, ": ", toupper(cars[i]) ) }' file2.txt
awk '{ cars[NR] = $1 } END { for ( i=1; i <= NR; i++) printf ("%-12s %1d %-2s %-10s\n", "CARS", i, ": ", tolower(cars[i]) ) }' file2.txt
- include headers:
awk '{ cars[NR] = $1 } END { printf ("%-12s %-1s %10s\n\n", "Cars", "Count", "Make"); for ( i=1; i <= NR; i++) printf ("%-12s %1d %-9s %-15s\n", "CARS", i, ": ", cars[i] ) }' file2.txt

Comments

Popular posts from this blog

Browser User Agent List

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36 Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/601.5.17 (KHTML, like Gecko) Version/9.1 Safari/601.5.17 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0 Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0;...

[linux] Adjusting child processes for PHP-FPM (Nginx)

Adjusting child processes for PHP-FPM (Nginx) Problem: The following warning message appears in the logs: [26-Jul-2012 09:49:59] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 8 idle, and 58 total children [26-Jul-2012 09:50:00] WARNING: [pool www] server reached pm.max_children setting (50), consider raising it It means that there are not enough PHP-FPM processes. Solution: We need to calculate and change these values based on the amount of memory on the system: /etc/php-fpm.d/www.conf pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 - the following command will help us to determine the memory used by each (PHP-FPM) child process: ps -ylC php-fpm --sort:rss The RSS column shows non-swapped physical memory usage by PHP-FPM processes in kilo Bytes. On an average each PHP-FPM process took ~75MB of RAM on my machine. Appropriate valu...

[symfony] Assert in Entity

* @Assert\NotBlank() * @Assert\Blank() * @Assert\NotNull() * @Assert\Null() * @Assert\True(message = "The token is invalid") * @Assert\False( *     message = "You've entered an invalid state." * ) * @Assert\Type(type="integer", message="The value {{ value }} is not a valid {{ type }}.") is_ array bool callable float double int integer long null numeric object real resource scalar string ctype_ alnum alpha cntrl digit graph lower print punct space upper xdigit * @Assert\Email( *     message = "The email '{{ value }}' is not a valid email.", *     checkMX = true * ) * @Assert\Length( *      min = 2, *      max = 50, *      minMessage = "Your first name must be at least {{ limit }} characters long", *      maxMessage = "Your first name cannot be longer than {{ limit }} characters long" * ...