Hi All,
Was wondering how I can do the following....
I have a String as follows
"ACCTRL000005022RRWDKKEEDKDD...."
This string can be in a file called tail.out or in a Variable called $VAR2
Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of $VAR1 in the string $VAR2
Example:
VAR1=000004785
|
|
VAR2= "ACCTRL000005022RRWDKKEEDKDD...."
So it should look like...
VAR2= "ACCTRL000004785RRWDKKEEDKDD...."
I know this can be done by sed, but I need to know how to replace from the 7th character till the 15th character.
Any help would be appreaciated.
Thanks,
Some ways:
VAR1="000004785"
echo "$VAR2" | sed "s/\([^0-9]*\)[0-9]*\(.*\)/\1$VAR1\2/"
or:
VAR1="000004785"
echo "$VAR2" | sed "s/\(......\).........\(.*\)/\1$VAR1\2/"
1 Like
alister
3
One more (minor variation of Franklin52's second option):
sed "s/\(.\{6\}\).\{9\}/\1$VAR1/"
Regards,
Alister
1 Like
danmero
4
bash 4
# VAR1=000004785
# VAR2="ACCTRL000005022RRWDKKEEDKDD...."
# VAR2=${VAR2:0:6}${VAR1}${VAR2:15}
# echo $VAR2
ACCTRL000004785RRWDKKEEDKDD....
1 Like
Thanks all... it worked as expected.
rdcwayx
6
by awk substr function.
VAR1=000004785
VAR2="ACCTRL000005022RRWDKKEEDKDD...."
VAR3=$( echo $VAR2|awk -v s=$VAR1 '{sub(substr($0,7,9),s)}1' )