-
-
Notifications
You must be signed in to change notification settings - Fork 315
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 Suggestion: Fields #197
Comments
Nice suggestion, @rmasci, thanks! Are you using this method in any real-world programs right now? It would be interesting to see them, to get a sense of how this would look in use. For example, what non-space-delimited data do you need to re-order or change the delimiter of? |
I have to manipulate multi lined data all the time, and that data might have various delimiters. Looking at my bash history it's filled with awk '{print}' lines... This came out of just one program I was writing internally to my company, I needed column to do something similar to awk '{print $1, $4, $7}'. In my version I modified Column to do what I wanted it to, but if I were to submit that as a PR to bitfield/script it would break things, so I made it Fields instead. One example I came up with is lets say I wanted to write a report from an /etc/passwd file and it's formatted like this:
The report needs to contain the following fields: First, Last, Email, UID, Home Directory. You could do that like this:
What this will do is take the : delimited line, and create a comma delimited line using fields 1,5,6. Field 5 has the format of last, first, email. So now we have a line that has five fields and are comma separated. So order them First, Last, UID, EMail, and Home. This outputs in CSV, I could put it to Stdout, but I use another I am working on called 'Table' to format that output.
Table takes any csv output, and as long as all the column counts are the same will give you output as a table. I built 'table' as a tool for my users who spent hours trying to make the output of their scripts look nice, and I added it to my version of script. Just output in CSV, pipe it to Table. Be glad to put a PR for table as well, mainly just wanted to show you how I would use Field as an example. |
Yes, that's a helpful example, but I find code like this a little confusing to read: ...Fields(":", ",", 1, 5, 6).Fields(",", ",", 3, 2, 1, 4, 5)... Since you can't see either the input data or the output data, it's hard to work out what effect this is going to have. In a regular Go program, we would probably grab each of these fields and give them symbolic names ( What do you think the equivalent API would be with something like |
Thanks John for your reply, please I am not trying to be argumentative :) It seems nearly all my bash scripts used awk to pick out and reorder the output to the way I needed it, and frequently I'd use the -F to specify the field. Usually the syntax in those are not easy to follow either. My example in Bash:
I am a huge fan of script, I recommend it to my coworkers all the time as a way to use Go instead of bash or python. I can see your point in that with every add like this script becomes more complex, and that discourages users. For me I had an instance where I needed to parse output that wasn't space delimited, needed three of the nine fields, and reorder those fields. It works for me thought I'd share it, and is why I opted to discuss it first before just shooting a PR. :) |
I've added to my version of Script an option that I thought it might be useful to the rest of the Script users but I don't want to just issue a pull request that isn't wanted. I've documentation and tests as per the guidelines, let me know if this is wanted by the script team and I'll submit my pr.
Fields
We have the column option, and you can get one column IF its space separated. Fields takes three options. inDelimeter, outDelimeter, and the fields you want displayed. You could do something like this:
script.Echo("one;two;three;four").Fields(";","|",4,2,1,3).Stdout() and the the following output:
This has the input semicolon separated, the output is pipe separated, and the fields ordered. Fields would allow you to not only change the delimiter in and out, but specify the order of the output, or only the fields you want. If you had 10 columns you could specify only three of them, and put them in the desired order.
script.Echo("one;two;three;four;five;six;seven;eight;nine;ten").Fields(";","|",5,2,8).Stdout()
I would have just rewrote Column, but that would break compatibility. So I created the Fields option. Maybe call it 'cut'?
The text was updated successfully, but these errors were encountered: