Filtering Commands



&&



ls file.txt && echo "astonishing success"
sudo apt update && sudo apt update


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# file.txt content
# someone:linux
# dave:we
# dave:we


cut



cat file.txt | cut -d: -f2
# -d - delimiter
# -f - field

# Output
# linux
# we
# we


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sort



# Display result on stdout
sort file.txt

cat file.txt | sort -bf
# -b - ignore leading blanks
# -f - case insensitive

# Output
# dave:we
# someone:linux


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

uniq



# Show unique records
cat file.txt | uniq


# Show unique
# Method 1
sort file.txt > sorted.txt
uniq sorted.txt

# Method 2
sort -u file.txt


# Show duplicates
uniq -d sorted.txt


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

wc



wc -l file.txt                  # count number of lines
wc -w file.txt                  # count number of words
wc -m file.txt                  # count number of characters
wc -c file.txt                  # count number of bytes


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

grep



grep user file.txt
# or
cat file.txt | grep user


# Search all files in current directory
grep someone ./*


grep unique sample.txt          # string containing '*unique*'
grep -w unique sample.txt       # -w - exact word match
grep -A 3 linux sample.txt      # plus 3 lines after matching pattern
grep -B 3 linux sample.txt      # plus 3 lines before matching pattern
grep -c unique sample.txt       # -c - count
grep -v linux sample.txt        # -v - reverse



Index