Reputation: 87
How can I replace two characters, 5th and 6th digit in the string below?
2xxx99xx
I want to replace 5th and 6th digit (which is 99) by getting the record count of the file.
$cat file | wc -l
3
The output must be:
2xxx03xx
Upvotes: 0
Views: 617
Reputation: 5298
With sed:
echo "2xxx99xx" | sed -r "s/(.{4})..(.*)/\1$(printf "%02d" `wc -l < ff`)\2/g"
first 4 characters form group 1, rest of the string except 5th and 6th characters form group 2. Then we put back the 1st group, formatted data, then the 2nd group.
If the sed version doesn't support extended regex, use below command:
echo "2xxx99xx" | sed "s/\(.\{4\}\)..\(.*\)/\1$(printf "%02d" `wc -l < ff`)\2/g"
Upvotes: 0
Reputation: 249113
foo=2xxx99xx
printf "%s%02d%s" ${foo:0:4} $(wc -l < file) ${foo:6}
Upvotes: 3