LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Replace characters in a text file at a certain position (https://www.linuxquestions.org/questions/linux-newbie-8/replace-characters-in-a-text-file-at-a-certain-position-4175508949/)

theEditor876 06-23-2014 06:55 PM

Replace characters in a text file at a certain position
 
Hey guys!

i have a text file and want to write a textstring at, let's say, line 5 at position 4, and REPLACE the old text at that position with the new text-
so if line 5 looked like this:
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Text to insert:
hello

pos 4:
ABCD>EFGHIJKLMNOPQRSTUVWXYZ

new line 5:
ABCDhelloJKLMNOPQRSTUVWXYZ

Can somebody tell me how to do this in bash, please?

Thank you!

kooru 06-24-2014 01:58 AM

Welcome to LQ!
Is this a homework? :)
You could try with sed.

chrism01 06-24-2014 05:35 AM

As above, try with sed http://www.grymoire.com/Unix/Sed.html

JeremyBoden 06-24-2014 05:32 PM

You can do quite a bit (tediously) with commands such as tr, cut, join etc...

theEditor876 06-27-2014 11:18 AM

thanks everybody, couldn't get it to work the way i wanted to with sed, so i wrote a c program instead, using fopen etc. :)

sycamorex 06-27-2014 12:30 PM

I don't think there's a need to reinvent the wheel. As others said it can be done with sed

I'll post it as a little puzzle. Some basic sed knowledge is enough to solve it.


Code:

sed '***********' file
The string below needs to be reordered to replace the * bit in the sed command.

Code:

hello/4.s//5
Example:

Code:

sycamorex@mainframe:~/data/tmp$ cat file
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ

sycamorex@mainframe:~/data/tmp$ sed '*********' file
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDhelloFGHIJKLMNOPQRSTUVWXYZ


theEditor876 06-28-2014 07:08 AM

thank you, sycamorex

sed 4s/./hello/5 file

so this would work to replace the 5th letter in line 4, but literally only one letter is replaced
from:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
to:
ABCDhelloFGHIJKLMNOPQRSTUVWXYZ
instead of:
ABCDhelloJKLMNOPQRSTUVWXYZ

can you point me in the right direction to replace not only one letter and then insert the string, but to delete as many characters from the input file at that position as stringlength of "hello" is?

thx a ton!

syg00 06-28-2014 07:44 AM

Quote:

Originally Posted by theEditor876 (Post 5195358)
so this would work to replace the 5th letter in line 4, but literally only one letter is replaced

Strictly speaking, the fifth occurrence of the pattern is replaced. Not merely symantic pedantry when more than one character is matched.
Quote:

can you point me in the right direction to replace not only one letter and then insert the string, but to delete as many characters from the input file at that position as stringlength of "hello" is?
Read the grymoire link above. Yes, all of it. You need to specify the length yourself.

sycamorex 06-28-2014 08:22 AM

Try the following:

Code:

sed '4s/^\(.\{4\}\)\(.\{5\}\)\(.*\)/\1hello\3/' file
The grymoire link will provide you with the knowledge to understand the above.


All times are GMT -5. The time now is 10:10 PM.