字符串操作 - 替换某些字符

时间:2012-11-29 16:11:12

标签: linux bash sed

linux命令我输出了一个如下所示的文件列表:

    folder/folder/folder/file_1
    folder_1/folder/folder/file2

我想格式化此输出,以满足以下条件:

  • 最后一个斜杠(/)总是被三个下划线替换
  • 除最后一个斜杠之外的每个斜杠都替换为一个下划线
  • 文件夹名称中的任何下划线都替换为两个下划线
  • 文件名中的任何下划线都保持原样

上面的内容如下:

    folder_folder_folder___file_1
    folder__1_folder_folder___file2

如果您也可以解释一下有用的解决方案。谢谢!

3 个答案:

答案 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 ___