Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue parsing | as field separator? #29

Closed
shah opened this issue May 26, 2019 · 4 comments
Closed

Issue parsing | as field separator? #29

shah opened this issue May 26, 2019 · 4 comments

Comments

@shah
Copy link

shah commented May 26, 2019

I have a simple program:

package main
import (
	"fmt"
	"bytes"
	"github.com/benhoyt/goawk/interp"
)

func main() {
	input := bytes.NewReader([]byte("xyz title | Healthcare IT News"))
	err := interp.Exec(" $0 { print $1}", "|", input, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
}

The output produces:
x

Instead of:
xyz title

Any idea why the "|" is not being parsed properly? If I use comma or hyphen, it works.

@benhoyt
Copy link
Owner

benhoyt commented May 27, 2019

Oh, interesting! I'm very surprised I haven't run into this before. What's going on is that I'm always treating FS as a regular expression (| is an "or" regex), when in fact other awks (and the POSIX spec) say that if FS is a single character, treat it as a literal single character, otherwise treat it as a regex. I'll fix this behavior.

@benhoyt
Copy link
Owner

benhoyt commented May 27, 2019

Thanks for the bug report, BTW. In the meantime you can use the workaround of setting FS to \\|, for example: https://play.golang.org/p/rqp_CxkVJT9

@niemeyer
Copy link

if FS is a single character, treat it as a literal single character, otherwise treat it as a regex. I'll fix this behavior.

There are more special cases, unfortunately: the single space, and the empty/null string is unspecified but actually specified for gawk.

@benhoyt
Copy link
Owner

benhoyt commented May 29, 2019

@niemeyer Thanks. Yes, I'm already handling the single space. And I handle empty string as per awk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants