I have a file, lets call it 'a.txt' and this file contains the following text line
do to what
I'm wondering what the SED command is to reverse the order of this text to make it look like
what to do
Do I have to do some sort of append? Like append 'do' to 'to' so it would look like
to ++ do (used ++ just to make it clear)
This might work for you (GNU sed):
Explanation:
G
add a newline to the end of the pattern space (PS):a
loop name spaces/^\n//;t
when the newline is at the front of the PS, remove it and print lines/^(\S+|\s+)(.*)\n/\2\n\1/;ta
insert either a non-space or a space string directly after the newline and loop to:a
The
-r
switch makes the regexp easier-on-the-eye (grouping(...)
, alternation...|...
and the metacharacter for one-or-more+
are relieved of the need of a backslash prefix).sed answer
As this question was tagged sed, my 1st answer was:
First (using arbitraty
_
to mark viewed spaces, whena.txt
containdo to what
:than, when
a.txt
containdo to to what
:There is one
+
for each supressed duplicated word:bash answer
But as there is a lot of people searching for simple bash solutions, there is a simple way:
this could be written:
or even with some bash scripting:
Will do:
Or as pure bash
where submited string have to be submitted as argument:
Or if prossessing standard input (or from file) is required:
So you can process multiple lines:
I would use
awk
to do this:Results:
EDIT:
If you require a one-liner to modify your file "in-place", try:
As Bernhard said,
tac
can be used here:I believe my example is more helpful.
May be you would like perl for this:
I know
tac
can do something relatedWhere the
-s
defines the separator, which is by default a newline.