Changing Characters and Tidying Up: tr, nl, paste and column

tr: translate characters

tr replaces characters one for one. It only reads standard input, so feed it with a pipe or <:

echo "hello world" | tr a-z A-Z
echo "hello world" | tr " " _
echo $PATH | tr : "\n"

In the terminal

student@vulntech:~$ echo "hello world" | tr a-z A-Z
HELLO WORLD
student@vulntech:~$ echo "hello world" | tr " " _
hello_world
student@vulntech:~$ echo $PATH | tr : "\n"
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin

-d deletes characters and -s squeezes repeats into one:

echo "Call 555-0199 now" | tr -d 0-9
echo "too     many    spaces" | tr -s " "

In the terminal

student@vulntech:~$ echo "Call 555-0199 now" | tr -d 0-9
Call - now
student@vulntech:~$ echo "too     many    spaces" | tr -s " "
too many spaces

nl: number the lines

nl fruit.txt

In the terminal

student@vulntech:~$ nl fruit.txt
     1	pear
     2	apple
     3	fig
     4	apple
     5	banana
     6	fig
     7	apple

paste: join lines

paste -sd, joins all the lines into one, with commas between them:

paste -sd, fruit.txt

In the terminal

student@vulntech:~$ paste -sd, fruit.txt
pear,apple,fig,apple,banana,fig,apple

column: line things up

column -t turns columns into a neat table. -s, says the columns are separated by commas:

column -t -s, scores.csv

In the terminal

student@vulntech:~$ column -t -s, scores.csv
name  team   points
Ada   red    42
Bo    blue   7
Cy    red    15
Di    green  108
Eli   blue   23
Flo   green  9
Gus   red    15

Remember: tr swaps (a-z A-Z), deletes (-d) or squeezes (-s) characters. nl numbers lines, paste -sd, joins them, column -t lines them up.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…