H. R. LEPROFESSEUR  SUBJECTS  SHOP

 SUBJECTS } LINUX } awk useful commands

Sept 6, 2023.

AWK is very useful and powerful for pattern extraction, match, print or in other words file manipulations, simple to use and get the job done.

1: Split line and print fields, try: cat list.txt | awk '{print $1, $2}'

2. Extract pattern from file and do sum of all entries matching the pattern, try: grep ” Sequential ” idle_power.txt | awk ‘{sum += $3} END {print sum}’

Here $3 represents which column is to get sum of. Idea is to grep a certain pattern from file from each line and then process output by awk to get sum of column 3. idle_power.txt:

2.345E-6 3.45E-4 1.6078E-3 Sequential …

1.345E-6 2.45E-4 3.6078E-3 Sequential …

3. Count a pattern match – in this example HARRISH to be matched in file.txt, try:awk ‘/HARRISH/{sum++} END {print “Count = “, sum}’

4. Split line and then print – in this example split with “:” and print 5th column, try: awk ‘{split ($0, a, “:”); print a[5]}’

$0 – gets whole line, a – array contains split column value per line, : – separator for split, default is space.

5. Find a pattern from file, print certain columns, and sort the output, try: grep “Leprofesseur” file.txt | awk ‘{print $1, $2, $5}’ | sort | uniq -c

Please leave a short message for errors, comments or anything else: hr@Leprofesseur.org