mors
mors

Reputation: 507

bash: retrieving number after a given string

I have a file with the following:

  blabla sometinh#LULWUT-12342"asa
  haha"LULWUT-9635bgh
  haha'LULWUT-3679//stuff

The pattern LULWUT- always precedes a four of five number sequence. How can I retrive the number pattern?

For the example abode it would be

blabla sometinh#LULWUT-12342"asa > LULWUT-12342
haha"LULWUT-9635bgh              > LULWUT-9635
haha'LULWUT-3679//stuff          > LULWUT-3679

Upvotes: 2

Views: 85

Answers (5)

smac89
smac89

Reputation: 43068

grep -o 'LULWUT-[0-9]*' sample.txt

Upvotes: 1

vapace
vapace

Reputation: 350

This is a bash-only solution.

shopt -s extglob # needed for the %%*() pattern below

while read line
do
    line_without_prefix="${line##*LULWUT}"    # remove longest prefix until LULWUT
    line_without_suffix="${line_without_prefix%%*([^0-9])}"   # remove longest non-digit suffix
    echo "LULWUT$line_without_suffix"
done

shopt -u extglob # undo, if extglob is not needed

Upvotes: 2

Dru
Dru

Reputation: 1428

Your could try using the following command to replace (s ubstitute) text in the input file.

sed 's/.*LUL/LUL/' file.txt > newfile.txt

It will create a new file called newFile.txt

Upvotes: 1

user000001
user000001

Reputation: 33307

To only match LULWUT- followed by a four or file digit pattern, you can use this:

grep -o 'LULWUT-[0-9]\{4\}[0-9]\?' file

Upvotes: 2

kojiro
kojiro

Reputation: 77059

grep -o 'LULWUT-[0-9][0-9]*' file

which uses the nonstandard -o extension to grep (but that is nonetheless available on Mac OS, GNU and BSD systems).

Upvotes: 7

Related Questions