user3190479
user3190479

Reputation: 355

How to search and copy particular pattern from a file to another file

i have a file like this

MY-27125:some text
MY-27132:some text
MY-26565:some text
some text
MY-26097:some text
MY-27129:some text
MY-27211  && MY-26565:some text
some another text
MY-27175/MY-27147:some text
MY-27159\MY-27149:some text
MY-26747,MY-26945:some text
MY-20811 MY-26889:some text

i want to use sed or notepad++ or awk to have only text starting with MY-[5 digit number].

Output should be like below

MY-27125
MY-27132
MY-26565
MY-26097
MY-27129
MY-27211
MY-27175
MY-27159
MY-26747
MY-20811
MY-26565
MY-27147
MY-27149
MY-26945
MY-26889

Upvotes: 1

Views: 74

Answers (4)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed -n "/\(MY-[0-9]\{5\}\).*/ s//\1/p" YourFile

add -posix on GNU sed

Upvotes: 2

Jotne
Jotne

Reputation: 41446

Using gnu awk

awk 'NR>1 {print RS substr($0,1,5)}' RS="MY-" file
MY-27125
MY-27132
MY-26565
MY-26097
MY-27129
MY-27211
MY-26565
MY-27175
MY-27147
MY-27159
MY-27149
MY-26747
MY-26945
MY-20811
MY-26889

Upvotes: 2

user1907906
user1907906

Reputation:

grep '^MY-' < in.txt | cut -b1-8 
MY-27125
MY-27132
MY-26565
MY-26097
MY-27129
MY-27211
MY-27175
MY-27159
MY-26747
MY-20811

Upvotes: 1

user000001
user000001

Reputation: 33317

Grep is the right tool:

grep -oP 'MY-\d{5}' file

Upvotes: 3

Related Questions