I want to remove 1 previous line and 10 following lines every time a string was found in the file.foo.txt:}Name: johnLine 3Line 4Line 5Line 6Line 7Line 8Line 9Line 10Line 11Line 12Appleorangegrapepine}Name: RubenLine Line Line Line Line Line Line Line Line Line grapezebradonkey}Name: TomLine Line Line Line Line Line Line Line Line Line Tigerredblueorangetomatocat}Desired Out.txt :Name: johnAppleorangegrapepineName: RubengrapezebradonkeyName: TomTigerredblueorangetomatocatI want to use sed or nawk. The flower brace and the next 10 lines after Name: should go away. Using Solaris 5.10 with ksh.
How to remove certain number of lines every time particular string is found in file
sed;awk;solaris
Using awk$ awk '/}/{next;} /Name:/{print;n=NR+10} NR>n' fileName: johnAppleorangegrapepineName: RubengrapezebradonkeyName: TomTigerredblueorangetomatocatHow it works/}/{next;}Skip over any line containing }./Name:/{print;n=NR+10}When we reach a line containing Name:, print it and, so we know when to start printing again, set n to 10 plus the current line number.NR>nIf the current line number is greater than n, then do the default action: print the line.Using sedWith very similar logic:$ sed -e '/}/d' -e '/Name:/{p;N;N;N;N;N;N;N;N;N;N;d;}' fileName: johnAppleorangegrapepineName: RubengrapezebradonkeyName: TomTigerredblueorangetomatocatHow it works/}/dDelete any line containing }./Name:/{p;N;N;N;N;N;N;N;N;N;N;d;}If we find a line containing Name:, print it, then read in the next 10 lines and delete them.Lines not falling into either the above categories are printed.