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 Suggestion: Fields #197

Open
rmasci opened this issue Dec 20, 2023 · 4 comments
Open

Feature Suggestion: Fields #197

rmasci opened this issue Dec 20, 2023 · 4 comments

Comments

@rmasci
Copy link

rmasci commented Dec 20, 2023

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:

four|two|one|three

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()

five|two|eight

I would have just rewrote Column, but that would break compatibility. So I created the Fields option. Maybe call it 'cut'?

@bitfield
Copy link
Owner

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?

@rmasci
Copy link
Author

rmasci commented Dec 20, 2023

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:

jt1234:x:1001:1001:Travers, Johnathon, [email protected]:/home/jt1234:/bin/bash
lh5678:x:1002:1002:Hargrove, Lillian, [email protected]:/home/lh5678:/bin/bash
ab9012:x:1003:1003:Blackwood, Aurora, [email protected]:/home/ab9012:/bin/bash

The report needs to contain the following fields: First, Last, Email, UID, Home Directory. You could do that like this:

script.File("password").Fields(":", ",", 1, 5, 6).Fields(",", ",", 3, 2, 1, 4, 5).Table("Header=First,Last,Email, User, Home Directory")

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.

+--------------+--------------+-----------+-------------------------+-----------------+
|     First    |     Last     |    User   |          Email          |       Home      |
+==============+==============+===========+=========================+=================+
|   Johnathon  |    Travers   |   jt1234  |   [email protected]  |   /home/jt1234  |
|    Lillian   |   Hargrove   |   lh5678  |   [email protected]  |   /home/lh5678  |
|    Aurora    |   Blackwood  |   ab9012  |   [email protected]  |   /home/ab9012  |
+--------------+--------------+-----------+-------------------------+-----------------+

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.

@bitfield
Copy link
Owner

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 (email). Then, when we reassemble the data, using those names makes it easy to see what the output will look like.

What do you think the equivalent API would be with something like Fields?

@rmasci
Copy link
Author

rmasci commented Dec 21, 2023

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:

awk -F":" '{print $1","$5","$6}' passwd | awk -F"," '{print $3","$2","$1","$4","$5}' 

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. :)

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