有个人这样写:
awk -v RS="@#$j" '{print gsub(/string/,"&")}' test.txt
我运行了下,也好像是对的,但是,我真心没看懂,尤其这些标红的地方,真心没理解其含义,求指教
0
总编总编 2年前
sunshine@debian :~$ cat test.txt
hello,let us have a try,now,begin,
sunshine@debian :~$ j=","
sunshine@debian :~$ echo $j
,
sunshine@debian :~$ awk -v RS="@#$j" '{print gsub(/,/,"&")}' test.txt
4
sunshine@debian :~$
看懂了吧,-v参数 引用外部变量j,gsub返回指定字符被替换的次数
给你贴个awk的man:
gsub(r,s,t) gsub(r,s)
Global substitution, every match of regular expression r in variable t is replaced by string s. The number of replacements is
returned. If t is omitted, $0 is used. An & in the replacement string s is replaced by the matched substring of t. \& and \\ put
literal & and \, respectively, in the replacement string.
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。