| Viewing and Editing Text Files: nano, vim Basics |
|
Linux stores most everything as plain text use CLI tools to open and edit files directly two main tools for terminal text work are nano and vim The Beginners Friend: nano
simple, intuitive, safebehaves like a standard notepad to open or create a file nano <filename>opens a terminal-based UI uses keyboard exclusively for navigation and entry menu at the bottom of screen carat ^ represents the Ctrl key
The Professional's Tool: vim
vim stands for Vi Improveddifficult to learn but is incredibly powerful has two Modes
The vim Survival Guide
|
| Searching Within Files: grep and Regular Expressions |
|
grep - Global Regular Expression Print searches for a pattern and prints every line containing a match syntax grep "<search term>" <file>look for user 'root' in password config file grep "root" /etc/passwdgrep is case-sensitive to search for both 'error' and 'Error' use insensitive (-i) flag grep -i "error" /var/log/syslogfor a recursive search use the -r flag grep -r "<search term>" /<directory path> Introduction to Regular Expressions (Regex)
special characters defining a basic search pattern
|
| Text Stream Processing: cut, sort, uniq, wc |
|
Word Count: wc
wc counts lines, words and characters
wc /etc/passwdoutput 45 130 2500 /etc/passwd45 lines, 130 words, and 2500 characters to just get a line count use -lflag wc -l /etc/passwd Sorting: sort
command rearranges lines of a file alphabetically or numerically
sort names.txtto sort a list of numbers use the numeric sort -n flag to reverse the sort order use the -r flag Removing Duplicates with uniq
uniq can filter duplicates only if the they are adjacentalmost always run sort before uniq sort names.txt | uniqto know how many times each line appeared use -c flag sort names.txt | uniq -c Slicing with cut
cut can extract specific columns from a fileexample line from file where data is separated by colons (:) root:x:0:0:root:/root:/bin/bashsyntax cut -d <delimiter> -f <field number(s)> <filename>to get a list of users from the user field cut -d : -f 1 /etc/passwdNote : column numbers are not zero-based |
| Comparing Files: diff and cmp |
|
diff command
diff compares two files line by line and outputs the changes needed to make file A look like file B
diff file1.txt file2.txtoutput uses < and >
cmp command
compares files byte by bytemostly used to check binary files if files are the same the command returns silently if files are different, result displays the byte number where the difference occurs |
| Transforming Text: tr, sed Basics, awk Introduction |
|
Translating Characters with tr
tr is used to swap or delete individual charactersis strictly a stream tool (takes input from another command) converting lowercase to uppercase echo "hello world" | tr "a-z" "A-Z"output HELLO WORLDdeleting specific characters echo "Hello 123" | tr -d "0-9"output Hello The Stream Editor: sed
sed is a programmable text editorcan modify a file while the data is flowing through it most common use is substitution (Find and Replace) syntax s/old_word/new_word/gcommand echo "I love Windows" | sed 's/Windows/Linux/g'sed arguments
sed can be used to edit a file in place using -i flag sed -i 's/false/true/g' config.txt The Powerhouse: awk
awk is actually a full programming language disguised as a commandis incredibly powerful for processing data organized in columns cut requires a single character delimiter awk handles whitespace automatically example file John Manager 50000 Sarah Engineer 60000to print the name and salary columns one-based column numbers awk '{print $1, $3}' employees.txt
|
| Redirecting Output: >, >>, < |
|
redirect command output from screen (stdout) as a stream to a file
Overwrite (>)
sends output to a fileif file exists it will be erased and replaced ls > filelist.txt Append (>>)
appends output to existing file
date >> log.txt Input Redirection (<)
feeds a file to a command
sort < names.txtcommand output goes to stdout Standard Error
third stream Standard Error stderrif an error occurs the error message goes to stderr and not stdout to redirect errors ls 2> errors.txt |
| Piping Commands: The Power of | |
|
a pipe takes the output of one command and uses itas input to another command like an assembly line Raw Data -> [Machine 1] -> Semi-finished Data -> [Machine 2] -> Finished Productno need to save intermediate files to page through a long list ls -l | lesslogic example grep "error" /var/log/syslog | wc -l
|
| Combining Commands for Powerful Text Processing Workflows |
example web log format192.168.1.50 - - [Date] "GET /page.html" 200 ... 10.0.0.1 - - [Date] "GET /index.html" 200 ... 192.168.1.50 - - [Date] "GET /image.jpg" 200 ...want to get the top three users
|
| Summary |
covered
key points
|