O'Reilly logo

Linux Shells by Example by Ellie Quigley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.14. Review

7.14.1. String Functions

% cat datafile
						northwest     NW     Joel Craig       3.0     .98     3      4
						western       WE     Sharon Kelly     5.3     .97     5     23
						southwest     SW     Chris Foster     2.7     .8      2     18
						southern      SO     May Chin         5.1     .95     4     15
						southeast     SE     Derek Johnson    4.0     .7      4     17
						eastern       EA     Susan Beal       4.4     .84     5     20
						northeast     NE     TJ Nichols       5.1     .94     3     13
						north         NO     Val Shultz       4.5     .89     5      9
						central       CT     Sheri Watson     5.7     .94     5     13
					
Example 7.94.
% awk 'NR==1{gsub(/northwest/,"southeast", $1) ;print}' datafile
						southeast     NW     Joel Craig         3.0   .98   3   4
					

Explanation

If this is the first record (NR == 1), globally substitute the regular expression northwest with southeast, if northwest is found in the first field.

Example 7.95.
% awk 'NR==1{print substr($3, 1, 3)} ' datafile
						Joe
					

Explanation

If this ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required