sed (stream editor) is one of the powerful workhorse commands in UNIX. Recently I had to use it with “Git” and “Maven” to find text “SNAPSHOT” in all pom.xml files, and manually update them with a realease version like 1.0.2 without the suffix “SNAPSHOT”. Here is the command that I ran.
|
1 |
find . -maxdepth 2 -type f -name pom.xml -exec grep 1.0.1-SNAPSHOT {} \; -exec sed -e "s/1.0.1-SNAPSHOT/1.0.1/g" {} \; |
Example 1: substitute Java with C. s –> substitute
|
1 2 3 |
$ echo "Java is a great language" | sed s/Java/C/ C is a great language |
Example 2: to extract “Peter” from “user_name=Peter”.
|
1 2 3 |
$ echo "user_name=Peter" | sed "s/^user_name=\(.*\)/\1/" Peter |
The above uses the regular expression.
^user_name= –> starts with “user_name=”
\ –> regex escape character
\( –> grouping start
.* –> 0 or more of any character. . means any and * 0 or more characters
\) –> grouping end. so, anything that follows user_name= will be captures as \1. “Peter” is captured as \1.
so, “user_name=Peter” will be substituted as “Peter”.
The above command is handy for extracting values from a Java .properties file that has name=value pairs. To assign extracted values to variables, use -n, which is the silent mode and will not print it in the screen.
|
1 2 3 |
typeset username username = `cat /etc/myapp.properties | sed -n "s/^user_name=\(.*\)/\1/p" |
myapp.properties should look like
user_name=Peter
p –> print
Example 3: To fix all occurrences of term “java” in the old.txt file with “Java”, and write the output to file new.txt.
|
1 2 |
sed "s/java/Java/g" < old.txt > new.txt |
Example 4:
Q. How will you display the contents of a file named myapp_last_run_date.dat?
A. cat command to display
Output is say:
| May 21 2014 12:00AM |
Q. How will you extract out the date from the above format?
A.
Step 1: get the first line.
|
1 2 |
$ cat wrap_last_run_date.dat | head -1 |
outputs first line of the file “wrap_last_run_date.dat ”
| May 21 2014 12:00AM |
Step 2: Trim all the white spaces with the sed command
|
1 2 |
$ cat wrap_last_run_date.dat | head -1 | sed -e 's/ *| */|/g' |
outputs
|May 21 2014 12:00AM|
Step 3: Remove the last pipe with regex “|$”. “-e” means expression,
|
1 2 |
cat wrap_last_run_date.dat | head -1 | sed -e 's/ *| */|/g' -e 's/\|$//' |
outputs
|May 21 2014 12:00AM
Step 4: Remove the starting pipe with regex “^|”. “g” means global change
|
1 2 |
cat wrap_last_run_date.dat | head -1 | sed -e 's/ *| */|/g' -e 's/\|$//' -e 's/^|//g' |
outputs
May 21 2014 12:00AM
Step 5: to break the commands in multiple lines, use “\”
|
1 2 3 4 |
cat wrap_last_run_date.dat | head -1 | \ sed -e 's/ *| */|/g' -e 's/\|$//' \ -e 's/^|//g' |
outputs
May 21 2014 12:00AM