You have a file data.txt which has a field YEAR_MONTH:
YEAR_MONTH="201211"
And you wish to change the value to reflect the current year and month. You could use sed to automate this process with a one-line sed program:
sed -e "s/YEAR_MONTH=\"20[0-9][0-9]\(\(0[1-9]\)\|\(1[0-2]\)\)\"/YEAR_MONTH=\"`date +%Y%m`\"/g"
Note:
1. You need to use double-quotes "..." to encapsulate the entire sed program string so that the `date` command will be executed by the shell before it passes the sed program string to sed.
2. You will need to escape the inner double-quotes and the parentheses ( ) with backslashes to avoid them being interpreted by the shell.
3. 20[0-9][0-9]((0[1-9])|(1[0-2])) means all values from 200001 to 209912
No comments:
Post a Comment