2013-11-14 24 views
0

我有一個像SED在一條線上

/test/something/test2/2013-10-12/file_123 
/test/123456/file_123 
/test/test2/test3/test4/something/2013/file_123 

我想將其更改爲以下格式在Linux中

/test/something/test2/????-??-??/file_123 
/test/??????/file_123 
/test/test2/test3/test4/something/????/file_123 

我能做到這一點的Linux文件中有特殊字符替換數除了最後一個字符串使用sed?

回答

3
kent$ awk -F/ -v OFS='/' '{for(i=1;i<NF;i++)if($i~/[0-9][0-9]/)gsub(/[0-9]/,"?",$i);}7' file 
/test/something/test2/????-??-??/file_123 
/test/??????/file_123 
/test/test2/test3/test4/something/????/file_123 
+0

的N-1部分這是我期待的...寫在.Thanks ... – disizjay

+0

我是新來的Linux ....請你解釋一下OFS,NF,NR的使用情況,您GSUB在上面的命令中使用....這將是偉大的.. – disizjay

0

如果點僅僅是刪除數據:

awk -F/ '{$(NF-1)="????"}1' OFS=/ file 
/test/something/test2/????/file_123 
/test/????/file_123 
/test/test2/test3/test4/something/????/file_123 
0
sed ": do 
s|[0-9]\([^/]*/[^/]*\)$|?\1| 
t do" YourFile 

更換任何數字路徑