Unix Sed commands for Java developers

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.

Example 1: substitute Java with C. s –> substitute

Example 2: to extract “Peter” from “user_name=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.

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.

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.


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


outputs

|May 21 2014 12:00AM|

Step 3: Remove the last pipe with regex “|$”. “-e” means expression,


outputs

|May 21 2014 12:00AM

Step 4: Remove the starting pipe with regex “^|”. “g” means global change


outputs

May 21 2014 12:00AM

Step 5: to break the commands in multiple lines, use “\”


outputs

May 21 2014 12:00AM


Categories Menu - Q&As, FAQs & Tutorials

Top