position specific replace in file

How to replace the position specific values in the file..
i searched a lot the forums but i couldn't able to do...
i have file like below

576666666666666666666666666 7878 897987 121 0asdas Y12
5900fbb 777 09JJJ 78798347892374 234234234364 234232898
89HJHIHIGIUG989902743748327khjkhkjlh jggfsgf9898 90909090

1)In the first line i want to change from 18 position - 25 position to some other value- alpha numeric
2)if the line startd with 5900 then i should change 5 - 17 to some text

Simiar to other lines also ...with different positions

Need help on this

sed '1s/\(^.\{17\}\).\{8\}\(.*\)/\1some123text\2/; /^5900/s/\(^.\{4\}\).\{13\}\(.*\)/\1some123text\2/' inputfile
1 Like

It worked thx...
Can you explain me the command

---------- Post updated at 01:55 AM ---------- Previous update was at 01:46 AM ----------

I have one more query in second part of sed

sed '/^5900/s/\(^.\{4\}\).\{13\}\(.*\)/\1some123text\2/' inputfile

How do i can insert another condition into the above command
Example:
in the same line i have to check wther the 30th position is R or S or V
i mean below is the condition

1)The should starts with 5900
2)The 30 position is R or S or V .. then i have to change to some text

If your concerned with the starting position then below would replace at 5th and 18th position in a line.

sed '/^5900/s/./TEXT/5;s/./TEXT2/18' inputfile
1s/\(^.\{17\}\).\{8\}\(.*\)/\1some123text\2/

--> '1s' refers to substitution only in the first line. First 17 characters (matched by pattern in green) are stored in buffer, referenced by \1. The next 8 characters (i.e. from char 18 through to 25, pattern in blue) are matched. Rest of the line (pattern in red) is stored in buffer, reference by \2. This whole thing is substituted by "\1some123text\2".

 /^5900/s/\(^.\{4\}\).\{13\}\(.*\)/\1some123text\2/

--> Pattern in green matches line starting with 5900. In such lines, first 4 characters (pattern in blue) are stored in buffer, referenced by \1. The next 13 characters (i.e. from char 5 through to 17, pattern in red) are matched. Rest of the line (pattern in pink) is stored in buffer, referenced by \2. This whole thing is substituted by "\1some123text\2".

1 Like

Thx for ur explanation...

can i insert another condition inthe same command...

the line should starts with 5900 and 30th position is R or S or V

The second part of sed can be extended to this:

/^5900/s/\(^.\{4\}\).\{13\}\(.\{12\}\)[RVS]\(.*\)/\1some123text\2X\3/

This will substitute characters from position 5-17 with "some123text" and substitute character 30 to "X", if it is R or V or S