Skip to content
Advertisement

How to filter a “diff” command to exclude certain entries?

I have to take some text my professor has given me, put it into two files, and I have to use the “diff” function to compare those two files. But, some lines in those files are missing an entry in the 6th (last) column. I need to filter the diff command to exclude those lines.

I tried to use the diff command with a regex, but I was unable to make a regex that was able to filter just that last column.

The file I am trying to filter looks something like:

b72cc000-b72ce000 rw-p 00147000 fc:00 3671434    /usr/lib/i386-linux-gnu/libX11.so.6.3.0
b72ce000-b72d0000 rw-p 00000000 00:00 0 
b72d0000-b72d7000 r-xp 00000000 fc:00 7606808    /lib/i386-linux-gnu/librt-2.23.so
b72d7000-b72d8000 r--p 00006000 fc:00 7606808    /lib/i386-linux-gnu/librt-2.23.so
b72d8000-b72d9000 rw-p 00007000 fc:00 7606808    /lib/i386-linux-gnu/librt-2.23.so
b72d9000-b72f2000 r-xp 00000000 fc:00 7602217    /lib/i386-linux-gnu/libpthread-2.23.so
b72f2000-b72f3000 r--p 00018000 fc:00 7602217    /lib/i386-linux-gnu/libpthread-2.23.so
b72f3000-b72f4000 rw-p 00019000 fc:00 7602217    /lib/i386-linux-gnu/libpthread-2.23.so
b72f4000-b72f6000 rw-p 00000000 00:00 0 
b72f6000-b72f9000 r-xp 00000000 fc:00 7602261    /lib/i386-linux-gnu/libdl-2.23.so
b72f9000-b72fa000 r--p 00002000 fc:00 7602261    /lib/i386-linux-gnu/libdl-2.23.so

I need to run the diff command on two files similar to this in a linux virtual machine. The diff should filter out the 2nd and 9th rows because there is nothing in the last column. I do not have install privileges on the machine, so I can’t install anything new. There is a vim editor and a C compiler pre-installed; however, so I can use those.

Advertisement

Answer

Use awk and filter by the number of fields.

awk 'NF > 5' filename

will only print the lines that have at least 6 fields.

You can use this with diff with process substitution:

diff <(awk 'NF > 5' file1.txt) <(awk 'NF > 5' file2.txt)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement