Delete all files from the directory except the ones in the log file

I have a log file with contents like below.

Repository: https://someserver:9443/
Workspace: (1000) "test_scripts_ws"
  Component: (1001) "some_Automated_Scripts"
    Change sets:
      (1002) ---$ john "test memory" 17-Sep-2014 02:24 PM
        Changes:
          --a-- \Libs\test1\test1\test1.txt
          --m-- \Libs\test1\test1\test11.txt
          --d-- \Libs\file\in\some\folder\lock.lck
          ---c- \Libs\Libs\test2\test2\test22.txt
          ---c- \Libs\Libs\test2\test2\test2.txt
          
      (1003) ---$ john "Test exist:"  17-Sep-2014 12
:33 PM
        Changes:
          ---c- \Libs\Libs\test3\test33.txt
      (1004) ---$ john "Memory Fix #2" 17-Sep-2014 12:47 PM
        Changes:

I want to delete all the files in the directory except the ones starting with the strings

--a--
--m--
---c-

for now I have :

cat required.txt | grep -e --a-- -e --m-- -e ---c-

Output:

--a-- \Libs\test1\test1\test1.txt
 --m-- \Libs\test1\test1\test11.txt
 ---c- \Libs\Libs\test2\test2\test22.txt
 ---c- \Libs\Libs\test2\test2\test2.txt
 ---c- \Libs\Libs\test3\test33.txt

The script should not error even if say test33.txt doesn't exist in the directory

What shell do you use? In bash , you can set the extglob option to perform extended pattern matching like !(...) for "not matching pattern".

Hi ,

I am using bash in cygwin. Can you please elaborate ?

So far I have

cat required.txt | grep -e --a-- -e --m-- -e ---c- | awk '{print $(NF-1)}'

Output:

\Libs\test1\test1\test1.txt 
 \Libs\test1\test1\test11.txt  
\Libs\Libs\test2\test2\test22.txt 
 \Libs\Libs\test2\test2\test2.txt  
\Libs\Libs\test3\test33.txt

---------- Post updated at 03:36 PM ---------- Previous update was at 03:34 PM ----------

Trying to get the file name using cut in reverse order ..not successful so far..

Your long pipe can be replaced by sth like

awk '/--(a-|m-|-c)-/ {print $(NF-1)' required.txt

For the extglob, try (in bash!)

shopt -s extglob
ls !(test1*)

If happy, extend to the result of above awk and do the rm on it.

then add " |xargs rm -rf "

but to make sure

use this first

" |xargs echo "

He/she wants to NOT remove the files listed!

$ cat r
--a-- \Libs\test1\test1\test1.txt
--m-- \Libs\test1\test1\test11.txt
--d-- \Libs\file\in\some\folder\lock.lck
---c- \Libs\Libs\test2\test2\test22.txt
---c- \Libs\Libs\test2\test2\test2.txt
 $ cat r|egrep -v '(--m--|---c-)'
--a-- \Libs\test1\test1\test1.txt
--d-- \Libs\file\in\some\folder\lock.lck
$

@Rudic: The output of the command:

awk '/--(a-|m-|-c)-/ {print $(NF-1)} ' required.txt
--a--
--m--
---a-

@kenshinhimura

There is no output for the command since all the files in the directory does not have a entry in the log file

$ cat r|egrep -v '(--m--|---c-)'

---------- Post updated at 02:01 PM ---------- Previous update was at 01:58 PM ----------

Currently I have

$ cat test.txt | grep -e --a-- -e --m-- -e ---c- | awk -F'\' '{print $NF}'

Output:

test1.txt
test11.txt
test22.txt
test2.txt
test33.txt

Need to delete all the files except the list above.

Then this

cannot be true. You are using $(NF-1) in your snippet, getting the output you need, so why should my proposal (which is based on your post) give the wrong result?

And, did your read the whole post? I gave a method (in bash ) to remove all files but the listed ones.

Your requirements are changing often. I suggest a multi-step approach:
1.

$ awk -F'\' '$1~/--a--|--m--|---c-/ {print $NF}' required.txt > exclude.txt
$ cat exclude.txt
test1.txt
test11.txt
test22.txt
test2.txt
test33.txt
$ ls -1 | fgrep -xvf exclude.txt
exclude.txt
required.txt
test.txt

Is this what you want to delete? Then go for it...
3.

$ ls -1 | fgrep -xvf exclude.txt | xargs rm