How to replace all middle characters with "*"?

I would like to replace the middle of the word with ****.

For instance:

ifbewofiwfib
wofhwifwbif
iwjfhwi
owfhewifewifewiwei
fejnwfu
fehiw
wfebnueiwbfiefi

It should become:

if********ib
wo*******if
iw***wi
ow**************ei
fe***fu
fe*iw
wf***********fi

So far I have managed to replace everything except the first two characters:

sed -e 's/./*/g3' 

Or follow the long path:

grep -o '^..' file > start
cat file | sed 's:^..\(.*\)..$:\1:' | awk -F. '{for (i=1;i<=length($1);i++) a=a"*";$1=a;a=""}1' > stars
grep -o '..$' file > end
paste -d "" start stars > temp
paste -d "" temp end > final
+4
source share
4 answers

I would use Awk for this if you have GNU Awk to set the field separator to an empty string ( How to set the field separator to an empty string? ).

Thus, you can scroll through the characters and replace the desired "*". In this case, replace from third to third:

$ awk 'BEGIN{FS=OFS=""}{for (i=3; i<=NF-2; i++) $i="*"} 1' file
if********ib
wo*******if
iw***wi
ow**************ei
fe***fu
fe*iw
wf***********fi
+4
source

If perlok:

$ perl -pe 's/..\K.*(?=..)/"*" x length($&)/e' ip.txt
if********ib
wo*******if
iw***wi
ow**************ei
fe***fu
fe*iw
wf***********fi
  • ..\K.*(?=..) to match characters other than the first / last two characters
    • . .
  • e Perl
  • "*" x length($&)
+3

, :

sed -E ':a; s/^(..)([*]*)[^*](.*..)$/\1\2*\3/; ta'

, , , , :a; ...; ta. 3 :

  • (..) .
  • ([*]*) .
  • [^*] , .
  • (.*..) .

GNU sed

, , , :

h                                 # save a copy to hold space
s/./*/g3                          # replace all but 2 by *
G                                 # append hold space to pattern space
s/^(..)([*]*)..\n.*(..)$/\1\2\3/  # reformat pattern space

:

sed -Ef parse.sed input.txt

if********ib
wo*******if
iw***wi
ow**************ei
fe***fu
fe*iw
wf***********fi
+2
source

The following awkmay help you with this. It should work in all versions awk.

awk '{len=length($0);for(i=3;i<=(len-2);i++){val=val "*"};print substr($0,1,2) val substr($0,len-1);val=""}'  Input_file

Now add another Liner form.

awk '
{
  len=length($0);
  for(i=3;i<=(len-2);i++){
    val=val "*"};
  print substr($0,1,2) val substr($0,len-1);
  val=""
}
'  Input_file

Explanation: Adding an explanation now for the above code.

awk '
{
  len=length($0);                           ##Creating variable named len whose value is length of current line.
  for(i=3;i<=(len-2);i++){                  ##Starting for loop which starts from i=3 too till len-2 value and doing following:
    val=val "*"};                           ##Creating a variable val whose value is concatenating the value of it within itself.
  print substr($0,1,2) val substr($0,len-1);##Printing substring first 2 chars and variable val and then last 2 chars of the current line.
  val=""                                    ##Nullifying the variable val here, so that old values should be nullified for this variable.
}
' Input_file                                ##Mentioning the Input_file name here.
+1
source

Source: https://habr.com/ru/post/1694909/


All Articles