Split the difference between two files, side by side
comm
Display lines that are common to both files
Capture output
In Bash scripting, you can use backticks ` ` or $() to warp and capture the output of an expression.
Consider the following examples
A=1A='expr $A + 1'echo $A
A=1A=$(expr $A + 1)echo $A
Both scripts are valid, in the second line, the output from expression expr $A + 1 is captured and assigned to the variable A.
(The expr is a command used to perform arithmetic calculation, find more on Bash Basic Operators )
Pipe
Pipe can take the output (standard output) of one process or command and feed it as an input (standard input) to the other process or command.
Consider the following example
echo hello | wc
The command takes the output from echo hello which is hello and feed it into the wc process.
The wc then count the words, lines, and characters of the input, then prints the follow to the screen.
1 1 6
Redirection
Redirection is a technique that allows you to change the default source or destination of input and output for a command. It is typically used with files or devices.
Different from piping, connecting the output of one command to the input of another command, redirection deals with files sources and destination.
There are two primary redirection
Output redirection (’>’ and ’>>’)
You can use the > symbol to redirect the standard output (stdout) of a command to a file, overwriting the file’s contents.
command > output.txt
You can you >> to append the output to a file.
command >> output.txt
You can even redirect standard error file output to an other destination using 2>
command 2> error.log
Input redirection (<)
You can use the < symbol to redirect the standard input (stdin) of a command from a file.
command < input.txt
Word count (wc)
The wc command is used to find the number of lines, words, characters from an standard input or text files.
wc [options] [file(s)]
wc options
Count lineswc -l or --lines: counts the number of lines in the input.
Count wordswc -w or wc --word: count the number of words in the input.
Count characterswc -c or wc -- bytes: count the number of characters (bytes) in the input.
Word count from an input
echo hello | wc
1 1 6
The output is in order of number of lines in the input, number of words in the input, and number of characters in the input (including a new line character which is why 6 here).
Word count from a file
wc -l example.txt
This command counts the number of lines in the example.txt file.
Extract (cut)
The cut command is used to extract specific sections (columns) of lines from files or standard input. It is particularly useful for working with structured text data, such as delimited file, where you want to isolate and display specific fields or portions of each line.
cut [options] [file]
cut options
Extract specified fieldscut -f: this option specifies which fields (columns) to extract from each line. You can specify the fields using a comma-separated list or a range of fields.
cut -f1,3 extracts the first and third fields.
cut -f2-4 extracts fields 2 through 4.
Extract with delimitercut -d: you can set a delimiter that separates fields in each line.
cut -d `,` -f2
This command extracts the second field when the delimiter is a comma.
Sort (sort)
Sort arranges lines in ascending (default) or descending order based on various criteria, such a alphabetical order, numerical order, or customised sorting rules.
By default, the sort command sorts input in ascending order, which is from smallest to largest or alphabetical.
sort [option] [file]
Sort options
Reverse sort-ror--reverse: this option reveres the sort order, causing lines to be sorted in descending order.