I have a file like:<Set of long paragraphs here><Set of long paragraphs here><Set of long paragraphs here>AfterAddress1Address2Address3Before<Set of long paragraphs here><Set of long paragraphs here><Set of long paragraphs here>AfterAddress1Address2Address3Before<Set of long paragraphs here><Set of long paragraphs here><Set of long paragraphs here>AfterAddress1Address2Address3Before<Set of long paragraphs here><Set of long paragraphs here><Set of long paragraphs here>AfterAddress1Address2Address3Before<Set of long paragraphs here><Set of long paragraphs here><Set of long paragraphs here>AfterAddress1Address2Address3Before<Set of long paragraphs here><Set of long paragraphs here><Set of long paragraphs here>I need to write the lines between the words After and Before into separate output files. For example, the output files should look like:Line Number : Address1Line Number : Address2Line Number : Address3............................................I tried this:awk '/After/,/Before/' Details.txt>output.txtbut it didn't work. How can I do this?
Paragraph based searching using before and after conditions UNIX KSH
text processing;awk
You can try something like this:$ awk '/After/{a=1;next}/Before/{a=0}; (a==1){print NR : $0}' file 5 : Address16 : Address27 : Address313 : Address114 : Address215 : Address321 : Address122 : Address223 : Address329 : Address130 : Address231 : Address337 : Address138 : Address239 : Address3The idea is very simple. The a variable changes value if the current line matches After or Before and we print the line only if a is equal to 1. To save them into separate output files, you could do:awk '/After/{a=1; k++;next}/Before/{a=0}; (a==1){print NR : $0 > k.out}' file Here, I am using the variable k to count how many times the word After is found. The output file will be the current value of k and .out. If I run the above on your example, I get:$ ls1.out  2.out  3.out  4.out  5.out