DEV Community

Cover image for Living in the Shell #23; cut (Cut Text/Fields from Text Streams)
Babak K. Shandiz
Babak K. Shandiz

Posted on • Originally published at babakks.github.io on

Living in the Shell #23; cut (Cut Text/Fields from Text Streams)

cut ✂️

Cuts/extracts text/fields out of text streams.

⚠️ Index values are one-based.

Cut characters on a range -c

echo "Hello World\!\nGoodbye\!" | cut -c2-5
Enter fullscreen mode Exit fullscreen mode

Takes characters at indices 2 through 5.

ello
oodb

Cut characters on a half-limited range -c

echo "Hello World\!\nGoodbye\!" | cut -c3-
Enter fullscreen mode Exit fullscreen mode

Takes characters from indices 3 and afterwards.

llo World!
odbye!

Cut fields split by any delimiter -f & -d

echo "1,20,300\n40,500,6000\n700,8000,90000" | cut -f2,3 -d','
Enter fullscreen mode Exit fullscreen mode
20,300
500,6000
8000,90000

Change fields delimiter --output-delimiter

echo "1,20,300\n40,500,6000\n700,8000,90000" | cut -f1- -d',' --output-delimiter '/'
Enter fullscreen mode Exit fullscreen mode
1/20/300
40/500/6000
700/8000/90000

Invert selection --complement

echo "1,20,300\n40,500,6000\n700,8000,90000" | cut -f2,3 -d',' --complement
Enter fullscreen mode Exit fullscreen mode
1
40
700

Top comments (0)