I'm trying to extract some text using awk.
Here is the sample file:
...
var a=2;
var x=[
0, 1;
1,0;
2,1;
3,2];
other text
//this is a comment with brackets []
So, when I execute the following command :
awk '/var/ , /;/' file
I obtain :
var a=2;
var x=[
0, 1;
Result expected :
var a=2;
var x=[
0, 1;
1,0;
2,1;
3,2];
Logically, the previous command took the first ; and print the result.
The process should ignore ; if that one is matching with the following regex: ^[\t\ ]{1,}[0-9,]{1,}.*;$
Do you have any idea about it?
You can just match ]; or ] *; like this:
awk '/var/ , /] *;/' file
var x=[
0, 1;
1,0;
2,1;
3,2];
With your shown samples please try following GNU awk code. Written and tested with shown samples Only.
awk -v RS='(^|\n)var x=\\[[^]]*\\]' 'RT && sub(/^\n/,"",RT){print RT}' Input_file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With