How to Add a Column of Numbers With Sed
Sed is a powerful text editor for Unix designed to make it easy for computer administrators to automate the processing of large amounts of text, such as log files. For example, one common task that administrators may need is to take an existing log file and edit it so that a column of line numbers are added to the left of the text, making it easier to quickly find a location in the file.
Difficulty: Moderately EasyInstructions1Open a terminal by clicking the terminal icon on the desktop.
2Type the following into the terminal:sed = filename.txtThis applies the "number lines" command (=) to number each line of the file. However, the line numbers are placed on separate lines from the lines themselves.
3Type the following to fix the problem:sed = filename.txt | sed 'N;s/n/t'This pipes the data from the first command into another sed command. In order, these tell sed to enter multiline mode and replace all newline characters with tabs. This has the effect of putting the line numbers and the text on the same lines. If done correctly, the output will look something like this:1 This is a line of text in the text file.2 3 Another line of text.4 More text.
References Unix Guide: Sed One Liners
