For example, given:$ cat test001.txtthisis a test bucketbucket line not this one.  bucket lein endy    bucket and others endyanda ttestHow can I change the lines that start with bucket with (bucket, ignoring spaces? So far I have:sed -i 's/^\sbucket/bucket(/g' test001.txt    but that doesn't work (no change and no error). Then I would like to only do it for lines that also end with 'endy' (so only 2 of the 3 bucket lines).
sed - change lines that start with one given word and end with another?
sed
Here's another approach:sed -e '/^[[:space:]]*bucket.*endy$/s/bucket/(bucket/' file.txtThe initial /pattern/ tells sed to only work on lines containing that pattern, and the rest of it is a simple, standard substitution.