DEV Community

Cover image for BASH - Main Commands_(2)
Israel-Lopes
Israel-Lopes

Posted on • Updated on

BASH - Main Commands_(2)

Text file manipulation commands

Cat

First command is cat. cat writes the contents of a file to the screen.

machine@machine:~ cat exemplo

Red
yellow
brown
black
white
purple
blue

Enter fullscreen mode Exit fullscreen mode

On the cat, we have the -b option that numbers the lines that are not blank.

machine@machine:~ cat exemplo -b

     1  Red
     2  yellow
     3  brown
     4  black
     5  white
     6  purple
     7  blue

Enter fullscreen mode Exit fullscreen mode

Main flags of cat:

  • -b numbers all non-blank lines.
  • -n numbers all lines, even blank ones.
  • -a show special characters and tabs.

Another cat-like command is tac. They read the contents of a file backwards.

machine@machine:~ tac exemplo

blue
purple
white
black
brown
yellow
Red
Enter fullscreen mode Exit fullscreen mode

Tail

tail shows the last lines of a file.

tail <file.txt>

flags:

  • -n5 show the last 5 lines of the file
  • -n10 lasts ten lines.

In this flag above you can define the number of lines you would like to see, just change it.

Head

The head command displays the first 5 lines of a file.

flags:

  • -n5 show the first 5 lines of the file
  • -n10 First ten lines.
  • -c10 Displays the first ten characters of the file

Wc

wc counts the number of characters, words, lines and bytes in a file.

wc <file.txt>

flags:

  • -l specifies number of lines
  • -w specifies number of words
  • -m specifies number of characters
  • -c specifies number of bytes

Sort

The use of sort is to sort the contents of a file.

sort <file.txt>

With the -r option, reverse order is also possible.

Uniq

uniq writes to output without repeating words from a file

  • -u show lines that have not been repeated
  • -d show lines that have been repeated
  • -c counts the number of repeated lines

OBS: To be able to use uniq it must be used together with sort before being able to sort the file's contents.

___________<< back______________next page >>__________

Texto alternativo da imagem

Top comments (0)