What I want to do is, in a loop pass a variable to a sed command. Sed should then search a file for a line that matches that variable, then remove all lines below until it reaches a line starting with a constant.
I have managed to write a sed command that will give me the block of text, but I cannot figure out to return the contents of the excluding the text I have searched for.
Code I have so far is.
sed -n '/'"${var[$count]}"'/,/'"Con ST"'q/p' < input | head -5> output
Running sed many times to process one file is extremely wasteful and slow. What you want to do, if I understand your intention, can be done with one single execution of awk.
# Array of lines to skip, initialized in the BASH way
ARRAY=( 3 4 5 )
# Feed the array into awk in the X variable.
# Then split it into the array L[1]=3, L[2]=4, L[3]=5
# Then turn that into the array S[3]=1, S[4]=1, S[5]=1.
# Then we can use the simple statement !S[NR] to check if the
# line should be skipped or not and print it accordingly.
awk -v X="${ARRAY[*]}" 'BEGIN { split(X,L," "); for(Z in L) S[L[Z]]=1; }; !S[NR]' filename
Thanks for the feedback. The command is nearly working. I am still having an issue getting sed to stop searching when it finds the second string in the range.
What it should do is.
Search the file for the first string in the range. This string will have a unique value for each occurrence in the file. So I am using a variable.
Take the lines from the file under the above string until it gets to the second occurrence of the second string in the range. The second string is no unique in the file. This is why I want sed to stop searching when it finds it for the second time after it has found the first string.
File looks like this
1st String. Remove 1
Data
Data
2nd String
2nd String
1st String. Do not remove
Data
Data
2nd String
2nd String
1st String Remove 2
Data
Data
2nd String
2nd String
When I am finished I would want the file to look like
1st String
Data
Data
2nd String
2nd String
When the command runs it removes everything from the file under 1st String. Remove 1
Is this even possible to do in sed? Or should I be looking into using awk. I used sed as I have no experience in Awk and had some in sed.
The code that I am using is as in the second post above
I'd still suggest awk, since it allows you to do rational if/then/else statements in code blocks instead of being completely restricted to inscrutiable line-matching regular expressions. It has those, too, but you get to do what you want with them
But your outline here is still incomplete -- we still have no idea what the contents of your array are, hence, what relation your output has to your input..
In the example above the array would have 2 elements.
1st = Remove 1
2nd = Remove 2
These values come from another file that identifies the String 1 records that need to be removed. So on the separate file there would be no line that is equal to "Do not remove"
The array values are read using a loop.
So on the first pass through of the file the block of text
1st String. Remove 1
Data
Data
2nd String
2nd String
Should be removed. On the second pass the block of text
1st String Remove 2
Data
Data
2nd String
2nd String
Should be removed.
Leaving the block of text below untouched.
1st String. Do not remove
Data
Data
2nd String
2nd String
Do you? Really? If you've got millions of lines as your other thread says, the performance would just be the pits, having to repeat the work 9 times...