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
foo=2xxx99xx
printf "%s%02d%s" ${foo:0:4} $(wc -l < file) ${foo:6}
tail -n 1 $FILES print for you?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"
sed, orawk, or many other programs. You might want to try yourself first, and tell us what you tried and how it went.wc -l < fileis preferable tocat file | wc -l.wc -l < $FILEFOO=2xxx03xx awk -v rec="${record_count}" ' { data = substr ($0, 5, 6); data=rec; print }' but nothing changes with FOO. Sorry I am new to this.2xxx99xxstored? In a variable? In a file? Piped from some other command? Do you want the original file or variable to be modified or do you just want to see the updated string in stdout? Please clarify.