Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How would I go about replacing part of a string with another part of that same string, in a bash script, using sed?

share|improve this question

closed as not a real question by Gilles, vonbrand, jasonwryan, rahmu, manatwork Mar 24 '13 at 10:32

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
use the following shortcuts : tldp.org/LDP/abs/html/refcards.html#AEN22664 –  Mohsen Pahlevanzadeh Mar 23 '13 at 21:40
    
Do you have the string in a variable? Or in a file? What are you doing with the string? –  Gilles Mar 23 '13 at 21:45
    
This needs a lot more detail to be able to give a reasoned answer. –  vonbrand Mar 23 '13 at 22:24

1 Answer 1

Your question is a bit too generic here.

An example:

sed 's/..\(.*\(..\)\)/\2\1/'

Would replace the first two characters of each line with the two last ones (only if there are at least 4 characters on those lines).

$ echo abcdef | sed 's/..\(.*\(..\)\)/\2\1/'
efcdef

The trick is to use \(...\) to capture what is being matched and use it in the replacement part as \1, \2...

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.