I need to change 's3_bucket': 'pass' to hello but it is not working
ex:
echo "'s3_bucket': 'pass'" | sed 's/'s3_bucket':/hello/'
outputs:
's3_bucket': 'pass'
Please help understand what am I doing wrong
The quoting is wrong. The part 's/'s3_bucket':/hello/'
is interpreted by the shell as 's/'
followed by s3_bucket
and ':/hello/'
, resulting in s/s3_bucket:/hello/
as an argument for sed
.
Try
echo "'s3_bucket': 'pass'" | sed "s/'s3_bucket':/hello/"
or
echo "'s3_bucket': 'pass'" | sed 's/'\''s3_bucket'\'':/hello/'
Note that in the second version there are two adjacent single quotes in '\''
, not a double quote.
There are more possible ways of quoting, see this answer to a similar question.
echo
, or are you working with a text file? Is that text file in JSON, YAML, or HCL format? Modifying a document withsed
when that document format requires special encoding of data is not really encouraged. There are special tools for working with that sort of data. Also, are you actually querying the document with a password, or do you just want to modify all instances of thes3_bucket
key? – Kusalananda Jan 31 '22 at 15:38