LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 09-01-2020, 09:02 AM   #1
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Rep: Reputation: Disabled
Sed to replace range of text at specific coordinates


Hello everyone,

I wish to edit a document, replacing a range of characters with another range of text.
For example the following input has to be modified between character 3 and 7 with "1234".

Input:
Quote:
abcdefghijklmnopqrstuvwxyz
Output:
Quote:
ab1234hijklmnopqrstuvwxyz
I was thinking about using the command sed to do so, but it seems it only replace lines by ranges addresses, and not characters. https://www.gnu.org/software/sed/man...ange-Addresses

Would anyone know how to do the substitution?
 
Old 09-01-2020, 09:15 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Drosera_capensis View Post
For example the following input has to be modified between character 3 and 7 with "1234".
If the above is the precise description, this is a solution:
Code:
sed 's/^\(..\)...../\11234'
EDIT:
This should also work. Didn't try.
Code:
sed 's/^\(..\).\{5\}/\11234'

Last edited by berndbausch; 09-01-2020 at 09:19 AM.
 
1 members found this post helpful.
Old 09-01-2020, 09:26 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,600

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

This can be done directly in the shell with parameter expansion...

Code:
$ myvar='abcdefghijklmnopqrstuvwxyz'
$ echo "${myvar:0:2}1234${myvar:7}"
abc1234hijklmnopqrstuvwxyz
 
2 members found this post helpful.
Old 09-01-2020, 09:32 AM   #4
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Original Poster
Rep: Reputation: Disabled
Thank you berndbausch, the two commands are indeed working as following:
Quote:
sed 's/^\(..\)...../\11234/'
sed 's/^\(..\).\{5\}/\11234/'
However, I am going to use these commands on a more complex script with variables ($s for starting position, $e for ending position. So the range would be $s-$e).
Would you know how replace the dots "." on the commands above?
 
Old 09-01-2020, 09:38 AM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Something like
Code:
s1=$((s-1))
diff=$((e-s))
sed "s/^\(.\{$s1\}\).\{$diff\}/\11234"
This becomes increasingly unreadable. Perhaps variable substitution works with non-constant positions?
 
1 members found this post helpful.
Old 09-01-2020, 09:43 AM   #6
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Original Poster
Rep: Reputation: Disabled
Thank you boughtonp and berndbausch, your scripts are indeed working.

I will try to build my script with the simple substitution command, but will use the sed script if the former does not work.

Thanks!
 
Old 09-01-2020, 09:48 AM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,600

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by berndbausch View Post
This becomes increasingly unreadable. Perhaps variable substitution works with non-constant positions?
Yep, and is also why the complete problem / context should be provided up-front.

Variable substitution doesn't even need a separate variable to go from position to offset - can simply write:
Code:
echo "${myvar:0:$start-1}1234${myvar:$end}"
 
  


Reply



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
[SOLVED] How to replace text using sed or awk in a file without changing the commented text coolpraz Programming 4 01-03-2013 06:19 AM
[SOLVED] Inserting text at specific line X column coordinates udiubu Programming 11 08-07-2012 09:11 AM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
Sed - replace text only in a certain range twantrd Programming 4 11-30-2006 06:54 PM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:12 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
Open Source Consulting | Domain Registration