如何使用gsub拆分列。?
下面是我想将第二列拆分为两个的示例文件。
> cat seq.txt
Hello/World /app/oracle/data/app4/trail/hb000003
Hello/World /app/oracle/data/app1/trail/gg000028
Hello/World /app/oracle/data/app1/trail/kk000111
Hello/World /app/oracle/data/app1/trail/kk000111
Hello/World /app/oracle/data/app3/trail/mw000021
Hello/World /app/oracle/data/app2/trail/pj000009
Hello/World /app/oracle/data/app1/trail/fx000276
Hello/World /app/oracle/data/app1/trail/fx000006
Hello/World /app/oracle/data/app1/trail/fx000006
Hello/World /app/oracle/data/app1/trail/fx000276
Hello/World /app/oracle/data/app1/trail/gt000005
Hello/World /app/oracle/data/app4/trail/xh000001
尝试过这样的事情。但似乎gsub不会将正则表达式视为替代品。
> cat seq.txt | awk -F" " '{gsub(".*/..",".*/.. ",$2)}1'
Hello/World .*/.. 000003
Hello/World .*/.. 000028
Hello/World .*/.. 000111
Hello/World .*/.. 000111
Hello/World .*/.. 000021
Hello/World .*/.. 000009
Hello/World .*/.. 000276
Hello/World .*/.. 000006
Hello/World .*/.. 000006
Hello/World .*/.. 000276
Hello/World .*/.. 000005
Hello/World .*/.. 000001
以下是我的期望。
Hello/World /app/oracle/data/app4/trail/hb 000003
Hello/World /app/oracle/data/app1/trail/gg 000028
Hello/World /app/oracle/data/app1/trail/kk 000111
Hello/World /app/oracle/data/app1/trail/kk 000111
Hello/World /app/oracle/data/app3/trail/mw 000021
Hello/World /app/oracle/data/app2/trail/pj 000009
Hello/World /app/oracle/data/app1/trail/fx 000276
Hello/World /app/oracle/data/app1/trail/fx 000006
Hello/World /app/oracle/data/app1/trail/fx 000006
Hello/World /app/oracle/data/app1/trail/fx 000276
Hello/World /app/oracle/data/app1/trail/gt 000005
Hello/World /app/oracle/data/app4/trail/xh 000001
答案 0 :(得分:4)
当然,替换不会采用正则表达式。这没用。他们会匹配什么?你想要的是从模式中捕获。
替换中的字符&
表示匹配的整个文本是您想要的:
awk -F " " '{gsub(".*/..", "& ", $2)}7'
答案 1 :(得分:1)
使用gawk
' s gensub
$ awk '{$2=gensub(/(\/..)([^/]+)$/,"\\1 \\2",1,$2)}1' file
Hello/World /app/oracle/data/app4/trail/hb 000003
Hello/World /app/oracle/data/app1/trail/gg 000028
Hello/World /app/oracle/data/app1/trail/kk 000111
Hello/World /app/oracle/data/app1/trail/kk 000111
Hello/World /app/oracle/data/app3/trail/mw 000021
Hello/World /app/oracle/data/app2/trail/pj 000009
Hello/World /app/oracle/data/app1/trail/fx 000276
Hello/World /app/oracle/data/app1/trail/fx 000006
Hello/World /app/oracle/data/app1/trail/fx 000006
Hello/World /app/oracle/data/app1/trail/fx 000276
Hello/World /app/oracle/data/app1/trail/gt 000005
Hello/World /app/oracle/data/app4/trail/xh 000001
答案 2 :(得分:0)
awk '{sub(/000/," 000")}1' file
Hello/World /app/oracle/data/app4/trail/hb 000003
Hello/World /app/oracle/data/app1/trail/gg 000028
Hello/World /app/oracle/data/app1/trail/kk 000111
Hello/World /app/oracle/data/app1/trail/kk 000111
Hello/World /app/oracle/data/app3/trail/mw 000021
Hello/World /app/oracle/data/app2/trail/pj 000009
Hello/World /app/oracle/data/app1/trail/fx 000276
Hello/World /app/oracle/data/app1/trail/fx 000006
Hello/World /app/oracle/data/app1/trail/fx 000006
Hello/World /app/oracle/data/app1/trail/fx 000276
Hello/World /app/oracle/data/app1/trail/gt 000005
Hello/World /app/oracle/data/app4/trail/xh 000001