I have n files, like:
file1:
1aaa
2eee
Test XXX
Hanna
Lars
file2:
1fff
2ddd
3zzz
Test XXX
Mike
Charly
I want to remove all rows before "Test XXX" from all n files. The number of rows to delete varies between files.
My idea:
for file in 1 :n
do
pos=grep -n "Test XXX" file$file
sed -i "1:$pos-1 d" file$file >new$file
done
This will work for your examples and even if the matched pattern is on the very first line:
For example if you input is simply
This will give you
If you want to keep the first match
Test XXX
then just use:This should work for you:
or simply
You can do it with
bash
( eg for 1 file)Use a
for
loop as necessary to go through all the filesOutput:
Explanation:
-e '/Test *XXX/p'
duplicates the line matching/Test *XXX/
-e '0,/Test *XXX/d'
deletes from line 0 to the first line matching/Test *XXX/
By duplicating the line, then removing the first one, we effectively retain the matched line, successfully deleting all lines BEFORE
Test XXX
Note: this will not work as expected if there are multiple
Test XXX
lines.