I am able to combine all lines into one using the following

cat input.txt | tr -d '\n' > output.txt

But as you can see below my data is separated by tabs and double commars.

mail._domainkey IN  TXT ( "v=DKIM1; k=rsa; t=s; s=email; "
    "qqmGCYGKk5ImzSIoa40L1gpcSPtIWbsVTSXK1FLsovGHd7NMmEHBgv2WabpDL4BQeEfSZC0Rk5sZEaWrepiHBiTcNjhb5tyLkgxmUIW21sOA+2kmChQUwyp10qDaP5h/+s7R0arQIDAQAB" )  ; ----- DKIM key mail for domain.comtest

How am I able to get the following output?

mail._domainkey IN  TXT ( "v=DKIM1; k=rsa; t=s; s=email; p=MBIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AEIIBCgKCAQEAxZfSF9WkStkz1A0tbSNWAEdyHiWfenEeihvzVn8mCOx7nsUBYnEDocbDb8P0pTHS8MVNZ4jhXSwkg5mc6+NVgyZIctSogUMdD1ddGsYDfUHgT/d5tsVTzecgRtTtpdxibRyh1HY5Zmg2wxFLuCPqMPVeJHjwfKwwQHHCQtIVH+b+QvmF6hxODu73UwNUaG1cr8UkLqoQexTGoSqqmGCYGKk5ImzSIoa40L1gpcSPtIWbsVTSXK1FLsovGHd7NMmEHBgv2WabpDL4BQeEfSZC0Rk5sZEaWrepiHBiTcNjhb5tyLkgxmUIW21sOA+2kmChQUwyp10qDaP5h/+s7R0arQIDAQAB" )  ; ----- DKIM key mail for domain.com

cat input.txt | tr -d '\n\r\t\"' > output.txt
This should work fine.


Assuming that:

  • there are tabs at the start of some lines (not spaces).
  • what you mean to say is: "Remove double quotes, (some) spaces and tabs form the opening parenthesis to the closing parentheses"
  • that you want to keep some spaces for readability
  • and that you want to reintroduce double quotes surrounding the text inside the parenthesis

This bash code will do it:


str='mail._domaikey IN  TXT ( "v=DKIM1; k=rsa; t=s; s=email; "
    "qqmGCYGKk5ImzSIoa40L1gpcSPtIWbsVTSXK1FLsovGHd7NMmEHBgv2WabpDL4BQeEfSZC0Rk5sZEaWrepiHBiTcNjhb5tyLkgxmUIW21sOA+2kmChQUwyp10qDaP5h/+s7R0arQIDAQAB" )  ; ----- DKIM key mail for domain.com'

reg='(^[^(]*\()([^)]*)(\).*)$'                       # split the text into parts
[[ "$str" =~ $reg ]] && a=("${BASH_REMATCH[@]:1}")   # capture in var `a` only the
                                                     # regex capture parentheses.

a[1]="${a[1]//[\"$' \n\t']}"                         # remove double quotes (")
                                                     # space, tab and newlines

a[1]="${a[1]//;/; }"                                 # add some spaces back.

printf '%s "%s" %s\n' "${a[@]}"                      # print parts formatted.

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.