I have struggled with this problem before, but I discovered the cleanest way is to use the builtin printf
printf "$(cat file.txt)" | less
Here is a real world example dealing with aws iam embeded json policy in the output, the file file.txt contains:
{
"registryId": "111122223333",
"repositoryName": "awesome-repo",
"policyText": "{\n \"Version\" : \"2008-10-17\",\n \"Statement\" : [ {\n \"Sid\" : \"AllowPushPull\",\n \"Effect\" : \"Allow\",\n \"Principal\" : {\n \"AWS\" : [ \"arn:aws:iam::444455556666:root\", \"arn:aws:iam::444455556666:user/johndoe\" ]\n },\n \"Action\" : [ \"ecr:BatchCheckLayerAvailability\", \"ecr:BatchGetImage\", \"ecr:CompleteLayerUpload\", \"ecr:DescribeImages\", \"ecr:DescribeRepositories\", \"ecr:GetDownloadUrlForLayer\", \"ecr:InitiateLayerUpload\", \"ecr:PutImage\", \"ecr:UploadLayerPart\" ]\n } ]\n}"
}
after applying the above (without the less) you get:
{
"registryId": "111122223333",
"repositoryName": "awesome-repo",
"policyText": "{
"Version" : "2008-10-17",
"Statement" : [ {
"Sid" : "AllowPushPull",
"Effect" : "Allow",
"Principal" : {
"AWS" : [ "arn:aws:iam::444455556666:root", "arn:aws:iam::444455556666:user/johndoe" ]
},
"Action" : [ "ecr:BatchCheckLayerAvailability", "ecr:BatchGetImage", "ecr:CompleteLayerUpload", "ecr:DescribeImages", "ecr:DescribeRepositories", "ecr:GetDownloadUrlForLayer", "ecr:InitiateLayerUpload", "ecr:PutImage", "ecr:UploadLayerPart" ]
} ]
}"
}
Note that the value for "policyText" is itself a string containing json.