4

I want to use awk to sort content of an input file into different output files.

Simple example

Assuming the following input file:

  • $cat sample.txt
    START  
    Unix  
    Linux  
    START  
    Solaris  
    Aix  
    SCO  
    

The awk program

awk '/START/{x="F"++i;}{print > x}' sample.txt

produces the following output into files:

  • $ cat F1
    START  
    Unix  
    Linux  
    
  • $ cat F2
    START  
    Solaris  
    Aix  
    SCO  
    

Actual usage scenario

When I apply this technique to my actual use case,

awk '/Certificate Revocation List (CRL):/{x="F"++i;}{print > x}' test_cert.pem

does not extract the contents starting from Certificate Revocation List (CRL):

Instead it gives following error:

awk: cmd. line:1: (FILENAME=test_cert.pem FNR=1) fatal: expression for `>' redirection has null string value

I tried putting the pattern in quotes and all, but it does not work, not sure if the pattern is multiword how we extract the content.

The test_cert.pem looks as follows:


Certificate Revocation List (CRL):
        Version 2 (0x1)
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: C = XX, O = XXXXX, OU = 0003 374154744412350, CN = XXX
        Last Update: Aug 15 04:37:16 2021 GMT
        Next Update: Sep 23 03:47:16 2021 GMT
        CRL extensions:
            X509v3 CRL Number:
                209
            X509v3 Authority Key Identifier:
                keyid:09:DF:3B:15:GE:10:08:D5:86:8F:5B:E7:E6:36:B9:A1:A8:1A:83:18

Revoked Certificates:
    Serial Number: AAS60F19DABCDA8AGHIK3E4A59988AAFDA8E6
        Revocation Date: Jan 29 12:45:09 2021 GMT
    Serial Number: GGF0HHHABCDA8AGHIK3E4A599KKKAFDA8E6
        Revocation Date: Jul 25 4:32:24 2021 GMT
    Signature Algorithm: sha256WithRSAEncryption
         1e:cc:8e:9d:gv:ae:eb:0a:67:95:4b:8b:b6:5d:9e:bd:48:42:
         a5:25:e8:eb:b2:22:BV:42
-----BEGIN X509 CRL-----
MIIDLLKKARMCAQEwLLKKAOKONcNAQELBQYYUzvgfzELLLKKA1UEBhMCRlIxDzANBgNV
mZ7YI0YYUzvgrzYYUzvgz9Deb78UGbaedXkYYUzvgr5Hu1Zm16YYUzvgXo67IiNUI=
-----END X509 CRL-----
0

2 Answers 2

6

The problem in your case is two-fold.

The first problem is that your matching pattern contains characters that are special to regular expressions, in this case the ( ... ). You need to escape them in order for your program to actually find the match. Currently, your program doesn't find the match and therefore x is never initialized. That is the reason for the "redirection has null string value" error.

In addition, even if the regular expression were formulated correctly, it would fail for anything that comes before the first occurence of the Certificate Revocation List (CRL): string. So you need to

  1. correct your regular expression
  2. ensure that nothing is printed unless x is initialized.

Your could change your program to

awk '/Certificate Revocation List \(CRL\):/{x="F"++i;}{if (x) print > x}' test_cert.pem

and it will work again.

But this is again an example why you shouldn't use regular expression matches if you simply look for a fixed string. To harden your program against this kind of problem, use

awk '$0=="Certificate Revocation List (CRL):"{x="F"++i}{if (x) print >x}' test_cert.pem

instead.

0
4

The input file has an empty line at the top. When the awk code reads this, the x variable has no value, so when print >x is executed, it triggers the error.

This is due to the awk code expecting the regular expression to match on the very first line, so that x gets set to something.

You could fix this by initiating x to e.g. F0 in a BEGIN block:

awk 'BEGIN { x = "F0" } /...your RE.../ { x = "F" ++i }  { print >x }' file

This would have the effect of outputting all line before the first match of the regular expression to the file F0.

You may also want to discard anything read before x is set:

awk '/...your RE.../ { x = "F" ++i } x != "" { print >x }' file

Additionally, the regular expression needs to have its parentheses escaped, as these are special:

awk '/Certificate Revocation List \(CRL\):/ { x = "F" ++i } x != "" { print > x }' 
1
  • Yes, I understood the space issue afterwards, but still could not solve the problem bcos of parentheses, now its OK. Thanks
    – Cheppy
    Jul 28, 2021 at 9:38

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.