linux - grep exact word match (-w) does not work with file paths in a text file -


im trying grep filename fullpath in file contains 'ls -l' output, fails match correctly.

line in shell script string search

pcline=`grep -w "$file1" $file2` # grep file1 in file2 contents 

if echo command, output of command looks below

grep -w run /home/rajesh/rootfs.layout  expected lrwxrwxrwx 1 root root 3 aug 28 run  actual lrwxrwxrwx 1 root root 7 aug 28 bin/run-parts lrwxrwxrwx 1 root root 3 aug 28 run -rwxr-xr-x 1 root root 303 aug 28 tests/aes/run.sh -rwxr-xr-x 1 root root 445 aug 28 tests/auto_ui/run.sh -rwxr-xr-x 1 root root 320 aug 28 tests/available_memory/run.sh -rwxr-xr-x 1 root root 308 aug 28 tests/fonts/run.sh -rwxr-xr-x 1 root root 309 aug 28 tests/html_config_page/run.sh -rwxr-xr-x 1 root root 361 aug 28 tests/ipc/run.sh -rwxr-xr-x 1 root root 304 aug 28 tests/json/run.sh -rwxr-xr-x 1 root root 303 aug 28 tests/log4cplus_cpp/run.sh -rwxr-xr-x 1 root root 301 aug 28 tests/log4cplus_c/run.sh -rwxr-xr-x 1 root root 751 aug 28 tests/msm_basic/run.sh -rwxr-xr-x 1 root root 472 aug 28 tests/res_man_dependency/run.sh -rwxr-xr-x 1 root root 465 aug 28 tests/res_man_ipc/run.sh -rwxr-xr-x 1 root root 789 aug 28 tests/res_man_multi_process/run.sh -rwxr-xr-x 1 root root 469 aug 28 tests/res_man_private_client/run.sh -rwxr-xr-x 1 root root 492 aug 28 tests/res_man_public_client/run.sh -rwxr-xr-x 1 root root 311 aug 28 tests/virt_mem_config/run.sh lrwxrwxrwx 1 root root 6 aug 28 var/run] 

the trick tried add white space, guaranteed in input file, works in console, not when assigned variable.

grep " tests/aes/run.sh" /home/rajesh/rootfs.layout 

line in script

pcline=`grep \"" $file1"\" $file2`     # grep file1 in file2 contents 

please let me know if have committed errors in script.

you can use egrep this:

egrep "(^| )$file1( |$)" "$file2" 

if file1="run" above command match string run preceded line start or space , followed space or line end.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -