rjshaw@ramius.ocf.llnl.gov (Robert Shaw) (08/13/90)
When awk'ing something like a passwd file, where the reasonable choice of field separator is something other than whitespace, how do you let a line simply fall through and be printed unchanged? 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. The best I can do at present is a printf. Something like printf("%s:%s:%s:%s:%s:%s:%s\n",$1,$2,$3,$4,$5,$6,$7); to get the colon's back out when operating on a passwd-format file, for instance =============================================================================== rjshaw@ramius.llnl.gov _____ ____ ____ ______ R o b / / / / / / / / / / / -------- / --/ / / / / / / / / / --------------------------- /-- / / / / / / / / S h a w /____/ /_/_/ /_/_/ /_____/ The Cosby's are precisely what's wrong with television today... ===============================================================================
roger@yuba.wrs.com (Roger Rohrbach) (08/14/90)
rjshaw@ramius.ocf.llnl.gov (Robert Shaw) writes: >When awk'ing something like a passwd file, where the reasonable choice >of field separator is something other than whitespace, how do you >let a line simply fall through and be printed unchanged? >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. I don't know what version of awk you are using; both the old and GNU versions of awk print the input line unchanged unless you explicitly reset the OFS (output field separator) variable. I.e, awk -F: '{ print }' /etc/passwd prints /etc/passwd unchanged. As a matter of fact, if you want the behavior you claim to be experiencing, you not only have to set OFS, but you have to mess with one of the fields in order to get awk to recompute $0, i.e., awk -F: ' BEGIN { OFS = " " } { $1 = $1 # recompute $0 print }' /etc/passwd produces the behavior you describe. Perhaps "new awk" behaves differently; in that case, try setting OFS to FS and use this trick. Roger Rohrbach sun!wrs!roger roger@wrs.com - Eddie sez: ----------------------------------------------- (c) 1986, 1990 -. | {o >o | | \ -) I'm lurching between the aesthetic sublime and the quotidian grime. |
george@hls0.hls.oz (George Turczynski) (08/14/90)
In article <491@llnl.LLNL.GOV>, rjshaw@ramius.ocf.llnl.gov (Robert Shaw) writes: > When awk'ing something like a passwd file, where the reasonable choice > of field separator is something other than whitespace, how do you > let a line simply fall through and be printed unchanged? > I must have misunderstood your question. This works fine: tmp: awk -F: '{ if( $1 == "nobody" ) print }' /etc/passwd nobody:*:-2:-2::/: This is under SunOS 4.0.3, and works for both sh & csh. I don't know what you're running, but it might have been useful to know >:-} -- | George P. J. Turczynski. |---------------------------------------------------- | Computer Systems Engineer. | ACSnet: george@highland.oz | I can't speak for the | | Highland Logic Pty. Ltd. | Phone: +61 48 683490 | company, I can barely | | Suite 1, 348-354 Argyle St | Fax: +61 48 683474 | speak for myself... | | Moss Vale. NSW. Australia. 2577 |----------------------------------------------------
jimr@hp-lsd.COS.HP.COM (Jim Rogers) (08/15/90)
The answer is to set up the output field separator to be a colon also. For instance: awk -F":" 'OFS = ":" { print $1, $2 }' /etc/passwd will print out the first two fields of the password file with the output fields separated by colons. Jim Rogers Hewlett Packard Company
irv@happym.wa.com (Irving Wolfe) (08/15/90)
In <491@llnl.LLNL.GOV> rjshaw@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@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@happym.wa.com