sed - What does this regex mean and why -


sed "s/\(^[a-z,0-9]*\)\(.*\)\( [a-z,0-9]*$\)/\1\2 \1/g" desired_file_name 

i apreciate if explain part of or @ lest structure words in s\alphanumerical_at_start\something\alphanumerical_at_end\something_else\global

could explain means, why , regex ... awful ?

i know replaces first lowcase alphanumerical word last one. explain bit bit what's going on here ? what's /\ , \(.*\)\ , else ?

i'm lost.

edit: here get: (^[a-z0-9]*) starting trough z , 0 trough 9; , [a-z,0-9]*$ same last word (however [0-9,a-z] = first 2 characters / first character, or entire word ?). also: * or \(.*\)\ mean ?

this sed search , replacement, form s/search/replace/flags, flag g means search/replace global, if match occurs multiple times on single line instead of first one.

first, here regex searches for:

\(^[a-z,0-9]*\)\(.*\)\( [a-z,0-9]*$\) 

or in more readable format:

\(             # start capture group 1   ^              # match @ beginning of line   [a-z,0-9]*     # 0 or more alphanumeric or comma characters (lowercase only) \)             # end capture group 1 \(             # start capture group 2   .*             # 0 or more of character (except newlines) \)             # end capture group 2 \(             # start capture group 3   [ ]            # literal ' ' character (i added brackets clarity)   [a-z,0-9]*     # 0 or more alphanumeric or comma characters (lowercase only)   $              # match @ end of line \)             # end capture group 3 

here replacement:

\1\2 \1 

this replace entire line (because of ^ , $ anchors in regex) contents of capture group 1, followed contents of capture group 2, space, contents of capture group 1.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -