1

I'm trying to split a file that contains multiple SSL certificates with AWK but is showing an error message: awk: too many output files 10

Command that I'm using is the following:

cat ${SSL_CERTIFICATES_PATH} | awk '/BEGIN/ { i++; } /BEGIN/, /END/ { print > i ".extracted.crt" }'

Error Message:

awk: too many output files 10
record number 735

Do you know how could I solve this issue?

jww
  • 97,681
  • 90
  • 411
  • 885
Javier Salas
  • 1,064
  • 5
  • 15
  • 38

2 Answers2

3

You have to close() file,

awk '/BEGIN/ {f=i++".extracted.crt"}/BEGIN/,/END/{print > f;if(/END/)close(f)}'

The Best solution as suggested by Ed Morton, one should not use range expressions, for more details Read Here

awk '/BEGIN/{f=(++i)".extracted.crt"} f{print>f} /END/{close(f);f=""}' 

Here is sample (not certificate)

Input

$ cat file
BEGIN
1
END

BEGIN
2
END

BEGIN
3
END

Execution

$ awk '/BEGIN/{f=i++".extracted.crt"}/BEGIN/,/END/{print > f;if(/END/)close(f)}' file

$ awk '/BEGIN/{f=(++i)".extracted.crt"} f{print>f} /END/{close(f);f=""}' file

Output files

$ ls *.crt
0.extracted.crt  1.extracted.crt  2.extracted.crt

File contents of each

$ for i in *.crt; do echo $i; cat $i; done
0.extracted.crt
BEGIN
1
END
1.extracted.crt
BEGIN
2
END
2.extracted.crt
BEGIN
3
END
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
  • looks like is not working, is displaying `awk: syntax error near line 1 awk: illegal statement near line 1` – Javier Salas Jul 24 '17 at 16:36
  • @JavierSalas which version of awk or else try `awk '/BEGIN/ {f=sprintf("%d.extracted.crt",i++)}/BEGIN/,/END/{print > f;if(/END/)close(f)}'` – Akshay Hegde Jul 24 '17 at 16:39
  • 1
    @JavierSalas that error message means you are using old, broken awk (/bin/awk on Solaris). Never use that awk. If you are on Solaris then use/install gawk and if that's not possible for some reason then use /usr/xpg4/bin/awk instead. – Ed Morton Jul 24 '17 at 16:51
  • 1
    But nawk has fewer features than /usr/xpg4/bin/awk and is further from being POSIX compliant and so isn't worth mentioning IMHO since xpg4 is available and superior to nawk. – Ed Morton Jul 24 '17 at 16:55
  • @JavierSalas instead of `awk` use `/usr/xpg4/bin/awk` and rest of the things – Akshay Hegde Jul 24 '17 at 17:00
  • @JavierSalas Then you are running the same tool - old, broken awk. Please read my earlier comment and just do what it says. – Ed Morton Jul 24 '17 at 17:00
  • 1
    @EdMorton I did it as you suggested ; – Akshay Hegde Jul 24 '17 at 17:14
1

We have to close the files each time variable i's value gets increases by 1, so try following and let me know if this helps you.

awk '/BEGIN/ {close(i".extracted.crt");i++} /BEGIN/, /END/ { print > i".extracted.crt" }' ${SSL_CERTIFICATES_PATH}

EDIT: Xavier, I have checked with a friend who has SUN 5 with him and following worked well without any error. You could put variable as per your need.

/usr/xpg4/bin/awk '/BEGIN/ {close(i".extracted.crt");i++} /BEGIN/, /END/ { print > i".extracted.crt" }' *.crt
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    I just tried with the command that you gave me and is still displaying the same message... – Javier Salas Jul 24 '17 at 16:36
  • try the edited one now and let me know if this helps you. I am sure it should work, because I changed the places of close and I variable now. – RavinderSingh13 Jul 24 '17 at 16:38
  • 1
    An unparenthesized expression on the right side of output redirection is undefined behavior, so your awk may be interpreting `print > i".extracted.crt"` as `(print > i)".extracted.crt"` (whatever that means!) instead of the presumably intended `print > (i".extracted.crt")`. Always parenthesize any expression on the right side of output redirection. – Ed Morton Jul 24 '17 at 16:57
  • Also I did with parentheses and still getting the same message `awk: too many output files 10` – Javier Salas Jul 24 '17 at 17:03
  • Have you stopped using old, broken awk? If so which awk are you now using? – Ed Morton Jul 24 '17 at 17:08
  • I'm trying to get the version of the AWK but is not working using `awk --version` – Javier Salas Jul 24 '17 at 17:19
  • @JavierSalas: Are you using csh or any old shell, could you please mention that also in your post. – RavinderSingh13 Jul 24 '17 at 17:36
  • is KSH using SunOS 5.10. – Javier Salas Jul 24 '17 at 17:56
  • @JavierSalas: I just tested my edited code in which I only made difference of changing awk to /usr/xpg4/bin/awk and it worked well, let me know if this helps you now? – RavinderSingh13 Jul 24 '17 at 19:10
  • Yesssss BINGO, glad that it helped you :), enjoy learning in here, keep posting and keep sharing. – RavinderSingh13 Jul 24 '17 at 19:34
  • 1
    @JavierSalas I told you to change to /usr/xpg4/bin/awk 3 hours earlier.... – Ed Morton Jul 24 '17 at 20:06
  • @EdMorton sorry, I didn't try your solution due I didn't see it, but I really appreciate all your answers and support! Thank you! – Javier Salas Jul 24 '17 at 22:55
  • 1
    It wasn't a solution it's the second comment under [Akshay's solution](https://stackoverflow.com/a/45285686/1745001). You're welcome. – Ed Morton Jul 25 '17 at 03:15