Archive for the ‘ Vi & String Processing ’ Category

Save File as in VI Editor

You can save file as another file in vi editor after you make changes to the current file, so that you can quit without saving and still retain the changes in another file.

:w! newfile.txt

or

:w! /var/newfile.txt

Thanks

Introduction to String Processing

Introduction to string Processing

Head/Tail

Head and tail command are used to view the first and last few lines of a file respectively.

By default these commands generate 10 number of lines.

# head /etc/passwd (shows first 10 lines)

# tail /etc/passwd (shows last 10 lines)

One very handful command to monitor log messages

#tail -f /var/log/messages  (this is used to see the online activities)

wc: Word count

This command is used to count number of lines, words or characters in a file

Syntax: wc <option> filename

options: -l counts number of lines

-w counts number of words

-c counts number of characters

# wc -wlc *

Cut: cut command cut the specified field from the file of known formats or to cut first few characters

#cut <option> <file name>

options:  d -delimiter or field separator

-f{number} -field number

-c -to cut the characters

#cut -d: -f1 /etc/passwd (shows only the first field of /etc/passwd’s contents

#cut -d: -f1-3 /etc/passwd (shows only first second and third fields of /etc/passwd contents)

#cut -c1-10 /etc/passwd (shows first 10 characters of file’s line)

SORT

sorting is the arrangement of data/information in increasing (ascending) or decreasing(descending) pattern.

#sort <options> file name

options: -r -reverse order

-n -numeric order

-t -as field separator, delimiter

-k position

-u unique sort

#sort /etc/passwd

#sort -r /etc/passwd

#cut -d:-f7 /etc/passwd|sort -u

#sort -t: -K1 /etc/passwd

Uniq: uniq moves the duplication and returns only the unique items.

# cut -d -f7 /etc/passwd|uniq -c (also counts number of counts for the item)

String processing with regular expressions

String Processing with regular expression

* Regular Expression . Regular expression is a pattern matching engine. . It is used by different tools like vi, grep, sed, awk etc.

* Modifiers . It determines the no. of previous character.

Modifier Meaning

* – Matches zero or more of the previous char.

\+ – ,, one ,, ,, ,, ,, ,, ,,

\? – ,, zero or one of the previous char.

\{i\} – ,, exactly ‘i’ of the ,, ,,

\{i,\} – ,, ‘i’ or more ,, ,, ,,, ,,

\{i,j\} – ,, between ‘i’ & ‘j’ ,, ,,, ,,

* Anchors .

It determines the begining or end of a line or a word.

Anchors Meaning

^ – Line begins with

$ – Line ends ,,

\< – word begins with

/\ apply above command is test file

Grep (genela regular expression processor) .

It seraches for tjhe specified pattern in a file & displays all these lines that have at least one machting pattern.

syntax: grep [pattern] [option] [argument]

-v – do not display the lines that have matching pattern

-c – display only the total count of lines that name matching pa ttern

-n – proceede the lines having matching pattern with line no.

-l – Lists all the fuiles in the specified dir that have at leas r one matching pattern.

Examples

#grep bash /etc/passwd

#grep -v bash /etc/passwd

#grep -c bash /etc/passwd

#grep -n bash /etc/passwd

#grep -l bash /etc/passwd

Sed command ———-—-

. It is used to perform search & replace

expression

#sed ‘s/old text/new tet /g’ source_file>output file

#sed ‘s/boy/girl/g’ test >sed_text

#sed ‘1,20s/boy/girl/g’ test>outfile

#sed -e ‘s/boy/girl/g’ -e ‘s/cat/dog/g’ test>output file

-e for to perform mutiple search & replace operation in the same file script

examples

#vi sd s/boy/girl/g s/cat/dog/g

#sed -f

Vi Editor

The VI editor

The standard Unix/Linux editor

-Now we have vim-improved with move feature.

-When Vi is used system invokes ‘Vim’ because vi is defined alias to vim.

Starting:

# Vi filename

(Open an existing file or editing or a new file is created if the current buffer is saved)

Three operating modes of vi:

1.Command Mode: All the vi command and cursor movement key-strokes can be used. Command such as copying, pasting (put), undo, redo is run is command mode

2.Insert Mode: Actually whatever we want to write from the keyboard to the file is done in this mode. Simply insert mode takes input to the file.

3. Execution Mode: Searching for text, saving, quitting etc is done from here normally followed by (/ or :)

Note:   1. Use: set nu (or number) to display the line number)

2 Use: set nonu (to hide line number)

3. To use color full text in vi this package should be installed

# rpm -q vim-enhanced

INSERT:

a- Insert after the cursor

i- Insert before the cursor

o- Insert to a line below

A= Append to the end of line

I= Insert at the beginning of the file

O= Insert to a line above

Copy, Paste, Delete, Undo, Redo

——————————–

yy – to copy the current line

3yy- copies 3 lines below the cursor position

p – Put below (paste) below the line.

P – Paste above the line

dd – Deletes the current line

5dd -Deletes 5 lines below the cursor

u – Undo the recent change

U – Undo the changes to present line

Ctrl+r -redo

Execution Mode:

:w – write but stay in the buffer.

:q – quit q! (Forcefully quit the buffer)

:wq – write and quit

:x     ,,    ,,   ,,

zz – from command mode.

:wq /var/test (saves in different location)

:r /etc/hosts (opens different file in the same buffer)

:r !cal , :!date – to execute commands

/ and enter searching word to find

eg. /Linux  (text -forward

? And enter searching word to find (backward)

:set ignorecase or set ic (it igone the text case)

:set noic (to disable case)

n= next search in same

N= Next search in reverse direction

Introduction to string processing

Introduction to string Processing

Head/Tail

Head and tail command are used to view the first and last few lines of a file respectively.

By default these commands generate 10 numbers of lines.

# head /etc/passwd (shows first 10 lines)

# tail /etc/passwd (shows last 10 lines)

One very handful command to monitor log messages

#tail -f /var/log/messages  (this is used to see the online activities)

wc: Word count

This command is used to count number of lines, words or characters in file

Syntax:

wc <option> filename

Options: -l counts number of lines

-w counts number of words

-c counts number of characters

# wc -wlc *

Cut: cut command cut the specified field from the file of known formats or to cut first few characters

#cut <option> <filename>

Options:  d -delimiter or field separator

-f{number} -field number

-c -to cut the characters

#cut -d: -f1 /etc/passwd (shows only the first field of /etc/passwd’s contents

#cut -d: -f1-3 /etc/passwd (shows only first second and third fields of /etc/pas                                    swd contents)

#cut -c1-10 /etc/passwd (shows first 10 characters of file’s line)

Reading an Input From File

Reading an input from a file:

Command <file>

#tr  ‘a-z’ ‘A-Z <file (Translates all the lowercase characters to uppercase).

Note: if you want to print the output of existing file to new file

Use this command

#tr `a-z’ `A-Z'<oldfilename>newfilename