我有一个带文字的文件(每行1个字)。我需要用*
来审查单词中的所有字母,除了前五个字母。
实施例
Authority
- > Autho****
我不太清楚如何做到这一点。
答案 0 :(得分:4)
如果你很幸运,你需要的只是
sed 's/./*/6g' file
当我最初发布此内容时,我认为这是合理的便携性;但根据@ ghoti的评论,它不是。
答案 1 :(得分:3)
Perl救援:
perl -pe 'substr($_, 5) =~ s/./*/g' -- file
-p
逐行读取输入并在处理后打印每一行s/./*/g
用星号替换任何字符。 g
表示替换将尽可能多次发生,而不仅仅发生一次,因此所有字符都将被替换。在sed
的某些版本中,您可以通过在操作中附加数字来指定应该进行的替换:
sed -e 's/./*/g6'
这将从第6个位置开始替换所有(再次,因为g
)个字符。
答案 2 :(得分:2)
关注awk
可能对您有帮助。
解决方案1: awk
解决方案substr
和gensub
。
awk '{print substr($0,1,5) gensub(/./,"*","g",substr($0,6))}' Input_file
解决方案第二:
awk 'NF{len=length($0);if(len>5){i=6;while(i<=len){val=val?val "*":"*";i++};print substr($0,1,5) val};val=i=""}' Input_file
Autho****
编辑: 现在添加一种非单一形式的解决方案。现在也添加解释。
awk '
NF{ ##Checking if a line is NON-empty.
len=length($0); ##Taking length of the current line into a variable called len here.
if(len>5){ ##Checking if length of current line is greater than 5 as per OP request. If yes then do following.
i=6; ##creating variable named i whose value is 6 here.
while(i<=len){ ##staring a while loop here which runs from value of variable named i value to till the length of current line.
val=val?val "*":"*"; ##creating variable named val here whose value will be concatenated to its own value, it will add * to its value each time.
i++ ##incrementing variable named i value with 1 each time.
};
print substr($0,1,5) val##printing value of substring from 1st letter to 5th letter and then printing value of variable val here too.
};
val=i="" ##Nullifying values of variable val and i here too.
}
' Input_file ##Mentioning Input_file name here.
答案 3 :(得分:1)
就我个人而言,我只是使用sed(参见@triplee's answer),但如果你想在awk中这样做,那就是:
$ awk '{t=substr($0,1,5); gsub(/./,"*"); print t substr($0,6)}' file
Autho****
或使用GNU awk for gensub():
$ awk '{print substr($0,1,5) gensub(/./,"*","g",substr($0,6))}' file
Autho****
答案 4 :(得分:1)
这是sed
的便携式解决方案:
$ echo abcdefghi | sed -e 's/\(.\{5\}\)./\1*/;:x' -e 's/\*[a-z]/**/;t x'
abcde****
以下是它的工作原理:
's/\(.\{5\}\)./\1*/'
- 保留前五个字符,用星号替换第六个字符。':x'
- 设置一个“标签”,我们可以将其分支回来。's/\*[a-z]/**/ - '
- 用带星号的星号后面的字母替换。't x'
- 如果最后一次替换成功,请跳回标签“x”。这在GNU和BSD sed中同样有效。
当然,调整正则表达式以适应。
答案 5 :(得分:1)
这是一个非常简单的sed解决方案(不需要GNUsed):
main.twig has block content with include 'card.twig'
card.twig realise block content with block secondContent include 'rec.twig'
rec.twig realise block secondContent
答案 6 :(得分:0)
使用sed
:
sed 's/./\*/6;:loop;s/\*[^\*]/\**/;/\*[^\*]/b loop' file_to_censor.txt
<强>输出:强>
<强>解释强>
s/./\*/6 #replace the 6th character of the chain by *
:loop #define an label for the goto
s/\*[^\*]/\**/ #replace * followed by non * char by **
/\*[^\*]/b loop #then loop until it does not exist a * followed by a non * char