I want to read a file without whitespaces, filled with root certificates and seperate them into new files per match. I can select all matching certificates at once and insert to a single file, but I can't figure out, how to seperate per match and make print create a new file each cert.

awk '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/ { print > outputlist }' inputlist

I tried it already with a while read loop, but that didn't work.


Give a try to this:

awk '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/ { if ( $0 ~ /-----BEGIN CERTIFICATE-----/) { nb++ } ; print > "certificate" nb ".txt" ;}' inputlist

I always have this page open aside when writing awk script: Awk

Files generated:

$ ls certificate*
certificate1.txt  certificate2.txt
  • This Looks great! I going to check it tomorrow. Need to hurry home, but thanks Forward for your help! I will reply to tomorrow with results :) – Mickey Mazurek Mar 11 at 16:20
  • It worked! thx for that. :) this was the trickiest part. At the beginning the Cert had no ---begin & end---- ... had to include it with sed, but that took Ages for me to figure out how to. I googled a lot, but I found no good explenation, because I didn't know, was it a record seperator, or do i Need some other Options and variables... such a brainf**** ^^ – Mickey Mazurek Mar 12 at 8:17

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.