-F option for awk

Irving Wolfe irv at happym.wa.com
Wed Aug 15 23:57:33 AEST 1990


In <491 at llnl.LLNL.GOV> rjshaw at ramius.ocf.llnl.gov (Robert Shaw) writes:

>print; or print $0; don't do the right thing because the lines come
>out with spaces as the field separators instead of the character given
>to the -F option.


If you change any field, $1 to $NF, then print or print $0 re-composes $0 from
all the individual fields, stringing them together with the output separator
OFS which defaults to space unless you change it.  If you don't touch any
fields, awk should just print the originally input $0.

Thus 
awk -F: '/^i/' /etc/passwd
(here) produces
irv:-------------:101:1:0000-Irving Wolfe(0000):/u/irv:/bin/gsh
 and
awk     '/^i/' /etc/passwd
produces
irv:-------------:101:1:0000-Irving Wolfe(0000):/u/irv:/bin/gsh
exactly the same thing.

Both print the /etc/passwd lines that begin with i exactly as is.

But
awk -F: '/^i/ {$2 = "hoho"; print}' /etc/passwd
produces:
irv hoho 101 1 0000-Irving Wolfe(0000) /u/irv /bin/gsh
It will replace all the :s with spaces.

You need
awk -F: 'BEGIN {OFS = FS} /^i/ {$2 = "hoho"; print}' /etc/passwd
which produces
irv:hoho:101:1:0000-Irving Wolfe(0000):/u/irv:/bin/gsh

-- 
 Irving Wolfe    Happy Man Corp.   irv at happym.wa.com    206/463-9399 ext.101
 4410 SW Point Robinson Road,  Vashon Island, WA  98070-7399     fax ext.116
 SOLID VALUE, the investment letter for Benj. Graham's intelligent investors
 Information free (sample $10 check or credit card): email patty at happym.wa.com



More information about the Comp.unix.questions mailing list