Can someone please help me correctly frame the below sed command:
sed 's/.*/$(date +%Y%m%d)' filepattern
I have read a line of the file in filepattern variable which is like below:
P.182.MKT_DISC.*.2_1_1.*
I want to replace the last .* part with the current date in the format yyyymmdd. I have recently started using sed so please excuse me if I am making a basic mistake.
Mistake 1: you are asking sed to process a file named filepattern in the current directory.
You have to feed the standard input of sed with the variable value:
print -- "$filepattern" | sed ...
echo "$filepattern" | sed ...
Mistake 2: the s command expects a regular expression, not a raw string.
Therefore .* means any character repeated zero or more times, in other words any character sequence.
You have to escape the metacharacters and then use the metacharacter $ to anchor the expression to the end of the input string:
print -- "$filepattern" | sed 's/\.\*$/$(date +%Y%m%d)'