Efficient text editing in Unix Command Line Mode (1)

Source: Internet
Author: User
Tags printable characters

A Brief Study of some basic command line text editing programs that can save time and effort. Text editing is usually performed interactively in the text editor application. However, some tasks can be conveniently and quickly completed directly from the UNIX command line. In addition, you can use these single-command-line programs in the script to automate various editing processes.
Most UNIX Developers choose Emacs, vi, or one of the many variants, branches, and clones of these two text editing applications. The operator usually opens a file in the selected text editor and interactively specifies and applies changes to the file.
However, compared to opening a file in a text editor, you can edit the file faster on the command line. Complex editing processes can be programmed and specified from the command line and executed across multiple files, eliminating unnecessary screen display, cursor movement, and manual interaction with files. A good strategy is to keep related command line programs at hand to complete common editing work. They not only save you time, especially in batch operations involving multiple files), but you can also use them in scripts.
The single-command-line program used to edit and process text is a well-known traditional feature in Perl, AWK, and recent Ruby) and of course Shell. This article uses three major command line editing tools available on all systems at any time to demonstrate basic text editing techniques: cat, ed, and sed. The following editing example begins with the simplest and most common structure and gradually transitions to a more complex structure.
Use cat for editing
Use cat to indicate "connection") to connect files and standard input streams, as shown in Listing 1. The world's slots also use it as a common paging Program (cat file) and a complete text editing environment (cat> file ). Its syntax is unparalleled in simplicity, and it also provides you with a shortcut to append or insert text without an editor for a single command line program.
Listing 1. Using cat to connect files and standard input streams
$ (Cat-input1-input2-input3-input4) | mailx ted
Take a look at these example files.
This is the first file...
This is the second file...
This is the third file -- note the fourth paragraph below...
And here's the last file...
Add text to the end of a file
However, the lazy is also about strategy. When you need to append text to the end of a file, there is no faster method than using cat:
$ Cat> file
> Line
> Line
> Line
When you add a row, press Ctrl-U to delete the current row, press Ctrl-Z to suspend the process, and press Ctrl-C to stop all operations. After editing, you can press Ctrl-D on each row. Some default Korn Shell control keys exist, but they apply to most Shell and editing modes .)
If the data you are entering is the X selection pasted from another window, the single command line program is usually faster, because you do not have to call an editor, open the target file, move to the end of the file, paste and select, save the file, and then exit the editor. When you paste formatted or specially formatted text, and you want to retain the format because some text editors and editing modes will re-format it when you paste X selection, A single command line program is also more useful.
Although this operation is very common and is a daily activity, you must be careful to use the shell operator to append the redirection (>>) instead of the normal redirection operator (> ); if you mistakenly use the latter, the original content of the file will be rewritten using the text to be appended.
To append all the content of a file to the end of another file, you can provide the file name:
$ Cat footnotes.txt> file
If you only append a single row instead of multiple lines or the entire file, you can use echo instead of cat:
$ Echo "192.00000000255 bigblue">/etc/hosts
To append a text line numbered from 1, you can use the-n option of cat. In this way, a line number is appended to a row with a maximum offset of five space characters) and a tab. You can add the-B Option to disable blank row numbers:
$ Cat-nb> file
This line is numbered
And so is this
Another numbered line
$ Cat file
1 This line is numbered
2 And so is this
3 Another numbered line
Insert text at the beginning of the file
By specifying the standard input with a hyphen (-) and writing it to a new file, you can use cat to insert text at the beginning of the file:
$ Cat-file> newfile
This is the beginning of the file
And then the old file is inserted
Below this line:
Although this single-command-line program is very simple, its disadvantage is that it creates a new file. If you want to insert the text into the original file, the necessary rename will make this single-command line program a failure. A better way is to use the ed that will be introduced soon.
Show non-printable characters
Cat has several useful options. Some of these options control the way it outputs non-printable characters, such as tabs and control characters. To determine whether a file or a group of text files have embedded control characters, you can use these options. For example, if a file has trailing spaces, you can use these options:
$ Cat-vet input.txt
This line has trailing blanks. $
This line does not. $
These options vary with UNIX implementations; table 1 provides options for the standard ibm aix operating system.
Table 1. Options for Output Control in AIX cat
Option description
-B does not have a blank row number.
-E: use the $ character to display the end of the line.
-N indicates the number of all output rows starting from 1.
-Q: Use the silent operation to disable error messages ).
-R replaces multiple empty rows with a single row "COMPRESSED" blank ).
-S compresses multiple blank rows into a single row, which is the same as-r ).
-S disables silent operation on error messages ).
-T displays the tab as ^ I.
-U does not buffer the output.
-V displays non-printable control characters visually.
Use ed for editing
As the name implies, the row editor ed edits the row of the input file. It reads the entire file into its own buffer, performs the specified operation on the copy, and optional writes the buffer to the disk. You can specify any number of rows in the edit operation, and these operations can be combined and specified in a sequence. These facts make ed an ideal choice for scripting. Specify the operation in the following format:
[Address] command [text]
Address specifies the row or multiple rows to be processed. By default, the current row is used. A single-character command is an operation performed on a specified row. For special single-command-line programs in scripts, you can use echo to transmit a set of commands and text pipelines to ed to use it in a non-interactive manner.
(Echo 'operation ';
... Echo 'wq') | ed-s FILENAME
If you enter text in the operation, a period (.) should be displayed to indicate that the input is complete. Write the final wq file and exit. The-s option allows ed to operate silently and disables all normal output.
Fortunately, the basic addressing methods and commands of ed are quite standardized. Table 2 describes the main addressing formats. Table 3 provides commands.
Table 2. Addressing rows in ed
Option description
. This option specifies the default address of the current row ).
Number this option is used to address the number line. The range (first, last) can be separated by commas to address rows. 0 indicates the beginning of the buffer before the first line ).
-Number this option specifies the number row before the current row. If no number exists, the minus sign is used to address the row that follows the current row.
+ Number this option specifies the number row after the current row. If no number exists, the plus sign addresses the rows that follow the current row.
$ This option is used to address the last line.
This option is used to address the first to last rows, including the first and last rows, which are the same as 1, $ ).
; This option addresses the current row to the last row.
/Pattern/this option is used to address the next row containing the text matching pattern.
? Pattern? This option is used to address the row of the previous text that matches pattern.
Table 3. Main ed commands
Command description
A. This command appends text after the specified address.
C. This command changes the specified address to the specified text.
D. Delete the line at the specified address.
I this command inserts text before the specified address.
Q this command stops the program and exits after saving the buffer to the disk.
R file this command reads the content of filespec and inserts it into the specified address.
S/pattern/replacement/this command replaces the text matching pattern with the replacement text in the specified address.
W file this command writes the specified address to file. If there is no address, this command uses the entire buffer by default.
Insert text at the beginning of the file, part 2
You can easily insert text at the beginning of a file by using the ed single-command line program in the script. The insert operation is completed using ed and appending the given text to the beginning of the 0th line file through the command:
$ Cat file
This is the end.
$ (Echo '0a'; echo 'this is the beginning. '; echo'. '; echo 'wq') | ed-s file
$ Cat file
This is the beginning.
This is the end.
You can perform the same task interactively:
$ Cat file
This is the end.
$ Ed-s file
> 0a
> This is the beginning.
> Wq
$ Cat file
This is the beginning.
This is the end.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.