Linux Utilities
wc is a utility for counting words, newlines, bytes.
Commonly we use it for counting lines.
wc -l will count how many lines (-l argument)
sort will sort the output in alphabetical order
uniq limits the occurences of the same record to only one
less file pager
head to show the first 10 lies
tail to show the last 10 lines
-n to specify
cat dumps text file contents on screen
-A shows all non-printable characters
-b numbers lines
-s supresses repeated lines
tac Shows the text order inverted
sort: (Ordering Output)
n-> Sorts numerically rather than alphabeticallyr-> Reverses the sort orderk-> Sorts based on a specific column or key
sort -t: -k3n /etc/passwdcut (Extracting Columns)
d-> Specifies the delimiter. The character that separates the columnsf-> Specifies the field (column) number you want
cut -d: -f 1 /etc/passwdtr (Translating Characters)
d-> Deletes the specified characters entirelys-> Squeezes repeating characters into a single character- echo hello | tr [:lower] [:upper]
grep
grep [opciones] 'patron_a_buscar' archivo.txtiIgnore casevInvert matchrRRecursivenLine numberEExtended regexB5Shows the line but also the five in front of it
Text where anna is only at the begining of the word
grep ^anna /etc/passwordShows all lines that end with ash
grep ash$ /etc/passwd. Wildcard (Matches any single character)
[abc] Matches a, b, c
? 0 o 1
+ 1 o +
* 0 to infinite
\{2\} Matches exactly two of the previous character
\{1,3\} Matches a minimum of one or a maximum of three of the previous character
colou?r Matches zero or one of the previous character
(...) Used to group multiple characters so that the regular expression can be applied to the group
\b end word
awk -> Tool specialized in data extraction and reporting
By default, awk has a built-in rule: it treats any contiguous sequence of whitespace (spaces and/or tabs) as a single separator.
$1, $2, etc. -> Specific fields (columns).
Examples:
awk 'pattern { action }' fileawk -F : `{print $4}$NF -> The number of fields (The last column)
-F -> Changes the separator
awk -F ':' '/bash/ {print $1}' /etc/passwdSed -> Stream editor, used to search and transform text
Section titled “Sed -> Stream editor, used to search and transform text”It can be used to search for text, and perform an operation on matching text
sed 's/texto_a_buscar/texto_nuevo/flags' archivo.txtflags:
g -> Global substitute
Elimina la segunda línea:
sed -i -e '2d' regexesImprimir la quinta línea
sed -n '5p' archivo.txt