Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk - ignore pattern

Tags:

regex

shell

awk

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?

like image 367
wolfgunner Avatar asked Sep 22 '26 22:09

wolfgunner


2 Answers

You can just match ]; or ] *; like this:

awk '/var/ , /] *;/' file

var x=[
        0, 1;
        1,0;
        2,1;
        3,2];
like image 165
anubhava Avatar answered Sep 24 '26 11:09

anubhava


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
like image 23
RavinderSingh13 Avatar answered Sep 24 '26 12:09

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!