grepfilefile-list.txtfile1.txtfile2.txtfile3.txt# Output higlights the matching text (colors enabled in the shell)# To search for lines that DO NOT match textgrep-v1file-list.txtfile2.txtfile3.txtgrep-vroot/etc/passwd|less# Check the grep man pagemangrep
# Search for the word "error" in multiple filesgreperror/var/log/*.log# Show lines before and after the matching linesgreperror-B2-A3/var/log/*.log
uniq - report or omit repeated lines, filtering adjacent matching lines from the input
# Sort the files and then use uniqsort-nrandom-numbers.txt|uniq1356891221435566678991
wc
wc - print number of lines, words and bytes in a file
wc/etc/passwd46812731/etc/passwd# Count lineswc-l/etc/passwd46# Number of line that contain the string "root"greproot/etc/passwd|wc-l2
Use pipes to create a command that uses all 4 tools:
# Find the number of uniq words in a file that don't have the letter "e"grep-verandom-words.txt|sort|uniq|wc-l
Manipulating Text
sed
sed - stream editor for filtering and transforming text
A stream editor is used to perform basic text transformations on an input stream.
sed manipulates text line by line
# Substitution# Substitute the word "8" with the word "sub" in a file# is only replaces the first instance on a linesed's/8/sub/'random-numbers.txt9sub631663sub9112556721sub9435echoTestTest|sed's/Test/Word/'WordTest# Globarl substitution, add "/g" at the endechoTestTest|sed's/Test/Word/g'WordWord# Delete the entire line if the word "root" is presentsed'/root/d'/etc/passwd# [First line with root is not present]daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologinbin:x:2:2:bin:/bin:/usr/sbin/nologinsys:x:3:3:sys:/dev:/usr/sbin/nologin# Matching criteria + Substitution# Substitutre a word only when the line contains "oo"grepoo/etc/passwdsed'/oo/ s/bin/TEST/g'/etc/passwd
# Replace the end of each line ($) with a new line character (\n)+sed's/$/\n/g'/etc/passwdroot:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologinbin:x:2:2:bin:/bin:/usr/sbin/nologin# Output to another sed command that replaces the colon (:) with a new linesed's/$/\n/g'/etc/passwd|sed's/:/\n/g'# or in one command (expression script)sed-e's/$/\n/g'-e's/:/\n/g'/etc/passwd
awk
awk - pattern scanning and text processing language
awk breaks each line of input into separate fields or columns using specific delimiters.
The default delimiter is "space".
Each field is numbered starting at 1.
# Print the second fieldecholinuxusercommands|awk'{print $2}'userecholinuxusercommands|awk'{print "learn", $1, $3}'learnlinuxcommands# Extract the first field of a file. /etc/passwd uses colon : to sepparate the different fields, not spacesawk-F':''{print $1}'/etc/passwdrootdaemonbinsyssyncgamesman [...]# Examples:awk-F':''{print $1}'/etc/passwd|sort# Print the output of awk using awkawk-F':''{print $1}'/etc/passwd|awk'{print "user:", $1}'user:rootuser:daemonuser:binuser:sysuser:sync [...]# Filter lines based on a pattern, for example word "root"cat/etc/passwd|greprootawk-F':''/root/ {print $1}'/etc/passwdrootnm-openvpn# Print which line correspond the the filtered wordawk-F':''/root/ {print NR,$1}'/etc/passwd1root37nm-openvpn
tr
tr - translate, squeeze, delete characters from the standard input
cat/etc/passwd|tr':''\t'# \t is for horizontal tab# Replace all the lower case letters with uppercase letterscat/etc/passwd|tr'a-z''A-Z'cat/etc/passwd|tr'[:lower:]''[:upper:]'