Amazon

Tuesday, May 7, 2019

Creating Viewing and Editing Text File

Standard input, standard output and standard error - 
A process structure is constructed with numbered channels (file description) to manage open files. Process connect to files to reach data content or devices these files represent. Processes are created with default connections for channels 0,1, and 2 known as standard input, standard output and standard error. Processes use channel 3 and above to connect to other files.

channels (File Descriptors)

Table

Output Redirection Operators

>file - redirect stdout to a file(1)
>>file - redirect stdout to a file, append to current file content(2)
2>file - redirect stderr to a file(1)
2>/dev/null - discard stderr error message by redirecting to /dev/null
&>file - combine stdout and stderr to one file (1)
>>file 2>&1 - combine stdout and stderr, append to current file content (2)(3)
Note: -
(1) overwrite existing file, create file if new.
(2) Append existing file, create file if new.
(3) The order of direction is important to avoid unexpected command behavior.
2>&1 sends stderr to the same place as stdout. For this work, stdout needs to be directed first, before adding stderr to stdout. Although &>> is an alternate way to append both stdout and stderr to a file, 2>&1 is the method needed to send both stdout and stderr through a pipe.

Example for output redirection

Save a timestamp for later reference
[student@desktop ~]$ date > /tmp/saved-timestamp

Copy the last 100 lines from a log file to another file.
[student@desktop ~]$ tail -n 100 /var/log/dmseg > /tmp/last-100-boot-message

Concatenate four lines into one.
[student@desktop ~]$ cat file1 file2 file3 file4 >tmp/all-four-in-one

List the home directory's hidden and regular file names into a file.
[student@desktop ~]$ ls -a >tmp/my-file-name

Append output to an existing file
[student@desktop ~]$ echo "new line of information" >> /tmp/many-lines-of-information

[student@desktop ~]$ diff previous-file current-file >> /tmp/tracking-changes-made

In the next examples, errors are generated since normal users are denied access to system directories. Redirect error to a file while viewing normal command output on the terminal.

[student@desktop ~]$ find /etc -name passwd 2> tmp/errors

Save process output and error message to separate files.
[student@desktop ~]$ find /etc -name passwd > /tmp/output 2> /tmp/errors

Ignore and discard error message.
[student@desktop ~]$ find /etc -name passwd > /tmp/output 2>dev/null

Store output and generated errors together
[student@desktop ~]$ find /etc -name passwd &> /tmp/save-both

Append output and generated errors to an existing file.
[student@desktop ~]$ find /etc -name passwd >> /tmp/save-both 2>&1

Constructing pipelines
Redirection controls channel output to or from files while piping sends channel output to another process

Examples for process pipeline redirection

Paginate a command's long ouput
[student@desktop ~]$ ls -l /usr/bin | less

Count the number of lines in an output or listing.
[student@desktop ~]$ ls | wc -l > /tmp/how-many-files

Grab the first lines, last lines, or selected lines of command output.
[student@desktop ~]$ ls -t | head -n 10 > /tmp/ten-last-changed-files

Example for using the tee command for piping 
The Tee command displays or redirects the intermediate result normally suppressed due to piping, In the first example, the ls listing is viewed on a terminal while simultaneously being stored in a file.

[student@desktop ~]$ ls -l | tee /tmp/saved-output

Determine the terminal device for the current window. Send the results as mail and view the same results in his window.

[student@desktop ~]$ tty
/dev/pts/0
[student@desktop ~]$ ls -l | tee /dev/pts/0 | mail -s subject
student@desktop1.example.com

Editing files with Vim
Vim is improved version of the vi editor distributed with Linux and Unix systems. Vim is highly configurable and effective for practiced users, including such features as split screen editing, color formatting and highlighting for editing text.

An i keystroke enters insert mode, where all text typed becomes file content. Pressing Esc return to command mode.

A v keystroke enters visual mode, where multiple characters may be selected for text manipulation. use V for multi-line and Ctrl-v for block selection. The same keystroke used to enter visual mode (v, V or Ctrl-v) is used to exit.

The : keystrokes begins extended command mode for tasks like writing the file to save it, and quitting the Vim editor

The instructor will demonstrate a typical file editing session using only basic Vim keystrokes.

1. Open a file with vim filename.
2. Repeat this text entry cycle, as many time as the task requires:
Use arrow key to position the cursor.
Press i to enter insert mode.
Enter Text
Press Esc to return to command mode.
If necessary, press u to undo mistaken edits on the current file.

3. Repeat this text deletion cycle, as many times as the task requires:
Use arrow keys to position the cursor.
Press X to delete a selection of text
If necessary, use u to undo mistaken edits on the current file.

4. To save or exit, choose one of the following to write or discard file edits:
Enter :w to write(save) the file and remain in command mode for ore editing
Enter :wq to write the file and quit Vim.
Enter :q! to quit Vim, but discard all file changes since the last write.

The instructor will demonstrate "yank and put" using visual mode.

1. Open a file with vim filename.
2. Repeat this text entry cycle, as many time as the task requires:
Use arrow key to position the cursor to the first character
Press v to enter visual mode.
Use arrow keys to position the cursor to the last character.
Press y to yank(copy) the selection.
Use arrow key to position the cursor at the insert location.
Press p to put (paste) the selection.

3. Repeat this text deletion cycle, as many times as the task requires:
Use arrow keys to position the cursor.
Press X to delete a selection of text
If necessary, use u to undo mistaken edits on the current file.

4. To save or exit, choose one of the following to write or discard file edits:
Enter :w to write(save) the file and remain in command mode for ore editing
Enter :wq to write the file and quit Vim.
Enter :q! to quit Vim, but discard all file changes since the last write.

No comments:

Post a Comment