• No results found

Replacing or substituting string Sed command is mostly used to replace the text in a file.

N/A
N/A
Protected

Academic year: 2020

Share "Replacing or substituting string Sed command is mostly used to replace the text in a file."

Copied!
10
0
0

Loading.... (view fulltext now)

Full text

(1)

Sed editor

SED command in UNIX is stands for stream editor and it can perform lot’s of function on file like, searching, find and replace, insertion or deletion. Though most common use of SED command in UNIX is for substitution or for find and replace. By using SED you can edit files even without opening it, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it.

 SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution).

 SED command in unix supports regular expression which allows it perform complex pattern matching.

Syntax –

sed OPTIONS... [SCRIPT] [INPUTFILE...]

$cat > geekfile.txt

unix is great os. unix is opensource. unix is free os. learn operating system.

unix linux which one you choose.

unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

1. Replacing or substituting string : Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word “unix” with “linux” in the file.

$sed 's/unix/linux/' geekfile.txt

By default, the sed command replaces the first occurrence of the pattern in each line and it won’t replace the second, third…occurrence in the line.

2. Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word “unix” with “linux” in a line.

$sed 's/unix/linux/2' geekfile.txt

3. Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.

$sed 's/unix/linux/g' geekfile.txt

4. Replacing from nth occurrence to all occurrences in a line : Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth… “unix” word with “linux” word in a line.

$sed 's/unix/linux/3g' geekfile.txt

5. Replacing string on a specific line number : You can restrict the sed command to replace the string on a specific line number. An example is

(2)

6. Duplicating the replaced line with /p flag : The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.

$sed 's/unix/linux/p' geekfile.txt

7. Replacing string on a range of lines : You can specify a range of line numbers to the sed command for replacing a string.

$sed '1,3 s/unix/linux/' geekfile.txt

$sed '2,$ s/unix/linux/' geekfile.txt

Here $ indicates the last line in the file. So the sed command replaces the text from second line to last line in the file.

Deleting lines from a particular file :

SED command can also be used for deleting lines from a particular file. SED command is used for performing deletion operation without even opening the file

Examples:

1. To Delete a particular line say n in this example

Syntax:

$ sed 'nd' filename.txt

Example:

$ sed '5d' filename.txt

2. To Delete a last line

Syntax:

$ sed '$d' filename.txt

3. To Delete line from range x to y

Syntax:

$ sed 'x,yd' filename.txt Example:

$ sed '3,6d' filename.txt

5. To Delete from nth to last line

Syntax:

$ sed 'nth,$d' filename.txt Example:

$ sed '12,$d' filename.txt

6. To Delete pattern matching line

Syntax:

$ sed '/pattern/d' filename.txt Example:

(3)

Vi Editor

Operation Modes

While working with the vi editor, we usually come across the following two modes −

Command mode − This mode enables you to perform administrative tasks such as saving the files, executing the commands, moving the cursor, cutting (yanking) and pasting the lines or words, as well as finding and replacing. In this mode, whatever you type is interpreted as a command.

Insert mode − This mode enables you to insert text into the file. Everything that's typed in this mode is interpreted as input and placed in the file.

vi always starts in the command mode. To enter text, you must be in the insert mode for which simply type i. To come out of the insert mode, press the Esc key, which will take you back to the command mode.

Hint − If you are not sure which mode you are in, press the Esc key twice; this will take you to the command mode. You open a file using the vi editor. Start by typing some characters and then come to the command mode to understand the difference.

Getting Out of vi

:q quit out of vi, If your file has been modified in any way, the editor will warn you of this, and not let you quit

:q! Exit without save :w Save the contents :wq Save and quit :ZZ Save and quit

:w filename Save file with given name

Moving within a File

To move around within a file without affecting your text, you must be in the command mode (press Esc twice). The following table lists out a few commands you can use to move around one character at a time –

k Moves the cursor up one line j Moves the cursor down one line

h Moves the cursor to the left one character position l Moves the cursor to the right one character position

Commands in vi can be prefaced by the number of times you want the action to occur. For example, 2j moves the cursor two lines down the cursor location.

Editing Files

(4)

I Inserts text at the beginning of the current line i Inserts text before the current cursor location a Inserts text after the current cursor location A Inserts text at the end of the current line

o Creates a new line for text entry below the cursor location O Creates a new line for text entry above the cursor location

Deleting Characters

x Deletes the character under the cursor location X Deletes the character before the cursor location

dw Deletes from the current cursor location to the next word

d^ Deletes from the current cursor position to the beginning of the line d$ Deletes from the current cursor position to the end of the line D Deletes from the cursor position to the end of the current line dd Deletes the line the cursor is on

2x deletes two characters under the cursor location and 2dd deletes two lines the cursor is on.

Copy and Paste Commands

yy Copies the current line.

yw Copies the current word from the character the lowercase w cursor is on, until the end of the word.

p Puts the copied text after the cursor. P Puts the yanked text before the cursor.

Set Commands

:set nu Displays lines with line numbers on the left side

Running Commands

The vi has the capability to run commands from within the editor. To run a command, you only need to go to the command mode and type :! command.

For example, if you want to check whether a file exists before you try to save your file with that filename, you can type :! ls and you will see the output of ls on the screen.

Replacing Text

The substitution command (:s/) enables you to quickly replace words or groups of words within your files. Following is the syntax to replace text −

:s/search/replace/g

(5)

emacs Editor

Emacs is one of the oldest and most versatile text editors available for Linux and UNIX-based systems. It's been around for a long time (more than twenty years for GNU emacs) and is well known for its powerful and rich editing features. Emacs is also more than just a text editor; it can be customized and extended with different "modes", enabling it to be used like an Integrated Development Environment (IDE) for programming languages like Java, C or Python.

$emacs

Basic Command Keys

When you open a file with emacs, you can just start typing and issue commands at the same time.

Command functions in emacs usually involve two or three keys. The most common is the Ctrl

key, followed by the Alt or Esc key. In emacs literature, Ctrl is shown in short form as "C". So if you see something like C-x C-c, it means "press the Ctrl key and x together, then press Ctrl and c". Similarly, if you see C-h t, it means "press Ctrl and h together, then release both keys and press t".

Alt and Esc keys are referred to as "meta" key in emacs lingo. In Mac machines that's the

Option key and in some keyboards it's labelled as Edit. That's why emacs documentations show these keys as "M" (M for meta). Just like the Ctrl key, emacs uses multi-key functions with the meta key. So if you see a notation like M-x, it means "press Alt/Esc/Option/Edit key and x together".

(6)

The Esc key usually plays its part when you try to back off from a command or prompt. Just keep pressing it till you get out of trouble. Another way of cancelling an operation would be pressing C-g (Ctrl+g).

Saving and Quitting

Once you have made some changes to your document or written some text, you would want to save it. To save a file, press C-x C-s (Ctrl+x, followed by Ctrl+s). The mini buffer will show a message like this:

Wrote <file_path_and_name>

If you don't want to save but quit, use the command C-x C-c (Ctrl+x, followed by Ctrl+c). If there is unsaved data, the mini-buffer will show a prompt like this:

Save file <file_path_and_name>? (y, n, !, ., q, C-r, d or C-h)

Getting Around: Navigation Basics

Navigating through a long document or help topic can be a tedious task if you don't know the right keys. Fortunately, in emacs there are not too many of these to remember and they are quite intuitive. Here is a list:

CTRL-F moves the cursor forward to the next character. CTRL-B moves the cursor back to the previous character. CTRL-N moves the cursor to the next line.

CTRL-P moves the cursor to the previous line.

In addition to basic cursor motion, emacs provides some other handy cursor motion functions: CTRL-A moves the cursor to the start of the current line.

CTRL-E moves the cursor to the end of the current line. ESC-F moves the cursor forward to the next word. ESC-B moves the cursor back to the previous word. ESC-< moves the cursor to the start of the buffer. ESC-> moves the cursor to the end of the buffer.

Basic Editing Functions

We will now learn about some basic editing functions in emacs. Let's start with the simple task of selecting text.

CTRL-D deletes forward one letter.

CTRL-K deletes from the point to the end of the line. ESC-D deletes forward one word.

ESC-delete deletes backward one word.

Marking Text Regions

To mark a text region (similar to selecting text in popular word processors), follow these steps:

(7)

 Press C-Space (Ctrl + Space Bar) or C-@ to set a mark. The mini buffer will show a status message of Mark set.

 Move the cursor to the position where you want the region to end. Again, use any of the key combinations described before.

 The text will be highlighted up to the point where your cursor is now located.

 If you want to "un-mark" the highlighted text, press C-Space or C-@ twice. The mini buffer will show a status message of Mark deactivated.

Here is an example of marked text:

If you want to select the all the contents of the main buffer (i.e. "select all"), press C-x h.

Pressing M-h will select the current paragraph.

Cut, Copy & Paste

Once you have the text region marked, you can copy or cut the text and paste it elsewhere.

 For copying the text, press E-w

 For cutting the text, press C-w

 Move your cursor to the position where the text needs to be pasted.

 Press C-y (y stands for "yank" - you are yanking the text from one position to another). The contents will be pasted here.

Deleting Text

(8)

For deleting a whole word, move the cursor at the beginning of a word and press M-d. For deleting multiple words, hold the meta key down and keep pressing d. Words will start deleting one by one.

For deleting a whole line, position the cursor where you want it to be and press C-k. This would delete the text right up to the end of the line on screen.

For deleting a sentence, press M-k. Just so that we know, emacs considers a sentence to have completed when it sees two spaces after the full stop. This is unlike traditional publishing where a new sentence begins after a single space following a full stop. That's how emacs is smart enough to know even when a sentence has broken across multiple lines.

Undo & Redo

Undoing the last operation is simple. Press C-x u. You can keep repeating this to go backwards. Another key combination is C-/ (Ctrl + /) or C-_ (Ctrl + _).

For redoing your last undo, press C-g, followed by C-_ (that's Ctrl + Shift+ Underscore). Another way to do the same thing would be to press C-x C-u again (Undoing the Undo).

Search & Replace

There are two search directions in emacs: forward and backward. In forward search, the word you specify will be searched forward from the current cursor position. For backward search, it's the other way round.

 Press C-s for forward search  Press C-r for backward search

The mini-buffer will display a prompt like

I-search:

or

I-search backward:

As soon as you start typing, emacs will try to search for the text being typed and highlight any matches it finds in the main buffer.

(9)

For replacing text, follow these steps:

 Press M-% (that's Alt + Shift + %). The mini buffer shows the prompt for the text to be searched (Query replace:).

 Type the text and press Enter

 The mini buffer displays a message like (Query replace <search_word> with:)  Type the replacing text and press Enter.

 For each match emacs finds, it will ask whether you would like to make a replacement (Query replacing <search_word> with <replace_word>: (C-h for help)). You can take any of the following actions:

 Press y to replace the current match found.  Press n to skip to the next match.

 Press q to exit without any replacements (basically escaping).

 Press ! to do a global replace without any prompts. (emacs will show a message like replaced n occurrences)

Left, Right and Centre Alignment

For centering a line, move the cursor at the beginning of the line and press M-o M-s (Alt-o followed by Alt-s).

For justifying a selected text region, follow these steps:

 Create a text region to highlight the text you wish to justify  Press M-x. The mini buffer will await a response

 Start typing set-justifiction- and press Tab.

 You will be given completion options like justification-center, set-justification-left, set-justification-right, set-justification-none and set-justification-full

(10)

References

Related documents

uninteresting because they are not at the same level or lack &#34;deep&#34; interests. It is common and normal for INTJs to spend lots of time playing alone away from the others.

You must use a unique email address to create your account and keep in mind that you will use the email and password used to create the account to login at a later date to

• Scholarship Award , Arizona Psychological Association (AzPA) Annual Convention: 2006 - 2010 • Scholarship Award , The Evolution of Psychotherapy, 5 th Annual Conference, Anaheim,

micro businesses in Enugu State, Nigeria and the effect of Industrial Training Funds (ITF) on business.. skill

A resistive touch screen works by applying a voltage across a resistor network and measuring the change in resistance at a given point on the matrix where a screen is touched by

 Successfully  genera:ng  monthly  metric   reports..  No  transforma:on

Entries must be on exhibit at the official opening of the Fair or at any other time specified in the Competition Handbook and shall not be removed from the Fairgrounds prior to