开发者社区> 问答> 正文

linux shell 统计一文件中某一字符串出现的次数

有个人这样写:

awk -v RS="@#$j" '{print gsub(/string/,"&")}' test.txt

我运行了下,也好像是对的,但是,我真心没看懂,尤其这些标红的地方,真心没理解其含义,求指教

展开
收起
a123456678 2016-06-14 15:32:23 3505 0
1 条回答
写回答
取消 提交回答
  • 
    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.
    2019-07-17 19:37:53
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 技术揭秘 立即下载
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载