0

I've below text, how can I extract list

[83472, 8327489] and abcd: 32324 ?

[83472, 8327489]abcd: 32324Expr bla bla
[243fd]abcd: 4342Expr ieddsk
[2324, 34543, 8937423]abcd: 3232432423423Expr bla bla
2
sed -E 's/(\[[^]]*\]abcd:[[:blank:]]*[[:digit:]]*).*/\1/' file

should do it.

Sample Output

[83472, 8327489]abcd: 32324
[243fd]abcd: 4342
[2324, 34543, 8937423]abcd: 3232432423423
  • Why not just sed -E 's/Expr.*//' file ? – DJMcMayhem Sep 1 '16 at 20:17
  • @DJMcMayhem That would not work in the rare cases like [Expr. 8327489] abcd: 32324Expr bla bla – sjsam Sep 1 '16 at 20:23
0

With grep and pcre

1) to extract upto but not including Expr and rest of line

$ grep -oP '.*(?=Expr)' file
[83472, 8327489]abcd: 32324
[243fd]abcd: 4342
[2324, 34543, 8937423]abcd: 3232432423423

2) to extract [...]abcd: numbers

$ grep -oP '\[[^]]*\]abcd:\s*\d+' file
[83472, 8327489]abcd: 32324
[243fd]abcd: 4342
[2324, 34543, 8937423]abcd: 3232432423423

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.