NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: bin/59766: awk does not handle RS="\0"



The following reply was made to PR bin/59766; it has been noted by GNATS.

From: RVP <rvp%SDF.ORG@localhost>
To: gnats-bugs%netbsd.org@localhost
Cc: 
Subject: Re: bin/59766: awk does not handle RS="\0"
Date: Mon, 17 Nov 2025 00:08:10 +0000 (UTC)

 On Sun, 16 Nov 2025, dholland%NetBSD.org@localhost wrote:
 
 >> Description:
 >
 > Awk does the wrong thing if you try to set the record separator to
 > NUL; it prints only the first record. This would be useful to have
 > working in conjunction with find -print0.
 >
 
 The usual way to do this portably is to use find(1) with `-exec' and turn the
 filenames into cmd. line args.:
 
 ```
 find . -type f -exec awk 'BEGIN { for (i=1; i<ARGC; i++) print ARGV[i] }' {} +
 ```
 
 You can also use xargs(1) even if neither it nor find(1) supports `-0' or `-print0':
 
 ```
 $ cat qstr.awk
 #!/usr/bin/awk -f
 #
 # quote strings (typically, filenames) for use with xargs(1)
 
 BEGIN {
  	if (ARGC < 2)
  		exit 1
  	pat = "[\"'\\\\[:space:]]"	# " ' \ [:space:]
  	for (i = 1; i < ARGC; i++) {
  		if (match(ARGV[i], pat)) {
  			gsub(pat, "\\\\&", ARGV[i])
 
  			#for (j = 1; j <= length(ARGV[i]); j++) {
  			#	c = substr(ARGV[i], j, 1)
  			#	s = s ((c ~ pat) ? "\\"c : c)
  			#}
  		}
  		s = s ARGV[i] ((i < ARGC-1) ? " " : "")
  	}
  	print s
 }
 
 $ find . -type f -exec ./qstr.awk {} + | xargs printf '>%s<\n'
 >./polipo.pid<
 >./.X0-lock<
 >./qemu.sh<
 >./boot-com.iso<
 >./boot.iso<
 >./SHA512<
 >./qstr.awk<
 
 $ ./qstr.awk $'\n\nhello\nworld\n\n' $'a\n\\\nb\n*!$$\n\n' | xargs printf '>%s<\n'
 >
 
 hello
 world
 
 <
 >a
 \
 b
 *!$$
 
 <
 
 $
 ```
 
 HTH,
 
 -RVP
 


Home | Main Index | Thread Index | Old Index