Skip to main content
Implementation with sed.
Source Link

awk might be a better tool here.

$ cat test.dat LINE 1 LINE 2 POP3_SERVER_NAME = localhost 

Search for lines that contain "POP3_SERVER_NAME"; print the last field. This doesn't depend on POP3_SERVER_NAME always being on line 3, which is probably a Good Thing.

$ awk '/POP3_SERVER_NAME/{print $NF}' test.dat localhost 

Depending on your application, you might need to make the regular expression more stringent. For example, you might want to match only that line that starts with POP3_SERVER_NAME.

$ awk '/^POP3_SERVER_NAME/{print $NF}' test.dat localhost 

Using sed is a little less intuitive. (Thanks, I'm aware of the irony.) Address the line that contains POP3_SERVER_NAME anywhere. Substitute an empty string for all the text from the beginning of the line to the optional space following "=". Then print.

sed -n -e '/POP3_SERVER_NAME/ s/.*\= *//p' test.dat 

awk might be a better tool here.

$ cat test.dat LINE 1 LINE 2 POP3_SERVER_NAME = localhost 

Search for lines that contain "POP3_SERVER_NAME"; print the last field. This doesn't depend on POP3_SERVER_NAME always being on line 3, which is probably a Good Thing.

$ awk '/POP3_SERVER_NAME/{print $NF}' test.dat localhost 

Depending on your application, you might need to make the regular expression more stringent.

awk might be a better tool here.

$ cat test.dat LINE 1 LINE 2 POP3_SERVER_NAME = localhost 

Search for lines that contain "POP3_SERVER_NAME"; print the last field. This doesn't depend on POP3_SERVER_NAME always being on line 3, which is probably a Good Thing.

$ awk '/POP3_SERVER_NAME/{print $NF}' test.dat localhost 

Depending on your application, you might need to make the regular expression more stringent. For example, you might want to match only that line that starts with POP3_SERVER_NAME.

$ awk '/^POP3_SERVER_NAME/{print $NF}' test.dat localhost 

Using sed is a little less intuitive. (Thanks, I'm aware of the irony.) Address the line that contains POP3_SERVER_NAME anywhere. Substitute an empty string for all the text from the beginning of the line to the optional space following "=". Then print.

sed -n -e '/POP3_SERVER_NAME/ s/.*\= *//p' test.dat 
Source Link

awk might be a better tool here.

$ cat test.dat LINE 1 LINE 2 POP3_SERVER_NAME = localhost 

Search for lines that contain "POP3_SERVER_NAME"; print the last field. This doesn't depend on POP3_SERVER_NAME always being on line 3, which is probably a Good Thing.

$ awk '/POP3_SERVER_NAME/{print $NF}' test.dat localhost 

Depending on your application, you might need to make the regular expression more stringent.