awk '{ if (NF > max) max = NF }
END { print max }'
This program prints the maximum number of fields on any input line.
awk 'length($0) > 80'
This program prints every line longer than 80 characters. The sole rule has
a relational expression as its pattern, and has no action (so the default
action, printing the record, is used).
awk 'NF > 0'
This program prints every line that has at least one field. This is an easy
way to delete blank lines from a file (or rather, to create a new file
similar to the old file but from which the blank lines have been deleted).
awk '{ if (NF > 0) print }'
This program also prints every line that has at least one field. Here we
allow the rule to match every line, then decide in the action whether to
print.
awk 'BEGIN { for (i = 1; i <= 7; i++)
print int(101 * rand()) }'
This program prints 7 random numbers from 0 to 100, inclusive.
ls -l files | awk '{ x += $4 } ; END { print "total bytes: " x }'
This program prints the total number of bytes used by files.
expand file | awk '{ if (x < length()) x = length() }
END { print "maximum line length is " x }'
This program prints the maximum line length of file. The input is piped
through the expand program to change tabs into spaces, so the widths
compared are actually the right-margin columns.
awk 'BEGIN { FS = ":" }
{ print $1 | "sort" }' /etc/passwd
This program prints a sorted list of the login names of all users.
awk '{ nlines++ }
END { print nlines }'
This programs counts lines in a file.
awk 'END { print NR }'
This program also counts lines in a file, but lets awk do the work.
awk '{ print NR, $0 }'
This program adds line numbers to all its input files, similar to `cat -n'.
awk -F "\"*,\"*" '{print $3}' file.csv
CSV parsing, prints 3rd field
Showing posts with label awk. Show all posts
Showing posts with label awk. Show all posts
Wednesday, July 25, 2012
Most useful awk programs
Labels:
administration,
AIX,
awk,
Scripting,
Shell,
Unix / Linux
Friday, May 11, 2012
how to extract the definitions of indexes in an Oracle export file without connection to the database
Today I found myself in a situation where I needed definitions of indexes of an Oracle 9i database, but everything I had on me was an export file and no access to a database.
I discovered a simple way and without connection to a database to extract the definitions of indexes from an export file of an ORACLE database running on AIX 6.1
This unix command will extract the indexes definition from the export file export_file.dmp and save it in a sql file indexes_files.sql
After that i needed to reformat the sql file and include a slash (/) after every definition in the file
cat achats_indexes.sql | awk '$0=$0 " \n/" {print $0}' >> output_indexes_files.sql
That's all, you can combine these two commands to create a shell script to extract indexes definition from oracle export files, taking 2 parameters : the export file and the sql output file.
I discovered a simple way and without connection to a database to extract the definitions of indexes from an export file of an ORACLE database running on AIX 6.1
grep "CREATE INDEX" export_file.dmp > indexes_files.sql
This unix command will extract the indexes definition from the export file export_file.dmp and save it in a sql file indexes_files.sql
After that i needed to reformat the sql file and include a slash (/) after every definition in the file
cat achats_indexes.sql | awk '$0=$0 " \n/" {print $0}' >> output_indexes_files.sql
That's all, you can combine these two commands to create a shell script to extract indexes definition from oracle export files, taking 2 parameters : the export file and the sql output file.
Subscribe to:
Posts (Atom)