Find/replace alpha characters in string

Hi,
I would like to find a 3-letter character series in a string/variable and replace it with x's.

An example set of strings is:

563MS333_101_afp_400-100_screening
563MS333_104-525_rjk_525_screening
563MS333_110_dlj_500-100_w24
563MS333_888-100_mmm_424_screening

The only constants are: 565MS333 and the time points at the end of each string (so 'screening', 'w24', 'w48', 'w96'). The rest can be variable.

I am trying to replace the 3-alpha characters in between the 3-digit numbers with x's (so, *_xxx_*). The 3 alpha characters are usually between underscores, but not always.

Thanks for the help/input!

This sed may help:

$ sed -r "s/([0-9]{1,})(_{,1})[a-zA-Z]{3}(_{,1})([0-9]{1,})/\1\2xxx\3\4/g" infile                   
563MS333_101_xxx_400-100_screening
563MS333_104-525_xxx_525_screening
563MS333_110_xxx_500-100_w24
563MS333_888-100_xxx_424_screening

That is a really nice solution, but it applies to an input text file. In my script, each of those lines is a variable in a loop. I do this so I can place the output of the sed command as a new variable. So for example:

cat dirlist | while read line
do
name="563MS663_100-101_afp_200_screening" (this value is actually derived using another function, I don't actually hard code it into $name like this)
newname="sed function on $name to replace alpha chars with x's"
done

Can you breakdown the sed command for me so I can try to adapt it?

Many thanks!

This certainly is not bulletproof, but might point you in a direction to pursue:

echo ${name/[a-z][a-z][a-z]/xxx}
563MS663_100-101_xxx_200_screening

This is a good start to something, thank you for that!

The sed command Klashxx gave you will work on some systems, but the -r option is not portable (and you haven't said what OS you're using).

For systems with a POSIX-conforming sed utility, the following sed script seems to do what you want:

strings='563MS333_101_afp_400-100_screening leading and trailing _
563MS333_104-525_rjk_525_screening leading and trailing _
563MS333_110_dlj_500-100_w24 leading and trailing _
563MS333_888-100_mmm_424_screening leading and trailing _
563MS333_101afp_400-100_screening trailing _
563MS333_104-525rjk_525_screening trailing _
563MS333_110dlj_500-100_w24 trailing _
563MS333_888-100mmm424_screening trailing _
563MS333_101afp400-100_screening no _
563MS333_104-525rjk525_screening no _
563MS333_110dlj500-100_w24 no _
563MS333_888-100mmm424_screening no _
563MS333_101_afp400-100_screening leading _
563MS333_104-525_rjk525_screening leading _
563MS333_110_dlj500-100_w24 leading _
563MS333_888-100_mmm424_screening leading _
563MS333_101_fp_400-100_screening No Change too few alphas
563MS333_101fp_400-100_screening No Change too few alphas
563MS333_101_fp400-100_screening No Change too few alphas
563MS333_101fp400-100_screening No Change too few alphas
563MS333_101_aafp_400-100_screening No Change too many alphas
563MS333_101aafp_400-100_screening No Change too many alphas
563MS333_101_aafp400-100_screening No Change too many alphas
563MS333_101aaafp400-100_screening No Change too many alphas'
printf '%s\n' "$strings" | while read line
do	printf 'Input line: %s\n' "$line"
	nline="$(printf '%s\n' "$line" |
		sed 's/^\(563MS333[-_[:digit:]]*[[:digit:]]\{3\}_\{0,1\}\)[[:alpha:]]\{3\}\(_\{0,1\}[[:digit:]]\{3\}\)/\1xxx\2/')"
	printf '  New line: %s\n' "$nline"
done

and produces the output:

Input line: 563MS333_101_afp_400-100_screening leading and trailing _
  New line: 563MS333_101_xxx_400-100_screening leading and trailing _
Input line: 563MS333_104-525_rjk_525_screening leading and trailing _
  New line: 563MS333_104-525_xxx_525_screening leading and trailing _
Input line: 563MS333_110_dlj_500-100_w24 leading and trailing _
  New line: 563MS333_110_xxx_500-100_w24 leading and trailing _
Input line: 563MS333_888-100_mmm_424_screening leading and trailing _
  New line: 563MS333_888-100_xxx_424_screening leading and trailing _
Input line: 563MS333_101afp_400-100_screening trailing _
  New line: 563MS333_101xxx_400-100_screening trailing _
Input line: 563MS333_104-525rjk_525_screening trailing _
  New line: 563MS333_104-525xxx_525_screening trailing _
Input line: 563MS333_110dlj_500-100_w24 trailing _
  New line: 563MS333_110xxx_500-100_w24 trailing _
Input line: 563MS333_888-100mmm424_screening trailing _
  New line: 563MS333_888-100xxx424_screening trailing _
Input line: 563MS333_101afp400-100_screening no _
  New line: 563MS333_101xxx400-100_screening no _
Input line: 563MS333_104-525rjk525_screening no _
  New line: 563MS333_104-525xxx525_screening no _
Input line: 563MS333_110dlj500-100_w24 no _
  New line: 563MS333_110xxx500-100_w24 no _
Input line: 563MS333_888-100mmm424_screening no _
  New line: 563MS333_888-100xxx424_screening no _
Input line: 563MS333_101_afp400-100_screening leading _
  New line: 563MS333_101_xxx400-100_screening leading _
Input line: 563MS333_104-525_rjk525_screening leading _
  New line: 563MS333_104-525_xxx525_screening leading _
Input line: 563MS333_110_dlj500-100_w24 leading _
  New line: 563MS333_110_xxx500-100_w24 leading _
Input line: 563MS333_888-100_mmm424_screening leading _
  New line: 563MS333_888-100_xxx424_screening leading _
Input line: 563MS333_101_fp_400-100_screening No Change too few alphas
  New line: 563MS333_101_fp_400-100_screening No Change too few alphas
Input line: 563MS333_101fp_400-100_screening No Change too few alphas
  New line: 563MS333_101fp_400-100_screening No Change too few alphas
Input line: 563MS333_101_fp400-100_screening No Change too few alphas
  New line: 563MS333_101_fp400-100_screening No Change too few alphas
Input line: 563MS333_101fp400-100_screening No Change too few alphas
  New line: 563MS333_101fp400-100_screening No Change too few alphas
Input line: 563MS333_101_aafp_400-100_screening No Change too many alphas
  New line: 563MS333_101_aafp_400-100_screening No Change too many alphas
Input line: 563MS333_101aafp_400-100_screening No Change too many alphas
  New line: 563MS333_101aafp_400-100_screening No Change too many alphas
Input line: 563MS333_101_aafp400-100_screening No Change too many alphas
  New line: 563MS333_101_aafp400-100_screening No Change too many alphas
Input line: 563MS333_101aaafp400-100_screening No Change too many alphas
  New line: 563MS333_101aaafp400-100_screening No Change too many alphas