Text manipulation/ Searching Commands
Unix
Text manipulation/ Searching Commands
grep : it is used to search patterns and print lines matching with pattern
grep hello file1
grep world sample sample2 sample3 → it searches world word in 3 files(sample, sample2, and sample3)
awk - Finds and Replaces text, database sort/validate/index
awk 'END { print NR }' file1 → To print count of lines in a files
awk 'BEGIN { for (i = 1; i <= 7; i++) print int(101 * rand()) }' → To print seven random numbers from zero to 100, inclusive.
awk { print $2 } file1 → Used to display second line of file1
sed : SED command in UNIX stands for stream editor and it can perform lots of functions on files like, searching, find and replace, insertion or deletion. Though most common use of SED commands in UNIX is for a substitution or for find and replace. It just prints with modified patterns but not save in the files
$sed 's/unix/linux/' file1 → replace(substitute) word unix with linux
$sed 's/unix/linux/2' file1 → replace word unix with linux in second occurrence
$sed 's/unix/linux/g' file1 → replaces all occurrences of file(globally)
$sed 's/unix/linux/3g' file1 → replaces unix with linux in 3rd, 4th, 5th …etc occurrences
sed '5d' file1 → to delete 5th line
sed '3,6d' file1 → to delete range of lines (deletes 3 to 6th line)
sed '/abc/d' file1 → to delete pattern (deletes abc from file1)
sed '$d' file1 → to delete last line of file
sed '12,$d' → to delete particular line (deletes 12th line)
cut: The cut command in UNIX is a command for cutting out the sections from each line of files and writing the result to standard output.Tabs and backspaces are treated as a character.
-b(byte): To extract the specific bytes, you need to follow -b option
-c (column): To cut by character use the -c option. This selects the characters given to the -c option.
-f (field): -c option is useful for fixed-length lines. Most Unix files don’t have fixed-length lines.
–output-delimiter: By default, the output delimiter is the same as the input delimiter that we specify in the cut with -d option.
cut -f 1 file1 → if -d not specified it prints whole line in a file
cut -d " " -f 1 file1 → If -d option is used then it considered space
as a field separator or delimiter. (prints each word of the first line)
cut -d " " -f 1-4 file1 → deletes first to fourth of each line
Vi - vi is a screen visual editor, editing files using the screen-oriented text editor vi is one of the best ways
vi <filename_NEW> or <filename_EXISTING>
Edit commands
i - Insert at cursor (goes into insert mode)
a - Write after cursor (goes into insert mode)
A - Write at the end of line (goes into insert mode)
ESC - Terminate insert mode
u - Undo last change
U - Undo all changes to the entire line
o - Open a new line (goes into insert mode)
dd - Delete line
3dd - Delete 3 lines.
D - Delete contents of line after the cursor
C - Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion.
dw - Delete word
4dw - Delete 4 words
cw - Change word
x - Delete character at the cursor
r - Replace character
R - Overwrite characters from cursor onward
s - Substitute one character under cursor continue to insert
S - Substitute entire line and begin to insert at the beginning of the line
~ - Change case of individual character
Moving within a file
k - Move cursor up
j - Move cursor down
h - Move cursor left
l - Move cursor right
Saving and Closing the file
Shift+zz - Save the file and quit
:w - Save the file but keep it open
:q - Quit without saving
:wq - Save the file and quit
find file1 → used to find file1 in a directory
Comments
Post a Comment