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

3 2

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay