Quote:
Originally Posted by
patelamit009
Hi Penchal,
I would appreciate if you can provide me a brief explanation on what you are trying to do in the commnad below.
echo "6-9-2008" | sed 's/\(.\)-\(.\)-\(.*\)/\3-0\2-0\1/g'
Thanks
Amit
The input "6-9-2008" gets converted to "2008-09-06". Here is how it happens.
The sed statement takes the form 's/pattern/replacement/flags'
Here pattern is \(.\)-\(.\)-\(.*\)
replacement is \3-0\2-0\1
and flags is g
In the sed world, a . refers to any character. A .* refers to a collection of characters.
When you do \(.\), you are collecting a single character into a buffer. Hence \(.\)-\(.\)-\(.*\) will put 6 into the first buffer, 9 into the second and 2008 into the third.
Each of these buffers can be accessed within the replacement space by using the notation \1 for the first buffer, \2 for the second and so on and so forth.
When you do \3-0\2-0\1 in the replacement space, you create an output as 2008-09-06.
The 'g' flag tells sed to make the change global i.e. if there are more occurrences, all of them should be considered and changed.