标题: 求助用sed或awk进行文件替换 [打印本页] 作者: legone2008 时间: 2014-04-16 16:47 标题: 求助用sed或awk进行文件替换 输入:
参数1: Insufficient free space in the %s directory. The free space is only %s. At least %s is required.
参数2: /export/home
参数3: 5%
参数4: 20%
输出: Insufficient free space in the /export/home directory. The free space is only 5%. At least 20% is required.
简单描述:
给出一个源串,如:Insufficient free space in the %s directory. The free space is only %s. At least %s is required.
把其中%s替换为指定的字符串,如:/export/home, 5%, %20
可以保证%s出现的个数与指定要替换的字符串个数相同, 但指定要替换的字符串个数是不定的,同时要替换的字符串内容也不定。
printf "Insufficient free space in the %s directory. The free space is only %s. At least %s is required." /export/home 5% 20%作者: huang6894 时间: 2014-04-16 17:04
=> awk -va='/export/home' -vb='5%' -vc='20%' 'BEGIN{print "Insufficient free space in the "a" directory. The free space is only "b". At least "c" is required."}'
Insufficient free space in the /export/home directory. The free space is only 5%. At least 20% is required.
./test.sh /export/home %50 %10
nsufficient free space in the /export/home directory. The free space is only %50. At least %10 is required.
这种方法好像可以实现,但是有点复杂,应该可以再优化下...