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

Find and replace special character & with and using ssub #1546

Open
karudonaldson opened this issue Apr 18, 2024 · 2 comments
Open

Find and replace special character & with and using ssub #1546

karudonaldson opened this issue Apr 18, 2024 · 2 comments

Comments

@karudonaldson
Copy link

Check Column D (four) for special characters:

  1. Replace & with string AND
  2. Remove characters ' ( ) /

$ cat example.csv

one,two,three,four,five
pan,pan,1,&,10
wye,wye,1,&,20
eks,wye,,1,'',10
zee,pan,1,(test),60

After transformation
$ cat example.csv

one,two,three,four,five
pan,pan,1,and,10
wye,wye,1,and,20
eks,wye,1,,10
zee,pan,1,,60
@karudonaldson karudonaldson changed the title Find and replace special string with string using ssub including special characters Find and replace special character & with and using ssub Apr 18, 2024
@aborruso
Copy link
Contributor

@karudonaldson please also always write down what test you did, what didn't work for you. Don't just write what you want.

So it will be possible to give you a better answer. Thank you

@johnkerl
Copy link
Owner

johnkerl commented Apr 18, 2024

@aborruso I think this question is well-posed

@karudonaldson how about:

cat 1546.csv

one,two,three,four,five
pan,pan,1,&,10
wye,wye,1,&,20
eks,wye,1,'',10
zee,pan,1,(test),60

cat 1546.mlr

for (k in ["four"]) {
    v = $[k];
    if (typeof(v) == "string") {
        v = ssub(v, "&", "AND");
        v = gsub(v, "['()/]", "");
    }
    $[k] = v;
}

mlr --csv --from 1546.csv put -f 1546.mlr

one,two,three,four,five
pan,pan,1,AND,10
wye,wye,1,AND,20
eks,wye,1,,10
zee,pan,1,test,60

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

3 participants