WebHosting

Monday, June 19, 2017

Get only matching words using Grep in a command Linux, Shell command

We can use Grep to get the fields or string which contains the specified character, word or string.

cat filename|grep -oh "\w*@\w*"

so suppose a test file contains:

2017-06-19 01:04:49 trigproc man-db:amd64 2.7.5-1 <none>
2017-06-19 01:04:49 status half-configured man-db:amd64 2.7.5-1
2017-06-19 01:04:50 status installed man-db:amd64 2.7.5-1
2017-06-19 01:04:50 configure vim-runtime:all 2:8.0.0095-1ubuntu3 <none>
2017-06-19 01:04:50 status unpacked vim-runtime:all 2:8.0.0095-1ubuntu3
2017-06-19 01:04:50 status half-configured vim-runtime:all 2:8.0.0095-1ubuntu3
2017-06-19 01:04:51 status installed vim-runtime:all 2:8.0.0095-1ubuntu3
2017-06-19 01:04:51 trigproc gnome-menus:amd64 3.13.3-6ubuntu4 <none>
2017-06-19 01:04:51 status half-configured gnome-menus:amd64 3.13.3-6ubuntu4
2017-06-19 01:04:51 status installed gnome-menus:amd64 3.13.3-6ubuntu4
2017-06-19 01:04:51 trigproc hicolor-icon-theme:all 0.15-1 <none>
2017-06-19 01:04:51 status half-configured hicolor-icon-theme:all 0.15-1
2017-06-19 01:04:51 status installed vim-common:all 2:8.0.0095-1ubuntu3
2017-06-19 01:04:51 status installed hicolor-icon-theme:all 0.15-1
2017-06-19 01:04:51 configure vim-tiny:amd64 2:8.0.0095-1ubuntu3 <none>
2017-06-19 01:04:51 status unpacked vim-tiny:amd64 2:8.0.0095-1ubuntu3
2017-06-19 01:04:51 status unpacked vim-tiny:amd64 2:8.0.0095-1ubuntu3
2017-06-19 01:04:51 status half-configured vim-tiny:amd64 2:8.0.0095-1ubuntu3
2017-06-19 01:04:51 status installed vim-tiny:amd64 2:8.0.0095-1ubuntu3
2017-06-19 01:04:51 configure vim:amd64 2:8.0.0095-1ubuntu3 <none>
2017-06-19 01:04:51 status unpacked vim:amd64 2:8.0.0095-1ubuntu3
2017-06-19 01:04:51 status half-configured vim:amd64 2:8.0.0095-1ubuntu3
2017-06-19 01:04:51 status installed vim:amd64 2:8.0.0095-1ubuntu3


in a file /tmp/test then if we want to get all word with ubun we can give the command as

grep -oh "\w*ubun\w*" /tmp/test

will give this word from all lines.

Featured Posts

How to Enable and Configure Remote Desktop (RDP) on Ubuntu 24.04.4 LTS

Remote Desktop Protocol (RDP) support in Ubuntu has become considerably easier with the GNOME Remote Desktop stack included in modern Ubuntu...