Saturday, May 18, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 1413  / 3 Years ago, thu, december 2, 2021, 1:52:23

I want to customize a string with * characters, except the first and last 3 characters in the string, as I don't want to print this string in logs.



For example: if the string is abctextxyz, I want it changed to abc****xyz



I tried to print the string with * except the first and last 3 characters, but hard-coding it to a 7 letters string. I would like to customize it for an arbitrary length.



If the string is of 6 character or less, it should be printed as it is.



Here is a bash script that I have created for this purpose:



#!/bin/bash
STRING1="testabctest";
echo $STRING1;
STRING2=${STRING1:0:3};
echo $STRING2;
STRING3=${STRING1:(-3)};
echo $STRING3;
STRING4=$STRING2"****"$STRING3;
echo $STRING4

More From » command-line

 Answers
7

Using string manipulation to replace string with asterisks.



$ a="abctextxyz"; 
b="${a:3:$((${#a}-6))}";
echo "${a:0:3}${b//?/*}${a:(-3)}"

[#4057] Saturday, December 4, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tusmuumu

Total Points: 195
Total Questions: 122
Total Answers: 104

Location: Oman
Member since Tue, Feb 7, 2023
1 Year ago
;