awk command examples Flashcards

1
Q

Fields in awk

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Inserting a couple of tabs and a string

A

kelly@octarine ~/test> ls -ldh * | grep -v total | \ awk ‘{ print “Size is “ $5 “ bytes for “ $9 }’

Size is 160 bytes for orig Size is 121 bytes for script.sed

Size is 120 bytes for temp_file

Size is 126 bytes for test

Size is 120 bytes for twolines

Size is 441 bytes for txt2html.sh

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

take out any number of columns and even reverse the order

A

kelly@octarine ~> df -h | sort -rnk 5 | head -3 | \ awk ‘{ print “Partition “ $6 “\t: “ $5 “ full!” }’

Partition /var : 86% full!

Partition /usr : 85% full!

Partition /home : 70% full!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

displays only local disk device information, networked file systems are not shown

A

kelly is in ~> df -h | awk ‘/dev\/hd/ { print $6 “\t: “ $5 }’

/ : 46%

/boot : 10%

/opt : 84%

/usr : 97%

/var : 73%

/.vol1 : 8%

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

search the /etc directory for files ending in “.conf” and starting with either “a” or “x”, using extended regular expressions

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In order to precede output with comments, use the BEGIN statement

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The END statement can be added for inserting text after the entire input is processed

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

awk script contains awk statements defining patterns and actions

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

input field separator

A

kelly is in ~> awk ‘BEGIN { FS=”:” } { print $1 “\t” $5 }’ /etc/passwd

–output omitted–

kelly Kelly Smith

franky Franky B.

eddy Eddy White

willy William Black

cathy Catherine the Great

sandy Sandy Li Wong

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

input field separator in awk script

A

kelly is in ~> cat printnames.awk

BEGIN { FS=”:” } { print $1 “\t” $5 }

kelly is in ~> awk -f printnames.awk /etc/passwd –output omitted–

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The output field separator

A

kelly@octarine ~/test> cat test

record1 data1

record2 data2

kelly@octarine ~/test> awk ‘{ print $1 $2}’ test record1data1

record2data2

kelly@octarine ~/test> awk ‘{ print $1, $2}’ test

record1 data1

record2 data2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

output record separator

A

kelly@octarine ~/test> awk ‘BEGIN { OFS=”;” ; ORS=”\n–>\n” } \ { print $1,$2}’ test

record1;data1

–>

record2;data2

–>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

count the total number of records

A

kelly@octarine ~/test> cat processed.awk

BEGIN { OFS=”-“ ; ORS=”\n–> done\n” } { print “Record number “ NR “:\t” $1,$2 } END { print “Number of records processed: “ NR }

kelly@octarine ~/test> awk -f processed.awk test Record number 1: record1-data1

–> done

Record number 2: record2-data2

–> done

Number of records processed: 2

–> done

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly