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

[Feature Request] ExecForEach: Accurately get columns by index #152

Open
wanglong001 opened this issue Dec 6, 2022 · 7 comments
Open

[Feature Request] ExecForEach: Accurately get columns by index #152

wanglong001 opened this issue Dec 6, 2022 · 7 comments

Comments

@wanglong001
Copy link

Example

$ cat t.txt 
column1 ./go.sum
column2 ./go.mod
cat t.txt | awk '{print($2" "$1)}'  #  Swap 1, 2 columns
# ./go.sum column1
# ./go.mod column2
cat t.txt | awk '{system("md5sum "$2"; echo "$1)}' # Column 2 is the input for  md5sum, Column 1 is the  input for echo
# b42820e6479d5c5fa3a8e2daca66d1a1  ./go.sum
# column1
# 9659322eb77f7139a925f7745510c01e  ./go.mod
# column2

This is very flexible function in awk

In script, I except

script.File("t.txt").ExecForEach("md5sum $2; echo $1").Stdout()
@bitfield
Copy link
Owner

bitfield commented Dec 6, 2022

I'm not sure how this would work, because pipe stages communicate with one another by a stream of bytes—or lines—not discrete values. Where would $1 and $2 come from?

The best thing to do here might be to write a FilterLine function to compute the arguments $1 and $2 by applying strings.Fields to each line, and then ExecForEach to invoke the shell with your suggested command.

@wanglong001
Copy link
Author

I'm not sure how this would work, because pipe stages communicate with one another by a stream of bytes—or lines—not discrete values. Where would $1 and $2 come from?

The best thing to do here might be to write a FilterLine function to compute the arguments $1 and $2 by applying strings.Fields to each line, and then ExecForEach to invoke the shell with your suggested command.

Go template supports slices accessed by index, and I used this function to achieve awk's columns acquisition, like $1, $2, corresponding to {{ index .Val 0}}, {{index .Val 1}}, Raw input corresponds to {{.Raw}}

@bitfield
Copy link
Owner

bitfield commented Dec 6, 2022

Great, so is what you're really asking for that Column should be able to return multiple columns separated by whitespace?

@wanglong001
Copy link
Author

Great, so is what you're really asking for that Column should be able to return multiple columns separated by whitespace?

Yep~ , and the data of multiple columns can be obtained through index

@bitfield
Copy link
Owner

bitfield commented Dec 6, 2022

and the data of multiple columns can be obtained through index

I think the program you're suggesting is something like this, isn't it:

script.File("t.txt").ExecForEach("md5sum $2; echo $1")

Is that right?

@wanglong001
Copy link
Author

and the data of multiple columns can be obtained through index

I think the program you're suggesting is something like this, isn't it:

script.File("t.txt").ExecForEach("md5sum $2; echo $1")

Is that right?

It works for me, although not very elegant

script.File("/tmp/test/t.txt").ExecForEach(`bash -c "md5sum {{index .Cols 1}};echo {{index .Cols 0}}"`).Stdout()
// b42820e6479d5c5fa3a8e2daca66d1a1  /tmp/test/go.sum
// a
// 9659322eb77f7139a925f7745510c01e  /tmp/test/go.mod
// b

not works for this

script.File("/tmp/test/t.txt").ExecForEach(`md5sum {{index .Cols 1}};echo {{index .Cols 0}}`).Stdout()

@bitfield
Copy link
Owner

bitfield commented Dec 6, 2022

Right, because using the ; character to separate two commands is a bash thing. ExecForEach uses Go's os/exec package, which doesn't have that functionality. Luckily, the shell does!

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

2 participants