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
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"
CodePudding user response:
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
}'
CodePudding user response:
If your input is a single line, piping it into sed 's/\(.*\)/"\1"/'
should do the trick.
If your text spans multiple lines, this won’t do, but the following would work:
… | { printf \"; cat; echo \"; }
… however, this will preserve all newlines, which might also not be desirable. To suppress the very last newline, use the following instead:
… | { printf \"; sed '$s/$/"/'; }
Or, as a single sed
command:
… | sed '1s/^/"/; $s/$/"/'