If you have file with many \grk{}
sections (and others), probably the fastest way to achieve the goal is what @Jan suggested. @noob regex is fine for single \grk{}
.
The problem with (?<=grk\{)(.*?)(?=\})
is that you can't get fixed length lookbehind in most regex engines, so you can't ommit any text before " `". Take a look at this post.
You can also use bash script:
#!/bin/bash
file=$1
newFile=$file"_replaced"
val=`cat $file`
regex="\\\grk\{(.*?)\}"
cp $file $newFile
grep -oP $regex $file | while read -r line; do
replacement=`echo $line | sed -r 's/(\s)\`/\1XLFY/g'`
sed -i "s/$line/$replacement/g" $newFile
done
cat $newFile
which takes file as an argument and create file_replaced meeting your conditions.
EDIT: Run script for each file in directory:
for file in *; do ./replace.sh $file; done;
before that change the script, to it override existing file:
#!/bin/bash
file=$1
val=`cat $file`
regex="\\\grk\{(.*?)\}"
grep -oP $regex $file | while read -r line; do
replacement=`echo $line | sed -r 's/(\s)\`/\1XLFY/g'`
sed -i "s/$line/$replacement/g" $file
done
But if you don't use any VCS, please make a backup of your files!
EDIT2: debug
#!/bin/bash
file=$1
val=`cat $file`
echo '--- file ---'
echo $val
regex="\\\grk\{(.*?)\}"
echo 'regex: '$regex
grep -oP $regex $file | while read -r line; do
echo 'LINE: '$line
replacement=`echo $line | sed -r 's/(\s)\`/\1XLFY/g'`
echo 'REPLACEMENT: '$replacement
sed -i "s/$line/$replacement/g" $file
done
echo '--- file after ---'
cat $file