Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to put text in quotes?

I have the following command which gives me value of Set-Cookie header:

curl --head http://www.stackoverflow.com | sed -n "/^Set-Cookie:/p" | cut -c 13-

Output:

prov=abed7528-7639-e2e3-39a0-361a6d3f7925; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need this output in quotes, like this:

"prov=abed7528-7639-e2e3-39a0-361a6d3f7925; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly"

>Solution :

Using printf

printf '"%s"\n' "$(curl ...)"

Command substitution strips any trailing newlines, so the ending quote will be on the same line.

However, there is a trailing carriage return (network traffic generally uses \r\n line endings). Add this to the end of the pipeline

| tr -d '\r'
# or
| sed 's/\r$//'

Collapsing the pipeline into one sed command:

curl -s --head http://www.stackoverflow.com | sed -En '/^Set-Cookie:/ {
    s/^.{12}/"/
    s/\r$/"/
    p
    q
}'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading