From aa607abac50ce7a2f8032dec9e4af0a5585f6b5c Mon Sep 17 00:00:00 2001 From: Adnan Ahmed Date: Sat, 1 Apr 2017 16:46:15 +0400 Subject: [PATCH] add text commands --- README.md | 139 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 112 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 5b1147c..7529534 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ ## Table of Contents 1. [Bash Basics](#1-bash-basics) 1.1. [File Operations](#11-file-operations) - 1.2. [Directory Operations](#12-directory-operations) - 1.3. [SSH, System Info & Network Operations](#13-ssh-system-info--network-operations) + 1.2. [Text Operations](#12-file-operations) + 1.3. [Directory Operations](#13-directory-operations) + 1.4. [SSH, System Info & Network Operations](#14-ssh-system-info--network-operations) 1. [Basic Shell Programming](#2-basic-shell-programming) 2.1. [Variables](#21-variables) 2.3. [String Substitution](#22-string-substitution) @@ -87,7 +88,6 @@ Clears content on window diff - wc chmod gzip gunzip @@ -95,7 +95,6 @@ Clears content on window lpr lpq lprm - grep @@ -190,55 +189,42 @@ Syntax: diff filename1 filename2 ``` -### k. `wc` -Tells you how many lines, words and characters there are in a file -Syntax: -```bash -wc filename -``` -Example: -```bash -$ wc demo.txt -7459 15915 398400 demo.txt -``` -Where `7459` is lines, `15915` is words and `398400` is characters. - -### l. `chmod` +### k. `chmod` Lets you change the read, write, and execute permissions on your files Syntax: ```bash chmod -options filename ``` -### m. `gzip` +### l. `gzip` Compresses files Syntax: ```bash gzip filename ``` -### n. `gunzip` +### m. `gunzip` Un-compresses files compressed by gzip Syntax: ```bash gunzip filename ``` -### o. `gzcat` +### n. `gzcat` Lets you look at gzipped file without actually having to gunzip it Syntax: ```bash gzcat filename ``` -### p. `lpr` +### o. `lpr` Print the file Syntax: ```bash lpr filename ``` -### q. `lpq` +### p. `lpq` Check out the printer queue Syntax: ```bash @@ -252,14 +238,67 @@ active adnanad 59 demo 399360 bytes 1st adnanad 60 (stdin) 0 bytes ``` -### r. `lprm` +### q. `lprm` Remove something from the printer queue Syntax: ```bash lprm jobnumber ``` -### s. `grep` +## 1.2. Text Operations + + + + + + + + + + + + + + + + + + + + +
awkgrepwcsedsortuniqcatcutechofmt
trnlegrepfgrep
+ +### a. `awk` +Awk is most usefull command for handling text files. It operates on entire file line by line. By default it uses whitespace to separate the fields. The most common syntax for awk command is + +Syntax: +```bash +awk '/search_pattern/ { action_to_take_if_pattern_matches; }' file_to_parse +``` + +Lets take following file `/etc/passwd`. Here's the sample data that this file contains. +``` +root:x:0:0:root:/root:/usr/bin/zsh +daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin +bin:x:2:2:bin:/bin:/usr/sbin/nologin +sys:x:3:3:sys:/dev:/usr/sbin/nologin +sync:x:4:65534:sync:/bin:/bin/sync +``` +So now lets get only username from this file. Where `-F` specfies that on which base we are going to separate the fields. In our case it's `:`. `{ print $1 }` means print out the first matching field. +```bash +awk -F':' '{ print $1 }' /etc/passwd +``` +After running the above command you will get following output. +``` +root +daemon +bin +sys +sync +``` +For more detail on how to use `awk`, check following [link](https://www.cyberciti.biz/faq/bash-scripting-using-awk). + +### b. `grep` Looks for text inside files. You can use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. Syntax: ```bash @@ -278,7 +317,53 @@ $ grep -r admin /etc/ ``` And `-w` to search for words only. -## 1.2. Directory Operations +### c. `wc` +Tells you how many lines, words and characters there are in a file +Syntax: +```bash +wc filename +``` +Example: +```bash +$ wc demo.txt +7459 15915 398400 demo.txt +``` +Where `7459` is lines, `15915` is words and `398400` is characters. + +### d. `sed` +TODO + +### e. `sort` +TODO + +### f. `uniq` +TODO + +### g. `cat` +TODO + +### h. `cut` +TODO + +### i. `echo` +TODO + +### j. `fmt` +TODO + +### k. `tr` +TODO + +### l. `nl` +TODO + +### m. `egrep` +TODO + +### n. `fgrep` +TODO + +## 1.3. Directory Operations @@ -312,7 +397,7 @@ Syntax: pwd ``` -## 1.3. SSH, System Info & Network Operations +## 1.4. SSH, System Info & Network Operations