cled - Command Line-Editor - Edit text file lines by commands directly from shell



NAME

cled - Command Line-Editor - Edit text file lines by commands directly from shell


SYNOPSIS

 cled [<OPTIONS>] <EXPR> [<EXPR> [...]] [-- <FILE> [<FILE> [...]]]


DESCRIPTION

Each EXPR expression consist of an optional SELECTOR term and a command and variable number of arguments depending on the command (see COMMANDS section), like:

 <SELECTOR>[..<SELECTOR>] <COMMAD> [<ARG> [<ARG> [...]]]

You may narrow the COMMAND's effect by a SELECTOR term. A SELECTOR selects either one line or a closed range of lines. For ranges, put .. (double dot) between SELECTORs. A SELECTOR may be a regexp, ie. /PATTERN/[MODIFIERS], or a line number, or the literal word last which means the last line. SELECTOR syntax:

 [ /<REGEXP>/[<MODIFIERS>][[+ | -]<OFFSET>] | <LINE> | last[-<OFFSET>] ]

Examples:

 10../lorem/i

Select line 10 and all subsequent lines down to the first one matching to case-insensitive "lorem".

 /begin/../end/

Select the lines between /begin/ and /end/ inclusively.

 /begin/+1../end/-1

Select the lines between /begin/ and /end/ exclusively.

The 2nd SELECTOR does not get tested on the same line as the 1st SELECTOR. So you can select at least a 2-lines long range by eg. /start/../stop/. If the 2nd SELECTOR is REGEXP and it does not match any lines, then practically it's an open-ended range, so the rest of the file is selected. Line numbers are indexed from 0.

REGEXP and the last SELECTOR have an optional OFFSET: eg. /begin/+2 selects the 2nd line following a line matching to /begin/. The last SELECTOR obviously supports only negative OFFSETs.

An EXPR expression can be a group denoted by brackets, in which there are subexpressions like EXPR. This way you can do multiple COMMAND commands, all when the common SELECTOR matches to the line in order:

 /^d/ [ ltrim prepend " " ]

Which removes all leading whitespace, if any, from lines starting with "d" and inserts a single space at the beginning of all of them. See COMMANDS below for all the supported editor commands.

Currently 3 type of brackets are supported: [ ... ] square, { ... } curly, and ( ... ) parenthesis. Use the square one to save shell escaping.


OPTIONS

-f, --file PATH

File to edit. May specify multiple times. Files are edited in-place by default, by persisting their Inode, ie. buffer the output data and write to the original input file when it's all read up. If --output option(s) is (are) given, then the file(s) won't be modified in-place, rather than saved in output file(s). If not given any --file, works on STDIN and print to STDOUT.

-o, --output PATH

File to save modified data into. May specify multiple times. If less --output parameters given than --file, then the input files without a corresponding output file will be edited in-place.

-c, --confirm

Prompt for confirmation for each selected line. If a readline module is installed then you may do further changes to the lines interactively. Term::ReadLine::Gnu(3pm) module is recommended.

Press Ctrl-J to insert newline (LF) at the cursor position, as it's not added automaticaly to the end of line.

If no readline module available, press only a single Enter to accept changes, and Ctrl-C to revert to the original line, or type in new content and press Enter to replace the promped line (newline is added to the end in this case). Additionally an inverse space char at the end of line indicates if the last line is not terminated by a newline.

-v, --verbose

Print edited lines to STDERR. Prefixed with line number if option -l is given. A line is edited if it's selected by any SELECTOR and not reverted thereafter at the interactive prompt.

-l

Show line numbers in verbose mode.


COMMANDS

s/<PATTERN>/<REPLACEMENT>/[<MODIFIERS>]

Regexp substitution. Works just like in perl(1). See perlre(1).

edit

Edit selected lines interactively by a readline interface. See --confirm option in OPTIONS section for details.

delete

Delete matching line(s).

ltrim, rtrim, trim

Remove leading (ltrim), trailing (rtrim), or leading and trailing (trim) whitespace from the line. End-of-line char (LF, \ ) is preserved.

replace STR1 STR2

Replace all STR1 to STR2.

replaceword STR1 STR2

Replace whole word STR1 to STR2.

replaceline STR

Replace the whole line to STR.

prepend STR

Prepend STR to the line.

insertline STR

Insert STR as a whole line before the matching line(s). Line numbering is preserved as there was not an inserted line, ie. line numbers are not incremented.

insertfile PATH

Insert the content of PATH file before the matching line(s). The last line of PATH file will be separated from the matched line by a newline (LF) either way. Line numbering is preserved as described above.

append STR

Append STR to the line.

appendline STR

Append STR as a whole line to the matching line(s). Line numbering is preserved as described above.


SIMILAR PROJECTS

https://github.com/andrewbihl/bsed