Issue
I am new to linux and trying to learn some commands.
In a directory I have so many files
-rw-rw---- 1 mnp8u mnp8u 16440 Aug 3 08:57 mnpcOutboundAdapter.log
-rw-rw---- 1 mnp8u mnp8u 11394510 Aug 3 08:57 x400.log
-rw-rw---- 1 mnp8u mnp8u 3838561 Aug 3 08:57 performance_cm_workflow.log
-rw-rw---- 1 mnp8u mnp8u 216723 Aug 3 08:57 controllerProcess.log
-rw-rw---- 1 mnp8u mnp8u 4192538 Aug 3 08:57 SP117Process.log
-rw-rw---- 1 mnp8u mnp8u 25689061 Aug 3 08:57 cmserver.log
-rw-rw---- 1 mnp8u mnp8u 1783658 Aug 3 08:57 receiverProcess.log
-rw-rw---- 1 mnp8u mnp8u 8132403 Aug 3 08:57 performance_outbound_adapter.log
-rw-rw---- 1 mnp8u mnp8u 949876 Aug 3 08:57 mnpcOutboundAdapter.log.1
-rw-rw---- 1 mnp8u mnp8u 5499756 Aug 3 08:57 cm_http_server.log
-rw-rw---- 1 mnp8u mnp8u 587538 Aug 3 08:57 mrdbSenderProcess.log
-rw-rw---- 1 mnp8u mnp8u 8716088 Aug 3 08:57 performance_mrdb.log
I have skipped files to reduce length.
In this directory for a particular file name like performance_mrdb.log, it has files like performance_mrdb.log.1, performance_mrdb.log.2, etc.
So, when I use simple grep command
ls -lrt |grep performance_mrdb.log
-rw-rw---- 1 mnp8u mnp8u 10240007 Jul 11 10:18 performance_mrdb.log.10
-rw-rw---- 1 mnp8u mnp8u 10240033 Jul 11 10:30 performance_mrdb.log.9
-rw-rw---- 1 mnp8u mnp8u 10240007 Jul 11 10:41 performance_mrdb.log.8
-rw-rw---- 1 mnp8u mnp8u 10240033 Jul 11 10:51 performance_mrdb.log.7
-rw-rw---- 1 mnp8u mnp8u 10240007 Jul 11 11:02 performance_mrdb.log.6
-rw-rw---- 1 mnp8u mnp8u 10240033 Jul 11 11:13 performance_mrdb.log.5
-rw-rw---- 1 mnp8u mnp8u 10240026 Jul 15 00:03 performance_mrdb.log.4
-rw-rw---- 1 mnp8u mnp8u 10240078 Jul 20 10:14 performance_mrdb.log.3
-rw-rw---- 1 mnp8u mnp8u 10240053 Jul 26 09:24 performance_mrdb.log.2
-rw-rw---- 1 mnp8u mnp8u 10240023 Jul 30 00:28 performance_mrdb.log.1
-rw-rw---- 1 mnp8u mnp8u 8716652 Aug 3 08:58 performance_mrdb.log
It is showing all the files that have performance.mrdb.log in their name. But I want to see only performance_mrdb.log file.
Is there any option by which I can search for exact match using grep command. Please suggest answer for awk command also.
Solution
Please suggest answer for awk command also.
Try
your_ls_command | awk '$NF=="performance_mrdb.log"'
Explanation: print only lines where content of last column ($NF
) is performance_mrdb.log
. This use awk
s default understanding of column, i.e. it does assume that columns are sheared by 1 or more whitespaces. NF
is one of built-in awk
variables, if you want to know more read 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR.
(tested in gawk 4.2.1)
Answered By - Daweo Answer Checked By - Pedro (WPSolving Volunteer)