A stream editor in C
C
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
Makefile
README.md
commands.txt
example.txt
svi.c

README.md

svi - a stream editor

NOTE: It has come to my attention that some people (a lot, actually) are submitting this code for an assignment in a class. I can understand that, as my code is awesome, and I hope that it can serve you as an example, but DO NOT use my code to submit it as your own. It is not your own. It is mine. And you will probably get a zero and be considered a cheater. Please keep that in mind.

What is svi

It modifies input according to a list of edit commands that are given in a file specified as the command line argument. The edited standard input is output to standard output. Each line of input, and output after editing, will be maximally 256 characters long.
Similar to the sed command found on UNIX machines

Commands

Each edit command consists of an optional line range specification, an edit operation specifier, and data required by the edit operation.

Three types of line range specifications

  • /<text>/-This format specifies all lines that contain the <text>. The <text> can be maximally 80 characters.
  • <1st line number>,<last line number>/-This format specifies all lines in the range <1st line number> to <last line number>, inclusive. Lines in the input file are considered to be numbered from 1, and this numbering is not changed in the editing process.
  • No line range specification specifies all lines in the file.

There are five edit operations

A<text>

Appends the <text> at the end of the line. For example: Ahello jim appends hello jim to all lines in the file. The <text> can be maximally 80 characters.

I<text>

Inserts the <text> at the start of the line. For example: /blah/Ineedle noddle noo inserts needle noddle noo at the start of all lines that contain blah. The <text> can be maximally 80 characters.

O<text>

Inserts the <text> on a new line before the current line. For example: 1,1/OThe Title Inserts a new line before the first line in the file, containing the text The Title. The <text> can be maximally 80 characters. The new line is not put through the editing process.

d

Deletes the line from the file. For example: 3,6/d deletes lines 3 to 6 inclusive.

s/<old text>/<new text>/

Replaces the first occurence of <old text>, in the line, with <new text>. For example: /filename/s/.pas/.c/ replaces the first occurrence of .pas with .c, in all lines containing filename. The <old text> and <new text> can be maximally 80 characters.

Install

svi uses autotools, you only have to run

make

Running svi

Here's an example run

Sample commands file commands.txt
/Never done/I---------------------------------------
1,3/IPrepended to 1,2 and 3 :
1,1/OThis must appear as the first line
A : Appended to all
/line for substitution/s/This is one/This has been substituted on a/
9,10/d
/deleted/IThis should not appear
/Never done/I---------------------------------------
Sample Input file example.txt
1 To have text prepended
2 To have text prepended
3 To have text prepended
4 Unmodified except appended text
5 Unmodified except appended text
6 Unmodified except appended text
7 This is one line for substitution
8 This is one line for substitution
9 This is to be deleted
10 This is to be deleted
11 The last line of the file
Run
./svi commands.txt < example.txt
Sample Output
This must appear as the first line
Prepended to 1,2 and 3 :1 To have text prepended : Appended to all
Prepended to 1,2 and 3 :2 To have text prepended : Appended to all
Prepended to 1,2 and 3 :3 To have text prepended : Appended to all
4 Unmodified except appended text : Appended to all
5 Unmodified except appended text : Appended to all
6 Unmodified except appended text : Appended to all
7 This has been substituted on a line for substitution : Appended to all
8 This has been substituted on a line for substitution : Appended to all
11 The last line of the file : Appended to all