0

I have a private key file with some extra nonsense in there, and want just the text of the key.

so:

nonsense -----Begin Key-----
keep this1
keep this2 
keep this3
-----End Key----- nonsense

should become

-----Begin Key-----
keep this1
keep this2 
keep this3
-----End Key-----

EDIT: I don't want to just remove the actual word "nonsense." It could be anything in there before and after the key text.

1

How about

sed -e '/Begin Key/ s/^[^-]*//' -e '/End Key/ s/[^-]*$//'

Ex.

$ sed -e '/Begin Key/ s/^[^-]*//' -e '/End Key/ s/[^-]*$//' file
-----Begin Key-----
keep this1
keep this2 
keep this3
-----End Key-----
  • Works like a charm! Thank you kindly. – J. Tate May 22 at 1:34
0
awk '/-----(Begin Key|End Key)-----/{sub(/^[^\-]+|[^\-]+$/, "")}1' data
  • /-----(Begin Key|End Key)-----/ find begin or end key line
  • {sub(/^[^\-]+|[^\-]+$/, "")} clear leading and trailing nonsense.
  • 1 always print

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.