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


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?

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


|May 21 2014 12:00AM|

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


|May 21 2014 12:00AM

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


May 21 2014 12:00AM

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


May 21 2014 12:00AM

800+ Java Interview Q&As Menu

Learn by categories on the go...
Learn by categories such as FAQs – Core Java, Key Area – Low Latency, Core Java – Java 8, JEE – Microservices, Big Data – NoSQL, Architecture – Distributed, Big Data – Spark, etc. Some posts belong to multiple categories.