This post is just a "Long Answer". It's the sequel of this simpler post on a sed problem I had: BigBrus' Corner - Sed Disable Regex . Here are some tips and curiosity I found.
sed -i 's/http:\/\/127\.0\.0\.1:8080\/webinterface/http:\/\/192\.168\.50\.13:9091\/my\/web\/interface/g' /home/bigbrus_corner/test_sed
Let me explain field per field the above sed command:
- sed : - well, obviously it's sed
- -i : means "in-place", which means "replace in the source file". Default sed behaviour is to output on stdout - which will probably be on the screen - leaving the file unchanged.
- the next part is between two single quotes ( ' ' ) . sed does not need them to run, but it's a good habit to use, for single quotes tells bash not to interpret what's between them. In this way you will only have to handle sed syntax and special characters. If you want bash to use substitution, you should probably use double quotes ( " " ).
- s : means substitution; it's the function you want to execute and it's absolutely the most common. For more functions, read this: Sed Manual - Other commands .
- / : just after s there's the delimiter character. By convention it's / , for there are many other tools that use the same concept and the same character. sed anyway uses the first character after s as delimiter, so you can really use any character.
- http:\/\/127\.0\.0\.1:8080\/webinterface : the text you want to be replaced. It needs to be properly escaped
- / : another delimiter
- http:\/\/192\.168\.50\.13:9091\/my\/web\/interface : the text to put in place. It needs to be properly escaped
- / : another delimiter
- g : it means global. sed will replace all occurrence in the text, not just the first. For more options of the function s, read this: Sed Manual - The "s" Command
- /home/bigbrus_corner/test_sed : the file you want to edit.
Some other specifications on sed:
- If you'd like to know, sed uses basic regular expression by default - sed Manual - Reporting Bugs - Regex syntax clashes (the first sentence of this paragraph is quite neat). Regarding which characters you should escape within a text to replace, you should know that it's a POSIX standard and you can find them, for example, here: Wikipedia - Regular Expression basic
- Well, I have to admit that I lied to you in the examples. It was to make more evident the difference of choosing the right separator. The fact is, "." is a special character in POSIX regex. So, I should have escaped it. But, "." in regex sintax means "match any single character"; incidentally, any dot in IP addresses IS "any single character", so the string matches correctly anyway. But remember, you won't be so lucky again! ;)
- If you would like to use extended regular expression instead of basic ones, you must use -r | --regexp-extended flag.
- sed with s and without options is "line-oriented". This means that it will replace only the first occurrence per line
- What amazed me the most, you can use sed as an interpreter to create scripts. It's rather complicated, but just the fact you can use it it's awesome. More informations here: Sed Manual - Example Scripts . If you want to know more on how to use interpreters in scripts, you can read this post of mine: BigBrus' Corner - SHEBANG! (or #! )
Labels: bash, linux, ubuntu