Last active
August 3, 2022 13:58
-
-
Save andfaulkner/48f3784a0e1d984cb76f to your computer and use it in GitHub Desktop.
Unix "cut" examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
which node | |
#-->node: /usr/bin/node /usr/bin/X11/node /usr/local/bin/node /usr/share/man/man1/node.1.gz | |
which node | cut -d':' -f1 | |
#-->node | |
which node | cut -d':' -f2 | |
#--> /usr/bin/node /usr/bin/X11/node /usr/local/bin/node /usr/share/man/man1/node.1.gz | |
which node | cut -d':' -f3 | |
#--> [[[[empty]]]] | |
################################### | |
### CHAINING CUT CALLS TOGETHER ### | |
################################### | |
whereis node | cut -d':' -f2 | cut -d'/' -f2 | |
#-->usr | |
whereis node | cut -d':' -f2 | cut -d'/' -f2 | |
#-->bin | |
whereis node | cut -d':' -f2 | cut -d'/' -f2 | |
#-->bin | |
## USING <SPACE> AS DELIMITER | |
whereis node | cut -d':' -f2 | cut -d' ' -f2 | |
#-->/usr/bin/node | |
whereis node | cut -d':' -f2 | cut -d' ' -f3 | |
#-->/usr/bin/x11/node | |
whereis node | cut -d':' -f2 | cut -d' ' -f4 | |
#-->/usr/local/bin/node | |
whereis node | cut -d' ' -f1 | |
#-->node: | |
whereis node | cut -d' ' -f2 | |
#-->/usr/bin/node | |
################################### | |
######## CHARACTER SLICING ######## | |
################################### | |
#...slice off the beginning... | |
whereis node | cut -c1- | |
#-->node: /usr/bin/node /usr/bin/X11/node /usr/local/bin/node /usr/share/man/man1/node.1.gz | |
whereis node | cut -c2- | |
#-->ode: /usr/bin/node /usr/bin/X11/node /usr/local/bin/node /usr/share/man/man1/node.1.gz | |
whereis node | cut -c3- | |
#-->de: /usr/bin/node /usr/bin/X11/node /usr/local/bin/node /usr/share/man/man1/node.1.gz | |
#...extract a range... | |
whereis node | cut -c1-13 | |
#-->node: /usr/bi | |
whereis node | cut -c2-13 | |
#-->ode: /usr/bi | |
#...extract some random list of characters (at positions 3, 13, 14, 21, 28, 30)... | |
whereis node | cut -c3,13,14,21,28,30 | |
#-->din/nX | |
#...extract individual characters (at positions 3, 21, 28, 30), and a range of characters (positions 13 to 18)... | |
whereis node | cut -c3,13-18,21,28,30 | |
#-->din/nod/nX | |
################################### | |
########## FIELD RANGES ########### | |
################################### | |
#remember, when you show all fields from "1" to "end"... | |
whereis node | cut -d' ' -f1- | |
#-->node: /usr/bin/node /usr/bin/X11/node /usr/local/bin/node /usr/share/man/man1/node.1.gz | |
#now fields 1 to 2 (where field is delimited by a space)... | |
whereis node | cut -d' ' -f1-2 | |
#-->node: /usr/bin/node | |
#now fields 2 to 3 (where field is delimited by a space)... | |
whereis node | cut -d' ' -f2-3 | |
#-->/usr/bin/node /usr/bin/X11/node | |
#now fields 2 to 4 (where field is delimited by a space) | |
whereis node | cut -d' ' -f2-4 --output-delimiter=$'\n' | |
#-->/usr/bin/node /usr/bin/X11/node /usr/local/bin/node | |
#fields 2 to 4, with field delimited by 5 spaces | |
whereis node | cut -d' ' -f2-4 --output-delimiter=' ' | |
#-->/usr/bin/node /usr/bin/X11/node /usr/local/bin/node | |
#fields 2 to 4, with field delimited by a new line | |
whereis node | cut -d' ' -f2-4 --output-delimiter=$'\n' | |
#-->/usr/bin/node | |
#-->/usr/bin/X11/node | |
#-->/usr/local/bin/node |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment