LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-23-2014, 07:55 PM   #1
theEditor876
LQ Newbie
 
Registered: Jun 2014
Posts: 4

Rep: Reputation: Disabled
Smile 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!
 
Old 06-24-2014, 02:58 AM   #2
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

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

Last edited by kooru; 06-24-2014 at 03:02 AM.
 
Old 06-24-2014, 06:35 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
As above, try with sed http://www.grymoire.com/Unix/Sed.html
 
Old 06-24-2014, 06:32 PM   #4
JeremyBoden
Member
 
Registered: Nov 2011
Posts: 939

Rep: Reputation: 174Reputation: 174
You can do quite a bit (tediously) with commands such as tr, cut, join etc...
 
Old 06-27-2014, 12:18 PM   #5
theEditor876
LQ Newbie
 
Registered: Jun 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-27-2014, 01:30 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
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

Last edited by sycamorex; 06-27-2014 at 01:35 PM.
 
Old 06-28-2014, 08:08 AM   #7
theEditor876
LQ Newbie
 
Registered: Jun 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
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!
 
Old 06-28-2014, 08:44 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,848

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
Quote:
Originally Posted by theEditor876 View Post
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.
 
Old 06-28-2014, 09:22 AM   #9
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Try the following:

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

Last edited by sycamorex; 06-28-2014 at 09:23 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using sed in bash script to serach and replace special characters, and text PoltoX Programming 6 08-14-2013 11:25 PM
[SOLVED] How to replace text using sed or awk in a file without changing the commented text coolpraz Programming 4 01-03-2013 07:19 AM
inserting text in a file at particular position azheruddin Linux - Newbie 11 12-03-2012 01:14 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-11-2008 12:13 AM
replace null characters in a file Philipp Programming 2 09-20-2001 03:29 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration