extract pattern using grep/sed
2
0
Entering edit mode
21 months ago
Nelo ▴ 20

Hi

Pm4.1LM10m04850 0.24924Pm4.1LM01m05240 0.02328Pm4.1LM01m11200 -0.02328Pm4.1LM01m11050 0.02899Pm4.1LM03m10920 0.04638Pm4.1LM00m08740 -0.04638Pm4.1LM09m10890 0.18085Pm4.1LM05m02500 0.23509Pm4.1LM03m01390

This is my query data above.

I want to exclude those digits that are in bold and the rest in rows like this:

 Pm4.1LM10m04850 
 Pm4.1LM01m05240
 Pm4.1LM01m11200

and so on..

I tried using grep command: grep "Pm4.1LM[0-9]" FILENAME of course it didn't work because there is character in between [0-9] pattern I used in command.

Can anyone help please

Appreciated if help is given with bit of detail explanation!!!

Thanks in advance

grep linux sed • 1.4k views
ADD COMMENT
1
Entering edit mode
21 months ago
 tr " " "\n" < input.txt | sed 's/^[0-9\.\-]*//'
ADD COMMENT
0
Entering edit mode

It works. Thank you.

ADD REPLY
0
Entering edit mode

A small educational note: if an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one answer if they work. This will help future users that might find this post find the right answer.

upvote_bookmark_accept

ADD REPLY
0
Entering edit mode
21 months ago
lacb ▴ 120
sed -r 's/\s[0-9\.\-]+Pm/ Pm/g' input.txt

outputs: Pm4.1LM10m04850 Pm4.1LM01m05240 Pm4.1LM01m11200 Pm4.1LM01m11050 Pm4.1LM03m10920 Pm4.1LM00m08740 Pm4.1LM09m10890 Pm4.1LM05m02500 Pm4.1LM03m01390

This script uses sed and a regular expression (regex): \s[0-9\.\-]+Pm

This regex matches the number with [0-9\.\-]+ (a series of numerical characters, decimal point and sign) and additional context (space before, "Pm" after)

The sed command replaces this match by only the " Pm" to remove the number.

ADD COMMENT

Login before adding your answer.

Traffic: 1797 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6