-
-
Notifications
You must be signed in to change notification settings - Fork 319
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
Propose adding a delimeter to Column() #180
Comments
Hey @rmasci Can I work on this issue? |
What about something like: script.File("data.csv").Fields(",").Columns(1, 3, 9).Stdout() |
sounds nice to me if we can extend it to CSVs along with strings, Should I start working on it ? |
I did this. You can specify the delimiter, the fields like 1,3,7, and the
delimiter out. I even added a CSV formatter to convert the CSV to a table
like MySQL, grid, or an HTML table. I wanted to ask if it was wanted before
issuing a pull request. I keep it in my version of 'script' in my github.
…On Sat, Sep 7, 2024 at 2:59 PM Rupesh Prajapati ***@***.***> wrote:
sounds nice to me if we can extend it to CSVs along with strings
—
Reply to this email directly, view it on GitHub
<#180 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGRIPKUESWPS5O7PHXBXSDZVNEHRAVCNFSM6AAAAABNXBGB4SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMZWGQYDMNBWG4>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
For example:
*sample.csv:*
Name,Abbreviation,Numeric,Numeric2
Monday,Mon,1,01
Tuesday,Tue,2,02
Wednesday,Wed,3,03
Thursday,Thu,4,04
Friday,Fri,5,05
Saturday,Sat,6,06
Sunday,Sun,7,07
Code:
script.File("sample.csv").Fields(",", "|", 1, 3).Stdout()
Documentation:
func (p *Pipe) Fields(inDelim string, outDelim string, a ...int) *Pipe"
*Output:*
Name|Numeric
Monday|1
Tuesday|2
Wednesday|3
Thursday|4
Friday|5
Saturday|6
Sunday|7
This gives you the option to change the delimiter in the output. I guess 'outDelim' could be eliminated to make the command simpler. 'Feilds(",", 1,3,7,12)' It's sort of like awk -F"," '{print $1,$3,$7,$12}'
I also added a 'Table' feature:
script.File("sample.csv").Fields(",", ",", 2,4).Table()
```
+-----------------+-------------+
| Abbreviation | Numeric-2 |
+============+==========+
| Mon | 01 |
| Tue | 02 |
| Wed | 03 |
| Thu | 04 |
| Fri | 05 |
| Sat | 06 |
| Sun | 07 |
+-----------------+--------------+
```
(looks better in markdown)
…On Sat, Sep 7, 2024 at 3:25 PM Richard Masci ***@***.***> wrote:
I did this. You can specify the delimiter, the fields like 1,3,7, and the
delimiter out. I even added a CSV formatter to convert the CSV to a table
like MySQL, grid, or an HTML table. I wanted to ask if it was wanted before
issuing a pull request. I keep it in my version of 'script' in my github.
On Sat, Sep 7, 2024 at 2:59 PM Rupesh Prajapati ***@***.***>
wrote:
> sounds nice to me if we can extend it to CSVs along with strings
>
> —
> Reply to this email directly, view it on GitHub
> <#180 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAGRIPKUESWPS5O7PHXBXSDZVNEHRAVCNFSM6AAAAABNXBGB4SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMZWGQYDMNBWG4>
> .
> You are receiving this because you were mentioned.Message ID:
> ***@***.***>
>
|
Very cool! I'm inspired by |
Just my two cents: How about making the delimiter variant a separate Method, like |
What about this suggested API, to separate the two concerns of parsing the fields and selecting the fields? |
Sounds good, I guess this could/should then have variants for different types of delimiter definition mechanisms (string, regexp, maybe a func)? Speaking of which, having a func option would allow for the regexps I was wishing for to be handled inside that, too, alleviating the need to have a regexp variant. |
I did this in my version of script. I asked at the time if you wanted a pull request but didn't hear anything. This is my version on go.doc: script.File("sample.csv").Fields(",", ",", 2, 4).Stdout() //.Table()
[08:23 PM rmasci@Richs-Mac-mini ~/go/scriptcsv] $ go run main.go
Abbreviation,Numeric-2
Mon,01
Tue,02
Wed,03
Thu,04
Fri,05
Sat,06
Sun,07 If you replacer .Stdout with .Table() function it formats it for you.
|
Right now Columns takes an int, and uses strings.Fields to split the line. I'd like to propose we add a delimiter, so that you could parse csv, semicolon, or pipe delimited files. The function would look like this:
d is a ...string means it takes 0 or more arguments, so if the user doesn't pass anything it still works maintaining compatibility with existing programs
Ex:
would still print 2. However
Would also print 2
Sort of a simple change, and it works for a case where I want to parse through comma separated lines. While I think it works, I might not be thinking of a use case where it would break existing code.
The text was updated successfully, but these errors were encountered: