Home > other >  sed: Replace string at a given position in a textfile
sed: Replace string at a given position in a textfile

Time:09-16

I am trying to search and replace value within a huge file (40 millions) for the substring(30 characters) that starts at position 235 in a file called testfile.txt. I want to mask the substring with ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

I am using the following command

sed -r 's/(.{235}).{30}(.*)/\1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/g' testfile.txt > anothertestfile.txt

When I look at the output, there are some lines that the string was not replaced.

CodePudding user response:

I believe this will work:

cut -c1-235,266- --output-delimiter=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

or

cut -c1-235,266- --output-delimiter=$(perl -e 'print "X" x 30')

This is using cut to print the char range 1-235,266-(eol), and delimiting those two ranges with 30 X's.

The second is using perl's x operator to repeat a char 30 times to make the delimiter string

CodePudding user response:

Using sed

$ sed -E "s/(.{235}).{30}/\1$(printf 'X%.0s' {1..30})/g;wanothertestfile.txt" testfile.txt

CodePudding user response:

just for the 'X'-repeating part

jot -s '' -b 'X' 30
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
nawk '$-_=substr(_="X",gsub(__="",_,_)*gsub(__,_,_),__)_ _' <<<'' 
gawk 'NF = ($_)^_^($_=OFS=$NF)' <<< '30 X' 
mawk 'NF = --$NF ($NF=OFS=$!_)' <<< '30 X'
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  • Related