I have two files; the first includes patterns ( file.txt) that I want to search for in the second file (file.cfg).
Once the patter is found in "file.cfg" I want to remove it + whatever comes after it until next Hello that comes at the beginning of the line.
I have created the below script but its not working :
#! /bin/bash
cat file.txt | while read LINE; do
echo $LINE
sed -i "/^$LINE$/,/^Hello/{//p;d;}" "file.cfg"
sed -i "/^$LINE$/d" "file.cfg"
done
It was working fine yesterday on test files, Today I have modified the file's name, and It stopped working.
I am not sure If I changed something by mistake, but if I will use the below from Ubuntu command line, it works :
sed -i "/^Hello World$/,/^Hello/{//p;d;}" "file.cfg"
Also, I added echo in the loop, and I can see each line in "file.txt"
To provide further information, I will give an example of what I need to achieve with this code :
"file.txt" contains patterns I need to find a match in "file.cfg" once the pattern is found, I need to remove it, and anything comes after it until next Hello.
sed -i "/^$LINE$/,/^Hello/{//p;d;}" "file.cfg"
-- > this line should remove anything in between.
sed -i "/^$LINE$/d" "file.cfg"
--- > remove the pattern itself.
+++++++++
See the below example :
the File.cfg is divided into sections; each section starts with Hello
File.txt contains random sections name; I need a script to read the section's name from File.txt and see if it's available in file.cfg , then remove the section name and all of its contents
File.txt :
Hello World
Hello Mohammad
Hello Scripting
File.cfg :
Hellow xyz
a
b
c
Hello World
v
b
n
Hello stack
q
w
e
The final results should be :
Hellow xyz
a
b
c
Hello stack
q
w
e
Once the section name is found, I need to remove everything until the next "Hello" that comes at the beginning of a line ( new section ).
None of the lines start with Hello except the sections name.