Advertisement
JoeJobs

remove text from text - 2

Dec 31st, 2020
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. I need to remove a multi-line text (contained in a file) from another text file but only the first occurrence
  2. I have Windows so I can use Visual Basic but I also have gnuwin32 so I can use tools like grep, sed, etc
  3. Therefore I need a solution using scripts that can run from command line (not c++ programs)
  4.  
  5. I was searching for this but what I could find was grep solutions that delete all occurrences of the lines searched, and not in the order they exist in the searched multi-line pattern
  6. https://stackoverflow.com/questions/174472/deleting-multiline-text-from-multiple-files
  7. https://askubuntu.com/questions/637639/delete-multiple-lines-from-file-when-text-is-found
  8.  
  9. The text file looks like this:
  10.  
  11. the
  12. quick
  13. the
  14. brown
  15. quick
  16. brown
  17. fox
  18. jumps
  19.  
  20. And I need to remove the lines
  21. quick
  22. brown
  23. fox
  24. And then the result will be:
  25.  
  26. the
  27. quick
  28. the
  29. brown
  30. jumps
  31.  
  32.  
  33.  
  34. I managed to find a very complicated solution:
  35. - transform the newlines into a special character "@"
  36. - read the content of the first file into a variable
  37. - use sed to remove the line from the second file
  38. - transform again the special character "@" into newline
  39.  
  40. However this is quite a mess for very large files that can contain the "@" character
  41.  
  42. This is my script:
  43.  
  44. [QUOTE]cat a.txt | tr -d "\r" | tr "\n" "@" > a1.txt
  45. cat b.txt | tr -d "\r" | tr "\n" "@" > b1.txt
  46.  
  47. set /p MyText=<a1.txt
  48.  
  49. sed -e "s/%MyText%//" b1.txt | tr "@" "\n"
  50. pause[/QUOTE]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement