# `printf`
- similar to `echo` with formatting
- `\n` new line
- `\r` carriage return
- `\t` tab character
- `%d` for integer, `%f` for float (default 6 decimal places), `%s` for string
- `printf "Hello World \n` outputs `Hello World`
- `printf "%s\n" "Hello World"` outputs `Hello World` on a new line, where "Hello World" is the argument associated with the `%s`
- `printf "[%s]\n" hello` outputs `[hello]` on new line
- `printf "[%10s]\n" hello` outputs `[ hello]`, where argument right-justified and padded with spaces to be minimum 10 characters
- `printf "[%-10s]\n" hello` outputs `[hello ]`, where argument left-justified and padded with spaces to be minimum 10 characters
- `printf "[%.9s]\n" "Hello World"` outputs `[Hello Wor]`, where argument is right-justified and truncated to 9 characters
- `printf "[%5d]\n" 43` outputs `[ 43]`, where argument right-justified and padded with spaces to be minimum 5 character
- `printf "[%05d]\n" 43` outputs `[00043]`, where argument right-justified and padded with zeroes to be minimum 5 character
- `printf "%f\n" 43.7` outputs `43.700000`, where default it 6 decimal places
- `printf "%010f\n" 43.7` outputs `043.700000`, where argument is padded with one zero to be minimum 10 characters (the . counts as a character)
- `printf "%010.2f\n" 43.7` outputs `0000043.70`, where argument is limited to 2 decimals places and padded with zeros to be a minimum 10 characters
# `uniq`
- filters out the adjacent matching lines from the input file and writes the filtered data to output file
- `uniq options input_file_path output_file_path`
- input can also be `stdin` if not specified
- -c prefix lines with number of occurrences
- -i ignore case
- -u only print unique lines ![[images/Pasted image 20250803195324.png]]
# `tr`
- translate program that runs on characters
- `tr "target_chars" "replacement_chars" < input_file` replaces one or more target characters with one or more replacement characters and sends to `stdout` ![[images/Pasted image 20250802134339.png]]
# `sed`
- stream editor runs on a file or `stdin`
- operates on strings
- by default prints result to `stdout`
- to stop this behavior add the `-n` flag
- commonly used for **substitution** purposes
- `sed 's/target/replacement/' file` substitutes first occurrence on each line of target with replacement and prints to screen
- `sed 's/target/replacement/g' file` global substitution of all occurrences of target with replacement and prints to screen
- `sed -e 's/#.*//' file` replaces commented lines in file with blank lines
- add the `-e` flag to denote usage of regular expressions
- `sed -e 's/#.*//' file | sed -e 's/^$/d'` removes commented lines in file and removes empty lines
- add `-i` to edit file in place
- add `-i[suffix]` to edit file in place and makes backup with .suffix
- `sed -n '17p' file` prints only 17th line of file
- `sed '17d' file` deletes 17th line of file and prints result on screen
- `sed '10,12!d' file` deletes all but lines 10 and 12 of file and prints result on screen
- `sed -f sed_script_path file_input` to use an external script to process file
- `sed -e 's/one/two ; s/line/circle/g' file_input` the -e flag allows for input of expression script at command line
- use -r flag for regular expression
# `awk`
- commonly used to grab specific fields/columns of delimited files or `stdin`
- delimits on space character by default
- use the `-F` flag to set a different delimiter
- `echo "hello you human" | awk '{print $3}'` prints only the third field, which is "human" to `stdout`
- `echo "hello,you,human" | awk -F "," '{print $3}'` prints "human"
- `echo "hello,you,human,person" | awk -F "," '{print $3 " " $4 "s"}'` prints "human persons"
- conditionals: `awk '$3 < 50 {print $1, $3}' file_name` if field 3 is less than 50 in a line, print fields 1 and 3 for the line ![[images/Pasted image 20250731192418.png]]
- `OFS="\t"` option replaces default space character as the output field separator with a tab character
# `cut`
- can be used to grab specific fields like `awk`
- `cut -d ":" -f 1,7 /etc/passwd` shows fields 1 and 7 of each line based on colon delimiter ![[images/Pasted image 20250802135248.png]]
- use -b to select specified bytes, -c to select specific characters
- only one of b, c, d, f may use a list at a time
# `tee`
- combine with one of the above operations to both send to a new file and display on `stdout`
- powerful redirect that works like a T fitting in plumbing ![[images/Pasted image 20250802172704.png]]