Search code examples
linuxbashawk

Can i use regex in the Record Separator in awk in linux


I have the test file like this

fdsf fdsf fdsfds fdsf
fdsfdsfsdf fdsfsf
fsdfsdf var12=1343243432

fdsf fdsf fdsfds fdsf
fdsfsdfdsfsdf
fsdfsdf var12=13432434432

fdsf fdsf fdsfds fdsf
fsdfsdf fdsfsf var12=13443432432

Now i want to use var12=\d+ as the record separator. Is this possible in awk


Solution

  • Yes, however you should use [0-9] instead of \d:

    awk '1' RS="var12=[0-9]+" file
    

    IIRC, only GNU awk can use multi-character record separators.

    Results:

    fdsf fdsf fdsfds fdsf
    fdsfdsfsdf fdsfsf
    fsdfsdf 
    
    
    fdsf fdsf fdsfds fdsf
    fdsfsdfdsfsdf
    fsdfsdf 
    
    
    fdsf fdsf fdsfds fdsf
    fsdfsdf fdsfsf 
    

    Please post your desired output if you need further assistance.