Text processing commands in Linux

Filters/Text processing commands
cut- allows us to cut the output, takes input of command/content of file and cut it to our desired output
awk- list by columns
grepandegrep- search by the keyword. Like searching for a specific word.
sort- sorts out the output in alphabetical order
uniq- not show duplicates in a file.
wc- word count command waca command. tells us how many words, letters, line we've in a file/output.
cut - Text Proecssors Commands
Cut is a command line utility that allows us to cut parts of lines from specified files or piped data and print the result to stdout. It can be used to cut parts of a line by delimiter, byte position, and character.
cut filename= doesn't work you've to specify options in this commandcut --version= check utility version.cut -c1 filename= list one charactercut -c1,2,4 filename= Pick and chose charactercut -c1-3 filename= list range of characterscut -c1-3,6-8 filename= list specific range of characters.cut -b1-3 filename= list by byte sizecut -d: -f 6 /etc/passwd= list first 6th column separated by :ls -l | cut -c2-4= only print user permissions of files/dir
awk - Text processors commands
awkis a utility/language designed for data extraction. Most of the time it is used to extract fields from a file or from an output.check version :
awk --versionprint 1st column of character in a file:
awk '{print $1}' filesecond column/field:
awk '{print $2}' fileoutput specific columns:
ls -l | awk '{print $1,$3}'last field of the output:
ls -l | awk '{print $NF}'search for specific keyword:
awk '/dev/ {print}' txoutput only first fields of file:
awk -F: '{print $1}' /etc/passwdreplace words fields words:
echo "Hello Tushar" | awk '{$2="Rajpoot"; print $0}'$0means whatever you find print it now.
replace words in output of file:
cat tx | awk '{$1="Tushar"; print$0}'get lines that have more than 3 bytes:
awk 'length($0) > 3' txGet the field matching seinfeld in /home/tushar:
ls -l | awk '{if($9 == "seinfeld") print $0;}'Number of fields:
ls -l | awk '{print NF}'
grep/egrep - Text processors commands
The grep command which stands for
global regular expression printprocesses text line by line and prints any lines which match a specific pattern.Check version or help:
grep --version,grep --helpSearch for a keyword from a file:
grep keyword fileSearch for a keyword and count:
grep -c keyword fileSearch for a keyword and ignore case-sensitive:
grep -i KEYword fileDisplay the matched lines and their line numbers:
grep -n keyword fileDisplay everything but keyword(exclude a keyword):
grep -v keyword fileSearch for a keyword and then only give the 1st field:
grep keyword file | awk '{print $1}'Search for a keyword and then only give the 1st field:
ls -l | grep DesktopSearch for 2 keywords:
egrep -i "keyword|keyword2" file
sort/uniq - Text processors commands
Sortcommand sorts in alphabetical order.uniqcommand filters out the repeated or duplicate lines.check version or help:
sort --versionorsort --helpsort files in alphabetical order ```sort file``
sort file content in reverse order:
sort -r filesort by field number:
sort -k2 fileremove duplicates:
uniq filesort command output:
ls -l | sortremove duplicates in command output:
ls -l | uniqAlways run sort first using uniq their line numbers:
sort file | uniqSort first then uniq and list count:
sort file | uniq -cOnly show repeated lines:
sort file | uniq -d
wc - Text processors commands
wccommand either reads stdin or stdout or a list of files and generates:newline count, word count and byte countcheck file line count, word count and byte count:
wc fileGet the number of lines in a file:
wc -l fileNumber of words:
wc -w fileNumber of bytes:
wc -c fileNOT ALLOWED:
wc DIRECTORYNumber of files:
ls -l | wc -lCount directories only:
ls -ld */ | wc -lNumber of keyword lines:
grep keyword | wc -l





