I have a string 1234567890
My requirement is to replace the characters between position 6 and 8 (both positions inclusive)
with a string KUMARJIT
i.e.,
1234567890 should change to 12345KUMARJIT90
I am using korn shell and linux 2.6x OS , and I want to do it using sed.
I made it till this far :
echo 1234567890 | sed -n -e 's/^\(.\{6\}\)/KUMARJIT'
only to replace the first 6 characters with KUMARJIT
but thats not what I need .
I looked accross numerous sed examples online , many of them had hiflying regexp written , but none bothered to take the pain
and explain what the code actually does .
Its an earnest request , please guide me through you posts in case you are replying .
I am still a Unix novice.
Thanks:o
Kumarjit.
@anbu: As I see you are pretty conversant with the sed usage, I must let you know that I wouldnt have posted this question at all if I knew the way to write the sed code for my requirement , for which , I clearly quoted in my post requesting you to EXPLAIN what your POST is actually doing at the background. But somehow YOU MIGHT HAVE MISSED THAT POINT.
The positions 6 and 8 are just mere examples , but in real-time , I would be dealing with a file with volume in Gigs , for which I need to REPLACE ALL
CHARACTERS BETWEEN 103 and 108 POSITIONS (BOTH POSITIONS INCLUSIVE) OF EACH LINE WITH A STATIC STRING 00000000000000.
Here is an exemple that - i hope - will help you to get the \( \) stuff
# echo "ABC" | sed 's/\(..\)\(.\)/\2-\1/'
C-AB
In fact it's like prenthesis ( ) but for them to be interpreted they need to be escaped with backslash
otherwise they are taken as litteral and not as an operator to delimit a matching string.
BTW Unix sed wants the ^ anchor outside the \( \):
echo 1234567890 | sed 's/\(^.....\).../\1KUMARJIT/'
1234567890
echo 1234567890 | sed 's/^\(.....\).../\1KUMARJIT/'
12345KUMARJIT90
and is IMHO better readable, too.
For the last requirement, it is
sed 's/^\(.\{102\}\).\{6\}/\100000000000000/'
.\{6\} is the same as ...... i.e. 6 times any character.
The \( \) marks the first 102 characters, followed by the next 6 characters. Trailing characters are not touched.
The entire match is substituted by \1 ie. the match of the first \( \) , followed by 00000000000000 .