Home How to exclude rows from a text file for loop
Reply: 1

How to exclude rows from a text file for loop

Stefan
1#
Stefan Published in 2014-11-11 14:00:37Z

I have a textfile (file1.txt) with multiple lines of data. This textfile I'm using to copy data from a directory A to another B. My script looks if an expression is included in a filename of a file stored in A. In the directory A I grep another textfile (file2.txt) to get information (rows like [bla][0-9][0-9][bla][0-9][0-9]) that I want to exclude in my script.

set x = `grep '[bla][0-9][0-9][bla][0-9][0-9]' file1.txt`

foreach i ( $x )
    cp A/*$i* B/.
end

For example rows in file1.txt:

bla11bla11
bla12bla12
bla13bla13
bla14bla14
bla15bla15

and grep result from file2.txt that has to be excluded for the loop

bla11bla11
bla12bla12

My script should finally only use the following lines

bla13bla13
bla14bla14
bla15bla15

How can I do this?

fork2execve
2#
fork2execve Reply to 2016-01-11 21:29:09Z

A simple nested loop does it completely in csh:

#!/bin/csh -f

set f = `grep 'bla[0-9][0-9]bla[0-9][0-9]' file1.txt`
set x = `grep 'bla[0-9][0-9]bla[0-9][0-9]' file2.txt`

echo "Files: $f"
echo "To be excluded from Files: $x"

set r = ( )
foreach i ( $f )
  set skip = 0
  foreach j ( $x )
    if ("$i" == "$j") then
      set skip = 1
      break
    endif
  end
  if ($skip == 0) set r = ($r $i)
end

echo "Result: $r"

The output when run on your above example files:

Files: bla11bla11 bla12bla12 bla13bla13 bla14bla14 bla15bla15
To be excluded from Files: bla11bla11 bla12bla12
Result: bla13bla13 bla14bla14 bla15bla15
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307744 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO