FriZY
FriZY

Reputation: 31

BASH | get text between brackets

could you please help me with my problem to get number from input?

I have input something like this:

Good [AA0060]    [LIB1:    61]        No   02/11/15 08:38:46

and I would like to get number after LIB1:

61

Problem is that I can get also input like this:

Good [AA0060]  AA0060  [LIB1:    61]        No   02/11/15 08:38:46

UPDATE: LIB1 should be variable. I would like have it more dynamic.

Upvotes: 0

Views: 136

Answers (3)

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

Try this sed command.

 sed 's/.*\[LIB1: \{0,\}\(.*\)\].*/\1/'  file_name 

Using the variable,

Consider,

 match="LIB1"

 sed 's/.*\['$match': \{0,\}\(.*\)\].*/\1/' file_name

Upvotes: 0

Vijay
Vijay

Reputation: 67221

use this perl command:

perl -lne 'print $1 if(/LIB1:\s+(\d+)\]/)' file

Upvotes: 0

Kalanidhi
Kalanidhi

Reputation: 5092

You can try this awk command

awk  -FLIB1: '{print $2--}' FileName

or

awk  -FLIB1: '{print $2+0}' FileName

Example :

echo "Good [AA0060]  AA0060  [LIB1:    61]        No   02/11/15 08:38:46" | awk  -FLIB1: '{print $2--}'

Output :

61

Upvotes: 1

Related Questions