M
M
morvlad2020-06-01 23:06:36
bash
morvlad, 2020-06-01 23:06:36

Output a part of a string based on a certain condition?

Please tell me the solution to the problem.
Using grep and awk I could not do it, I probably need to make a regular expression, I have difficulties with this.

There is a text line in which certain keywords occur, after which there is text enclosed in square brackets. Inside square brackets there can be digits, letters, in principle it doesn’t matter.
After finding the word, you need to display what is in these brackets.
example:[123; 0.000; ""; "-"] example2:[456; 0.000; "";"-"]
PS Parsing by columns is not an option for me, since this is a large text file and the columns in it are not uniform.

Thanks in advance for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-06-02
@morvlad

If I understand correctly:

echo 'example:[123; 0.000; ""; "-"] example2:[456; 0.000; "";"-"] example3:[1hfgh; 0.000; ""; "-"] example:[123; 0.000; ""; "-"] example3:[2222; 0.000; ""; "-"]' \
| awk 'BEGIN{RS="]"} {print}' | grep example3 | grep  -o '\[.*' | sed 's/\[//'

1hfgh; 0.000; ""; "-"
2222; 0.000; ""; "-"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question