1

I have a csv where I need to trim some text from a column probably using a regex. This is an example:

test.csv:

Number Text Group
1 the id is G11 (sometimes) A
2 the id is G12 B
3 the id is G15 C
4 the id is Z13 (always) D

I want to only grab the code value like (G11, G12, G15, Z13) from the Text column and replace the value so that I would get:

Number Text Group
1 G11 A
2 G12 B
3 G15 C
4 Z13 D

I have tried using forms of grep, awk, and sed but haven't got anything to work. I'm unfamiliar with bash, but essentially I want a new csv that looks like table 2.

These are my attempts, unsure how to only work on the Test column....

sed 's/([A-Z]\d{2}).*/([A-Z]\d{2})' test.csv > test2.csv

1 Answer 1

2

Using sed

sed -E '1!s/([^a-z]*)[^A-Z]*([^ ]* )[^|]*/\1\2/'
Number Text Group
1 G11 A
2 G12 B
3 G15 C
4 Z13 D

1! - Do not match line 1

([^a-z]*) - Retain everything within the parenthesis up until the next occurrence of lower case letters which will later be returned with back reference \1.

[^A-Z]* - Exclude everything up till the next occurrence of capital letters as it is not captured within parenthesis.

([^ ]* ) - Retain everything up to the next occurrence of a space and including a space which will later be returned with back reference \2.

[^|]* - Exclude everything up till the next occurrence of a pipe symbol

Anything not included in the find will be returned with the replacement.

Sign up to request clarification or add additional context in comments.

3 Comments

What is the 1! do at the start? Also I need to get rid of the bracket (sometimes) and (always) in my final output
@lmmjrf Do not match line 1, so no modification is carried out on that line
@lmmjrf Is the bracket with the contents literally in your data? You can try this sed -E '1!s/([^a-z]*)[^A-Z]*([^ ]* )[^|]*/\1\2/' input_file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.