Filters/Text Processors Commands
cut
Cut is a command line utility that allows you to cut parts of lines from specified files or piped data and print the result to standard output. It can be used to cut parts of a line by delimiter, byte position and character.
cut filename
= Does not workcut --version
= Check Versioncut -c1 filename
= List one charactercut -c1, 2, 4
= Pick and choose charactercut -c1-3 filename
= List range of characterscut -c1-3, 6-8 filename
= List specific range of characterscut -b1-3 filename
= List by byte sizecut -d: -f 6 /etc/passwd
= List first 6th column separated by :cut -d: -f 6-7 /etc/passwd
= List first 6th & 7th column separated by :ls -l | cut -c2-4
= Only print user permissions of files/dir
awk
awk is a utility/language designed for data extraction. Most of the time it is used to extract fields from a file or from an output.
awk --version
= Check versionawk ‘{print $1}‘ file
= List 1st field from the filels -l | awk ‘{print $1,$3}‘
= List 1st and 3rd field of ls -l outputls -l | awk ‘{print $NF}‘
= Last field of the outputawk ‘/Jerry/ ‘{print}‘ file
= Search for a specific wordawk -F: ‘{print $1}‘ /etc/passwd
= output only 1st field of /etc/passwdecho “Hello Tom“ | awk ‘{$2=’”Aditya” ; print $0}‘
= Replace words field wordscat file | awk ‘{$2=’”Aditya” ; print $0}‘
\= Replace words field words
awk ‘length($0) > 15’ file
= Get lines that have more than 15 byte sizels -l | awk ‘{if($9 == “seinfeld“) print $0;}‘
= Get the field matching seinfeld in /home/adityals -l | awk ‘{print NF}‘
= Number of fields
grep
andegrep
The grep command which stands for “global regular expression print“, processes text line by line and prints any lines which match a specified pattern.
grep --version
ORgrep --help
= Check version or helpgrep keyword file
= Search for a keyword from a filegrep -c keyword file
= Search for a keyword and countgrep -i keyword file
= Search for a keyword ignore case-sensitivegrep -n keyword file
= Display the matched lines and their line numbersgrep -v keyword file
= Display everything but keywordgrep keyword file | awk ‘{print $1}‘
= Search for a keyword and then only give the 1st fieldls -l | grep -i desktop
= Search for a keyword and then only give the 1st fieldegrep -i “keyword|keyword2“ file
= search for 2 keywords
sort
Sort command sorts in alphabetical order
sort --version
ORsort --help
= Check for version or helpsort file
= Sorts file in alphabetical ordersort -r file
= Sorts file in reverse alphabetical ordersort -k2 file
= Sort by field number, here it is sorting out by 2nd column
uniq
Uniq command filter out the repeated or duplicated lines
uniq file
= Removes duplicatessort file | uniq
= Always sort first before using uniq their line numberssort file | uniq -c
= Sort first then uniq and list countsort file | uniq -d
= Only show repeated lines
wc
The commands reads wither standard input or a list of files and generates: newline count, words count and byte count
wc --version
ORwc --help
= Check the version or helpwc file
= Check the file line count, word count and byte countwc -l file
= Get the number of lines in a filewc -w file
=Get the number of words in a filewc -b file
= Get the number of bytes in a filewc DIRECTORY
= NOT Allowedls -l | wc -l
= Number of filesgrep keyword | wc -l
= Number of keyword lines
Compare Files (diff and cmp)
cmp command-
Compares two files byte by byte
Reports the byte and line number of the first difference found
If no differences are found, it returns no output
Can be used to compare binary files
Can be used to check for corruption in files
diff command-
Compares two files line by line
Highlights changes, additions, and deletions
Provides instructions on how to change one file to match the other
Pre-installed on most Linux distributions
Compress & Un-Compress Files
Mainly, we are having 3 commands on this-
tar
Here, using tar
cvf aditya.tar .
- we’ve created a tar file or zip file.Using, tar
xvf aditya.tar
- we’ve unzipped the foldergzip
Using
gzip aditya.tar
- we’ve compressed the tar file.gzip -d
orgunzip
using
gzip -d aditya.tar.gz
, removed the compressed version from this tar file.
Truncate File Size (truncate)
The Linux
truncate
command is often used to shrink or extend the size of a file to the specified size. Actually, it chops the file based on the size. Don’t use it anywhere, it can cause issue.Command-
truncate -s 10 filename
Example-
Combining and Splitting Files
Multiple files can be combined into one and one file can be split into multiple files
cat file1 file2 file3 > file4
split file4
e.g., split -l 300 file.txt childfile
split file.txt into 300 lines per file and output to childfileaa, childfileab and childfileac
Executing Multiple Commands with ;
Syntax- Command1 ; Command2 ; Command3
It is useful during the automation
Thanks for going through this blog, Happy Learning !! 😁