sed Help

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.

Will this do? awk no sed

echo "1234567890" | awk '{print substr($1,1,5) "KUMARJIT" substr($1,9,99)}'
12345KUMARJIT90

If you are on Linux, your ksh is most likely a kornshell 93. If so, this should work

$ str=1234567890
$ str=${str:0:5}KUMARJIT${str:8:2}
$ print $str
12345KUMARJIT90

---------- Post updated at 14:05 ---------- Previous update was at 14:03 ----------

Explanation:

${str:x:y} takes a substring out of str , where x ist the start index (starting at 0) and y is the length of the substring in characters.

Works also in bash

str=1234567890
str=${str:0:5}KUMARJIT${str:8:2}
echo $str
12345KUMARJIT90
$ echo 1234567890 | sed -n -e 's/\(.\{5\}\)...\(.*\)/\1KUMARJIT\2/p'
12345KUMARJIT90

How about Perl ?

 
echo '1234567890 should change to 12345KUMARJIT90' | perl -pe 'substr($_,5,3)="KUMARJIT";'

Just another lazy one

# echo 1234567890 | sed 's/^\(.....\).../\1KUMARJIT/'
12345KUMARJIT90

@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.

Thanks
Kumarjit.

Thanks again ctsgnb .:b:

But I fankly wasnt able to make out the meaning of the following line from quote.

\1 refers to what matched what was enclosed in the first \( \) 
\2 refers to what matched what was enclosed in the second \( \) 

Request you to explain in simple terms ( if possible)

Thanks
Kumarjit.

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 .

1 Like