Can anyone please explain to me in brief about the syntax of this command? I know the output of this command but want to learn how it is working.

I expect an explanation in terms of how actually this command is written and significance of each and every character used in the command.

sed -i 's|\(LAYOUT PATH \).*|\1 "../GDS/'$1'.gds"|g' drcCustimize2 
  • First you have looked at the man page for sed and realize that this will edit the file (i.e. change and replace it with the edited contents). – mdpc Aug 26 '14 at 6:06
  • sed: Call the sed command
  • -i: Pass sed the -i option: Modify the file in-place
  • 's|\(LAYOUT PATH \).*|\1 "../GDS/'$1'.gds"|g': The '...'$1'..' are used to make this whole part one single argument to the sed command
  • s|\(LAYOUT PATH \).*|\1 "../GDS/'$1'.gds"|g:
    • s|pattern|replacement|options: s is the substitute (or search and replace) command, the | characters are used to separate the parts of that command from each other
    • \(LAYOUT PATH \): Search for the literal string "LAYOUT PATH ", and capture it into a group; the content of this group can be put into the replacement string using \1
    • .*: Match everything until the end of the line
    • \1: Put the matched group 1 into the replacement text
    • "../GDS/: Put the literal string "../GDS/ into the replacement text
    • $1: Put the first argument to the shell-script into the replacement text
    • .gds": Put the literal string .gds" into the replacement
    • g: Do the replacement for every match on a line; without the g, only the first match is replaced
    • drcCustimize2: The file that sed should change
  • Thanks a lot Daniel for clear explanation Regards Madhvi – yisha Aug 26 '14 at 7:14
  • 1
    Strictly speaking 's|\(LAYOUT PATH \).*|\1 "../GDS/'$1'.gds"|g' doesn't necessarily make up one argument because $1 is not quoted so the split+glob operator applies here. 's|\(LAYOUT PATH \).*|\1 "../GDS/'"$1"'.gds"|g' would be correct. – Stéphane Chazelas Aug 26 '14 at 9:27
  • 1
    Also, the g is useless, as a pattern ending with .* cannot possibly match a line more than once. – G-Man Says 'Reinstate Monica' Aug 26 '14 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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