Reputation: 15
So i have a gigantic file (file1) where i need to delete or outcomment specific lines, this file could look something like this:
Lorem ipsum **abc** dolor sit amet,
consectetur adipiscing elit.
Cras finibus **123** laoreet dignissim.
Curabitur dignissim auctor tortor a cursus.
Nullam sapien ante, tempor eu rutrum
...
for this i have file2 which contains strings which i need to locate lines in file1
file2 could look like this:
abc
123
xyz
098
...
Now, when a string from file2 is found, the line, in file1, where it is found + the line directly beneath it, should be outcommented or deleted. so that if 123 is found in the above example, it should delete these two lines (marked with --> ):
Lorem ipsum abc dolor sit amet,
consectetur adipiscing elit.
--> Cras finibus 123 laoreet dignissim.
--> Curabitur dignissim auctor tortor a cursus.
Nullam sapien ante, tempor eu rutrum
...
I hope this makes sense
I was fiddeling around with sed and awk, but never got it to work
Upvotes: 0
Views: 62
Reputation: 74645
Something like this would work:
awk 'NR==FNR{a[$0]; next}p{p=0;next}{for(i in a)if(p = $0 ~ i)next}1' file2 file1
Populate the array a
with the lines in file2
. The first block only applies to file2
because the total record number NR
is equal to the record number of the current file FNR
. next
skips the rest of the blocks.
For each line of file1
, loop through the keys in array a
. If the current line matches the key, skip the line in the output. Also assign p
the true value. For lines where p
is true, set it back to false but skip the line in the output. The 1
at the end is always true, so any line that has made it this far is printed, as the default action is to print the line.
Upvotes: 1
Reputation: 58430
This might work for you (GNU sed):
sed 's|.*|/&/{N;d}|' file2 | sed -f - file1 >file3
Create a sed script from file2 and run it against file1 saving the results in file3.
Upvotes: 1