linux命令我输出了一个如下所示的文件列表:
folder/folder/folder/file_1
folder_1/folder/folder/file2
我想格式化此输出,以满足以下条件:
上面的内容如下:
folder_folder_folder___file_1
folder__1_folder_folder___file2
如果您也可以解释一下有用的解决方案。谢谢!
答案 0 :(得分:2)
# Assume each line in a variable called $value
# Split the values up into dirname and basename
val_dir="${value%/*}"
val_base="${value##*/}"
# Replace underscores in dirname with two underscores
val_dir="${val_dir//_/__}"
# Replace slashes in dirname with single underscore
val_dir="${val_dir//\//_}"
# Re-join dirname and basename with three underscores
result="${val_dir}___${val_base}"
答案 1 :(得分:1)
一个awk解决方案:
awk '
BEGIN {FS="/";OFS="_"}
{for (i=1; i<NF; i++) gsub("_","__",$i); $NF="__" $NF; print}
'
答案 2 :(得分:0)
这是你用sed做的一种方法(用GNU sed测试):
<infile rev | sed -r 'h; s,_,__,g; G; s,[^/]+/([^\n]+)\n([^/]+/).*,\2\1,; :a; s,/,_,2; ta; s,/,___,' | rev
输出:
folder_folder_folder___file_1
folder__1_folder_folder___file2
rev
使解析更容易,它所做的就是颠倒该行上字符的顺序。我将分解下面的sed脚本:
h; # save a copy of PS in HS, prepare to replace folder underscores
s,_,__,g; # replace folder underscores
G; # append HS to PS
s,[^/]+/([^\n]+)\n([^/]+/).*,\2\1,; # reorder into correct order
:a;
s,/,_,2; # replace most / with _, leave the first alone
ta;
s,/,___, # replace the first / with ___