Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 – PersianGulf 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

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/'

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.