0

I have a simple list like following

ss_Asd_n1_455_9_1
ss_Asd_n1_98_9_32
ss_Asd_n1_562_9_145
ss_Asd_n1_1_9_6

Using regex linux I want the list to trimmed like follwing

ss_Asd_n1_455_9
ss_Asd_n1_98_9
ss_Asd_n1_562_9 
ss_Asd_n1_1_9

I tried the following code but it did not print any output

grep '[a-z_]*[a-zA-Z]+(\_)[v0-9]+(\_)[0-9]+(\_)[0-9]'

Kindly guide me. Thanks in advance

2
  • 2
    Why grep if you need to modify the lines? Use sed -i.bak 's/\(.*\)_[^_]*$/\1/' file Commented Nov 24, 2017 at 9:46
  • 4
    Or sed -i.bak 's/_[0-9]*$//' file. Do you really need to check the format that thoroughly? Commented Nov 24, 2017 at 10:00

2 Answers 2

1

Looks like you simply want sed 's/_[^_]*$//' filename

To edit the file in-place, use sed -i (or if you are on BSD / MacOS, sed -i '').

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

Comments

0

using grep I'd use the perl regex syntax 'P' and match only the matching portion 'o'

echo ss_Asd_n1_455_9_1 | grep -oP '[a-z]*_(?:[a-z]|[A-Z])+_[a-z][0-9]+_[0-9]+_[0-9]+'
ss_Asd_n1_455_9

Comments

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.