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: create new field from calculating length of string in field #153

Closed
avilella opened this issue Jul 9, 2021 · 9 comments

Comments

@avilella
Copy link

avilella commented Jul 9, 2021

Hi,

This is either a feature request or a question as to how to achieve in an elegant way:

Is there a way of creating a new field which is the length of the string in another field? E.g. in pseudocode below:

$ seq 1 10 | csvtk add-header -n field1 | csvtk mutate -f field1 -n f1_len -p '^length(.+)$'
field1,f1_len
1,1
2,1
3,1
4,1
5,1
6,1
7,1
8,1
9,1
10,2

Thanks in advance

@shenwei356
Copy link
Owner

shenwei356 commented Jul 12, 2021

sorry, not supported.

@tseemann
Copy link

@avilella i've been wanting this feature too.
I don't think datamash has it either.
Very frustrating to have to switch to awk or perl here.

@shenwei356
Copy link
Owner

@avilella @tseemann

$ (echo a; seq 0 42 200  ) \
    | csvtk mutate2 -t -n b -e "\$a + 50" -L 0 \
    | csvtk mutate2 -t -n len -e 'len($a, $b)'
a       b       len
0       50      3
42      92      4
84      134     5
126     176     6
168     218     6

@avilella
Copy link
Author

Fantastic! Thank you very much!

@tolot27
Copy link

tolot27 commented Nov 8, 2021

@shenwei356 Please can you implement the len function in filter2 as well?

@shenwei356
Copy link
Owner

Sure, but it's midnight here, how about tomorrow.

And I decide to change expression evaluation package to https://github.com/antonmedv/expr someday , which has more functions. #172

@shenwei356
Copy link
Owner

Please can you implement the len function in filter2 as well?

Done. And also fixed len and ulen in mutate2, where it could not be used for arithmetic operators.

@tolot27
Copy link

tolot27 commented Nov 9, 2021

Many thanks @shenwei356. Unfortunately, this and the some other intermediate builds after 0.23 have a problem with mutate2. Windows and Linux version is affected. Most of the time, they report:

[ERRO] Invalid token: '_'
[ERRO] xopen: no content

But after running it ten or more times, it passes and returns a result. Shall I open a separate issue for that or report it a (non-working) example elsewhere?

@shenwei356
Copy link
Owner

Yes please open another issue with the demo file and commands.

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

No branches or pull requests

4 participants