/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package editored;



import java.io.*;
import java.util.*;
import java.util.regex.*;


class Ed {
    private String fFileName;
    private ArrayList<String> fRows;
    private int fCurrLine;
    private boolean fQuit = false;
    private BufferedReader fir;


    private int fMinLine, fMaxLine = -1;
    private char fCommand;
    private String fRegexp;
    private boolean fAddressWas = false;

    private void parseCommand(String aLine) throws Exception {
        fCommand = 'U';
        fMinLine = -1;
        fMaxLine = -1;
        fAddressWas = false;
        String aNumber = "";
        for (int i = 0; i < aLine.length(); i++) {
            char c = aLine.charAt(i);
            if (c >= '0' && c <= '9') {
                aNumber = aNumber + c;
                fAddressWas = true;
            } else if (c == '.') {
                fAddressWas = true;
                if (fMinLine == -1)
                    fMinLine = fCurrLine;
                else fMaxLine = fCurrLine;
            } else if (c == '$') {
                fAddressWas = true;
                if (fMinLine == -1)
                    fMinLine = fRows.size();
                else fMaxLine = fRows.size();
            } else if (c == ',') {
                if (fMinLine == -1)
                    fMinLine = Integer.parseInt(aNumber);
                aNumber = "";
            } else if (c == 'a' || c == 'i' || c == 'c' ||
                       c == 'q' || c == 'n' || c == 'w' ||
                       c == 's' || c == 'p') {
                       if (!aNumber.equals("")) {
                            if (fMinLine == -1)
                                fMinLine = Integer.parseInt(aNumber);
                            else fMaxLine = Integer.parseInt(aNumber);
                       }

                       fCommand = c;
                       fRegexp = aLine.substring(i+1);
                       break;
            } else throw new Exception();
        }

        if (fMinLine == -1)
            fMinLine=fCurrLine;
        if (fMaxLine == -1)
            fMaxLine = fMinLine;

        if (fMaxLine < fMinLine)
            throw new Exception();
        if (fMaxLine > fRows.size() || fMinLine > fRows.size())
            throw new Exception();
        
        return;
    }

    public void mainLoop () throws IOException {
        fir = new BufferedReader (new InputStreamReader(System.in));
        do {
            String s = fir.readLine();
            try {
                parseCommand(s);
            } catch (Exception e) {
                fCommand = 'U';
            }
            try {
                switch (fCommand) {
                    case 'q':
                        quit();
                        break;
                    case 'w':
                        saveFile();
                        break;
                    case 'n':
                        commandN();
                        break;
                    case 'i':
                        commandI();
                        break;
                    case 'a':
                        commandA();
                        break;
                    case 'c':
                        commandC();
                        break;
                    case 's':
                        commandS();
                        break;
                    case 'p':
                        commandP();
                        break;
                    default:
                        unknown();
                        break;
                }
                //System.out.println(fCurrLine + "command executed");
            } catch (Exception e) {
                unknown();
            }
        } while (!fQuit);

        fir.close();
    }

    private ArrayList<String> readInput() throws IOException {
        ArrayList<String> as = new ArrayList<String>();
        String s = fir.readLine();
        while (!s.equals(".")) {
            as.add(s);
            s = fir.readLine();
        }

        return as;
    }
    private void unknown() {
        System.out.println("Something is wrong");
        
        return;
    }

    private void quit() {
        fQuit = true;

        return;
    }
    private void saveFile () throws IOException {
        BufferedWriter bw = new BufferedWriter (new FileWriter(fFileName));
        for (String s:fRows) {
            bw.write(s);
            bw.write("\n");
        }
        bw.close();
        
        return;
    }
    private void commandN () {
        fCurrLine = fMinLine;
        if (!fAddressWas)
            System.err.println(fCurrLine);
    }
    private void commandA () throws Exception {
        ArrayList<String> as = readInput();
        // Radky zacinaji od jednicky, proto tady neni +1
        if (!fRows.addAll(fMinLine, as))
            throw new Exception();
        fCurrLine = fMinLine + as.size();
    }
    private void commandI () throws Exception {
        ArrayList<String> as = readInput();
        fRows.addAll(fMinLine-1, as);
        fCurrLine = fMinLine + as.size() - 1;
        return;
    }
    private void commandC () throws Exception {
        ArrayList<String> as = readInput();
        int rozdil = fMaxLine - fMinLine;
        assert (rozdil >= 0);
        do {
            fRows.remove(fMinLine - 1);
            rozdil--;
        } while (rozdil >= 0);
        fMinLine--;
        fRows.addAll(fMinLine,as);
        fCurrLine = fMinLine + as.size();

        return;
    }
    private void commandS () throws Exception {
        fRegexp = fRegexp.substring(1);
        fRegexp = fRegexp.substring(0,fRegexp.length()-1);
        int patternEnd = -1;
        for (int i = 0; i < fRegexp.length(); i++) {
            if (fRegexp.charAt(i) == '/') {
                if (i == 0)
                    patternEnd = i;
                else if (fRegexp.charAt(i-1) != '\\')
                    patternEnd = i;
            }
            if (patternEnd != -1)
                break;
        }

        String vReg = fRegexp.substring(0, patternEnd);
        String vRep = fRegexp.substring(patternEnd+1,fRegexp.length());

        Pattern p = Pattern.compile(vReg);

        for (int i = fMinLine-1; i < fMaxLine; i++) {
            Matcher m = p.matcher(fRows.get(i));
            if (m.find())
                fRows.set(i, vRep);
        }

        return;
    }
    private void commandP () {
        for (int i = fMinLine-1; i < fMaxLine; i++)
            System.out.println(fRows.get(i));
        return;
    }

    private void createFile () {
        fCurrLine = 0;
        
        return;
    }
    private void loadFile () throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(fFileName));
        String s = br.readLine();
        while (s != null) {
            fRows.add(s);
            s = br.readLine();
        }
        br.close();
        fCurrLine=fRows.size();

        return;
    }

    public Ed(String aFileName) throws IOException {
        super();
        fRows = new ArrayList<String>();
        fFileName = aFileName;
        fCurrLine = 0;
        File f = new File(fFileName);
        if (f.exists()) {
            loadFile();
        } else createFile();

        return;
    }
}


/**
 *
 * @author drabm7am
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            Ed e = new Ed(args[0]);
            e.mainLoop();
        } catch (Exception e) {
            System.out.println("Unhandled exception!");
        }
    }

}
